# How to use a transistor to drive strings of LEDs from GPIO pins?

This is similar to How to drive a 20mA LED From a 4mA max GPIO Pin except that I need a little more hand-holding in order to understand what is happening.

For background, I'm a software engineer and currently only know enough about electronics to successfully test a 9V battery will my tongue.

I have an Arduino Uno board which I have rigged to separately control 3 LEDs blinking in a "chase lights" type of configuration. I have connected each GPIO pin directly to an LED, a resistor, and back to the board's +3V ground pin. The software strobes the GPIO pins at intervals to achieve the desired effect.

With one LED wired, everything looks great. When adding a second LED to each of the 3 small circuits (to "extend" my string of chasing-lights), the LEDs dim considerably. My plan was to eventually wire a string of 10 LEDs for each circuit for a total of 30 LEDs. Obviously, if I can't light 2 LEDs, 10 is out of the question.

So I thought of using a transistor for each circuit with the GPIO merely providing the "trigger" for lighting the strand of 10 LEDs. I have wired-up a few LEDs, a resistor, and the transistor, and connected it all to a 9V battery. It's dark, as expected. There is only one base pin on the transistor, so obviously the GPIO pin should be connected to that, but how should I correctly complete the circuit back to the Arduino board?

I experimented by taking a 3V battery and connecting the negative terminal to the transistor's emitter pin and then touching the base pin as a trigger and it seemed to work, thought the LEDs flickered a little. That might just be because my hands weren't super-steady. Is this the right approach for working with the Arduino's GPIO pins?

Here are all the electrical specs for the components I have in front of me, because the math matters, of course.

Arduino GPIO: 5V LEDs: NTE30143 5V 25mA (continuous forward current) Transistor: 2N4401 600mA

What's the "proper" way to use the Arduino's GPIO pins to light a string of 10 LEDs? Can it be done with the components above, or should it really be done with a more complex circuit?

I'm building this as a prototype for some elementary school students to build themselves, and I'd rather make the circuit as simple as possible.

(Bonus points for explaining where the resistors need to go, what their values need to be, and why they need to be there in the first place :)

UPDATE

I forgot to mention that I want to wire each string of 10 LEDs in series, not in parallel I want to minimize the amount of wire required. These LEDs will end up being maybe 4-5 inches apart and the entire string of 30 LEDs will make a big loop, so I don't want to string each LED individually. I'd like to have 3 large loops of 10 LEDs each with all of the controls in a single place.

UPDATE

One last requirement (I think): this has to run off of batteries. No mains power. :)

• Comments are not for extended discussion; this conversation has been moved to chat. Any conclusions reached should be edited back into the question and/or any answer(s). Jan 16, 2019 at 23:38
• Just FYI, 9V batteries, are expensive and have poor capacity. If you want to use alkaline batteries, AA or AAA will be much better. If you want the best rechargeable, go for lithium ion, but depending on the age of the kids, that might be a bad idea and they complicate the charge circuit, so NiMH is probably your best bet. If you're driving LEDs with resistors, the resistors temperature coefficient is used to prevent thermal runaway, so the resistor should likely drop between 1/4 and 1/3 of your voltage, but 9V is much much more than you need. 3 AA alkalines or 4 AA NiMHs are probably good.
– K H
Jan 17, 2019 at 1:44
• @KH Thanks for the note about the batteries. I'm actually using a rechargeable 9V NiMH battery which actually provides ~8.5V when fully charged. I don't have a battery-pack for AAs handy so I'm using the 9V as a convenient stand-in. When no longer balancing things in my hands as a proof-of-concept, I'll swap-out the 9V for something which provides closer to 5V and lasts longer (likely 4x 1.2V NiMH). Jan 17, 2019 at 2:49
• Makes sense. You can also add a voltage regulator to the circuit if you wish, but it would have to be a switching regulator to improve your battery life. The best way to power LEDs is with a current regulator, but that probably won't be as useful to teach.
– K H
Jan 17, 2019 at 3:01
• What is the voltage range of LEDs you're using and at what voltage do you consider your battery depleted?
– K H
Jan 17, 2019 at 3:03

## 3 Answers

You have wimpy GPIO. You want 250mA, and you want (trust me on this) to saturate your output transistor. A 2N4401 needs a base current to collector current ratio of 1:10 in saturation. So you need 25mA at the base of your output transistor. This is more than your wimpy GPIO supplies. So you need another transistor.

Q1 is an emitter follower. It's emitter voltage will be 0V when the GPIO pin is low, and 0.7V below the GPIO pin voltage when the pin is high. Assuming that the supply voltage to the microprocessor is 5V, the base of Q1 will be 4.3V when the GPIO pin is high. The base of Q2 will be at about 0.7V when Q2 is on. This leaves 3.6V to generate current across R1.

You need 30mA at R1 (that's 25mA rounded up). (3.6V)/(30mA) = 120$$\\Omega\$$ (note that the units work out -- it's nice when that happens). As long as your Arduino is a 5V unit, you're fine. Change R1 to 47$$\\Omega\$$ if it's a 3.3V one.

LEDs do not like to be driven from a voltage source -- you need to start with the voltage you're supplying, and work out how to give them the specified current. Your LEDs drop around 3.5V (check that, I'm going from memory and I'm lazy). Assuming you're driving this string from a 9V source, you can run pairs in series with one current-limit resistor -- that's the R2 I'm showing in the diagram.

