# Driving LEDs directly from microcontroller pins

In the circuit I'm designing, I'm using an at91sam7s128 which is a micro-controller with an ARM7 core.

1. Its PIO0 to PIO31 provide 8mA max which is low to handle direct driving of LEDs like this:

simulate this circuit – Schematic created using CircuitLab

UPDATED: thanks to jippie and Olin Lathrop for telling me about the resistor in series with the LED.

1. So some designers use a safer method, connecting PIO to the base of a transistor that acts like a switch:

simulate this circuit

1. Others just make a simple trick. They connect the anode of the LED to the power supply and the cathode to a PIO:

simulate this circuit

In the last method the LED will turn on when the PIO goes low - acting like GND - and current will be taken from power supply, not from the PIO like in the first method. So do I have to take care of maximum current of the PIO, although it just acts like GND, not like a power supply (like in the first method).

When I set the PIO low, does the pin really get connected to GND or something else?

NOTE: In this Atmel evaluation kit they use the last method, but I need to make sure.

• Did you try what an LED looks like at 8mA? A modern LED may have a 20mA maximum rating, but they are extremely bright when you just want to use them as a signaling indicator. Jun 2, 2013 at 11:58
• (1) 8mA is plenty for most LEDs unless you need to see it in full sunlight (2) the second circuit shown has no current limiting resistor - 9V will blow the LED, the transistor, or both, and flatten the battery rather fast too! (3) the third circuit is not fundamentally different from the first.
– user16324
Jun 2, 2013 at 12:09
• So do I must take care of maximum current of PIO although it just act like GND not like a power supply -like method 1 Yes, you do. The 1st and 3rd methods are equivalent in modern day microcontrollers. Jun 2, 2013 at 12:15
• @jippie I don't want to try with this valuable part , I fear this would burn-out one of PIO . Jun 2, 2013 at 14:01
• @yahyatawil because most microcontrollers can source AND sink the same amount of current on each GPIO. They are functionally the same thing. Only difference is your code, where turning on the led is setting the pin high (when it is the source) or low (when it acts like the ground). Jun 2, 2013 at 19:13

The first and third methods you show are fine, assuming the resistor is sized so that the 8 mA maximum port pin current is not exceeded. The difference between these two methods is whether the low or high side drive transistor in the processor is in series with the LED. Sometimes the low side transistors are a little more beefy, which is why you see method three more than method 1. However, either method is fine as long as you take care to not exceed the current spec for the pin. Also note that some processors have a total current spec that must not be exceeded. For example, you could have 10 pins each driving a LED at 8 mA, which would be fine individually for each pin. However, if the overall processor spec is that all I/O pins together can't source our sink more than 50 mA, for example, then you're still out of spec.

Your method two is wrong as drawn. It will likely damage the LED or transistor. To fix it, add a resistor in series with the LED so that the LED current does not exceed its maximum rated value. For example, let's say the LED is rated for 20 mA max, and it drops 2.1 V in that case. With a 9 V supply, that leaves 6.9 V accross the resistor and the transistor. The saturation voltage of the transistor is probably around 200 mV, but for worst case analisys let's just say it's 0. 6.9 V / 20 mA = 345 Ω, so the next size up common value of 360 Ω would be fine. Working backwards, and this time assuming the 200 mV drop on Q1, the LED current will be 6.7 V / 360 Ω = 18.6 mA. Even in a side by side comparison, it will be difficult for humans to distinguish that from the brightness achieved by 20 mA thru the LED.

Here is yet another method, which is useful when there is a external supply available that is a bit more than the processor voltage plus the LED drop:

This uses the transistor as a switchable current sink, so the exact power voltage (9 V in your case) doesn't matter over a wide range. When the base is 0, the transistor is off and the LED is off. When the base is driven to 3.3 V by the digital output, the emitter will be about 700 mV less, or 2.6 V in this example. The current thru R1 will therefore be 2.6 V / 150 Ω = 17.3 mA. Since most of this current comes from the collector due to the gain of the transistor, that will be the LED current to a reasonable approximation.

Note that the 9 V supply voltage didn't enter into the calculations. As long as it is above a minimum value, the transistor will sink very nearly the same current because the transistor gain changes little with collector voltage. If we give the transistor about 700 mV C-E voltage so that it is nicely in its linear region, then the supply must be at least the digital output level plus the LED drop, which comes to 5.4 V in this case. That means the "9 V" supply can vary down to about 5.4 V without effecting the operation of this circuit.

The maximum allowable supply is limited by the power dissipation in the transistor. Let's say we want to keep it to 150 mW or less. 150 mW / 17.3 mA = 8.7 V, which is the maximum C-E voltage for the transistor when it is on. Since R1 will drop 2.6 V and the LED 2.1, that means the supply can't exceed 13.4 V. So this circuit will work nicely and run the LED at the same brightness even if the "9 V" supply ranges from about 5.4 to 13.4 volts.

• thanks ,I could now understand why 1st and 3rd methods are same some who . I didn't know that there is a transistor which control the High/Low logic of pins . I missed a resistor as you said in schematic of 2nd method I will edit it now . thanks ! Jun 2, 2013 at 14:28
• Methods 1 and 3 a subtly different. In case 1, the microcontroller sources current, and in 3 it sinks current. In this case, it makes no difference, but its just something to keep in the back of your mind Jun 2, 2013 at 14:41
• I found this resource taking about modes of input/output of micro-controller GPIO includes the transistors you mentioned . coactionos.com/embedded-design/98-gpio-output-pin-modes.html Jun 2, 2013 at 14:44

I don't want to try with this valuable part , I fear this would burn-out one of PIO

I'd suggest instead using 3 pins and driving a shift register, let it handle the current for the LEDs. Part like cd74AC164

https://www.digikey.com/product-detail/en/texas-instruments/CD74AC164E/296-4235-5-ND/375630