Electronic – How to use a transistor to drive strings of LEDs from GPIO pins

gpioledtransistors

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. 🙂

Best Answer

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.

schematic

simulate this circuit – Schematic created using CircuitLab