If you have $$\N\$$ LEDs in a string (two if it's 9V, one if it's 5V), then $$R_2 = \frac{V_{cc} - N V_{led}}{I_{led}}$$ In the case that you use 9V and you want 20mA, that's R = (9V - 2 * 3.5V) / (20mA) = (2V) / (20mA) = 100$$\\Omega\$$.

Note that you probably don't want to get your 9V from a "9V" battery. A "9V" battery is 9V, fully charged with a light load, and you don't want to pull more than about 50mA from it. If you are going to pull 250mA (or even 125mA), then you should design your circuit to still work at around 6V -- which means just one LED per section, which means twice the current, you can't win for losing, etc.

So -- use a 9V wall-wart and be happy.

simulate this circuit – Schematic created using CircuitLab

• So the GPIO pin from the Arduino is connected to the base pin for the transistor, but how do I wire the whole circuit back to the Ardiuno's ground? And where is the "external" power connected to the circuit? I'm still too dumb to be able to read the circuit diagram you have, there, unfortunately. Jan 16, 2019 at 22:12
• The little triangle at the bottom means "ground". So the emitter of Q2, the ground of your external power supply, and the Arduino ground all get connected together. Connect the + terminal of the external supply to the collector of Q1 (the net labeled "See Text"). If it can supply enough current you could use the same power supply for the lights and the Arduino. Jan 16, 2019 at 23:21
• Okay, that's the thing that I was both assuming would work but also a little worried about. To me, it was clear that the only way to complete the circuit with the GPIO pin was to ground everything together, but I was a little worried that I might end up dumping some of the current from the external power supply back into the GPIO. Jan 17, 2019 at 2:07
• My current circuit has 6 total LEDs in 3 separate "strings". Each LED is coupled with a resistor, and each of the LED+R pairs is wired in parallel between the + external source and the collector pin of a transistor (so, 3 transistors). Each of the 3 GPIO pins are wired to the base pin of one transistor (each). All emitter pins are wired together with the "shared" ground of both the - external source and the GPIO ground on the Arduino. The Arduino has a 9V battery powering it, and the "external source" is another 9V battery (too much, I know). All lights are bright and no magic smoke. Jan 17, 2019 at 2:12
• So it sounds like the next thing I need to do is set up another loop that separates the GPIO from the transistors by introducing another transistor in between GPIO/existing-transistor. I'm starting to run out of space on my 30-column breadboard :) Jan 17, 2019 at 2:15

simulate this circuit – Schematic created using CircuitLab

The resistors R1 to R10 are current limiting resistors, for each led from data sheet the forward current is 20mA @ 3.1V forward voltage. from this formula

where Vs is source voltage, Vf is led forward voltage and i is current. we get R nearly equal to 100 (nearest standard value). in parallel led circuit you need to add separate resistor for each led branch, since even though they are with same specifications, but their forward voltages are slight difference and results in some leds lit bright and other dull.. even some times they will burn out. so the best practice is addingindividual resistor in each branch (for individual led)

regarding transistor base calculations

total no of leds: 10 each led consumption : 20mA

so total current is : 20*10 = 200mA is the collector current.

the current gain factor of 2N4401 is 40 @ 500mA (since even though it is 100 @ 150mA, but we crossed the limit consider it as 40)

Ic = hfe * Ib (collector current = gain * base current)

so Ib = 200/40 = 5mA ( this is minimum required base current to get 200mA of collector current).

NOTE: Arduino GPIO can deliver up to 40mA. so you can go for this .If you want to control this even with less base current (based on your question with 4mA, you need an extra transistor in darlington pair configuration

now the base is driven via arduino GPIO (5V) effective voltage across resistor is = 5V - Vbe (base emitter voltage) = 5V - 0.7V = 4.25V

So Ib = V base resistor / resistance => resistance = V base resistor / Ib => 4.25/0.005 => 850ohm so the nearest value with some margin factor will be 750ohm / 820ohm

• 4mA at the base won't reliably drive the transistor into saturation, though -- it'll probably get the collector voltage down to less than 1V, but more than 0.2V. Jan 16, 2019 at 23:22
• @TimWescott So the problem with a "single transistor" approach is that the base pin needs enough amperage relative to the collector's amperage to actually trigger? Jan 17, 2019 at 2:37

This is the simple Johnson counter method using 10 Cd ~ 20 Cd 5mm 5000k LEDs using only 5mA per LED which is really bright with these ones.

With 30 more resistors you can fade the leading a trailing LED to half intensity

A smoother effect uses resistors and transistors like this.

The 50 Ohm resistor controls the brightness and average current and can be paired to 25 Ohms.

Transistor emitter follower approach

Assuming 5V GPIO

simulate this circuit – Schematic created using CircuitLab

If I assumed wrong and it is a 3.3GPIO, then another design is needed. UPDATE aha 3V argh..

So now we need a low voltage inverting open drain HEX switch in DIP chip. http://www.ti.com/lit/ds/symlink/cd74ac05.pdf

With Vcc=3V to 5V the switch resistance < 20 Ohms. which inverts all inputs and the LEDs to common ANODE to 5.0V with no 50 ohm shared resistor now using the open drain resistance.

more calculations needed.

• I appreciate the "better circuit" design, here, but I did want to understand how to use GPIO to trigger the transistor-switched LED string. Jan 17, 2019 at 2:20