# Choosing a resistor value for a microcontroller controlling multiple LEDs

I have circuit controlled by a PIC16f18076 microcontroller that simply powers and turns on some LEDs. I am having a lot of difficulty figuring out how I can prevent my circuit from frying any of my components.

The schematic below is a mockup, in reality the microcontroller has 22 output pins connected to LEDs:

simulate this circuit – Schematic created using CircuitLab

I want to know how to calculate what the value for the resistors should be.

• My PIC16f18076 microcontroller can sustain a maximum 250mA on VDD
• Each of my individual LEDs have currents of 20 mA and a forward voltage of 3.0-3.2V
• Ideally at any time 1-7 pairs of LEDs may be on. If 7 pairs is too many please explain.
• The maximum current on any I/O pin is 25mA and the output voltage=VDD

I'm hoping someone could explain how to correctly do the math on this.

EDIT: Original schematic was wrong please refer to the current one

• You need to know the MCU GPIO output characteristics (voltage and current). Your MCU board may have a 3.3V regulator and that is what the MCU runs on. You shouldn't need anywhere near 20 mA to get the brightness you want. Try 2-5 mA. Different color LEDs have different voltage drops. Jan 31, 2023 at 17:47
• @Mattman944 I edited to include a lot of the missing info you suggested, additionally all my LEDs are the same color from the same distributor, finally I can't find anything for LED current that says anything but 20 mA Jan 31, 2023 at 17:52
• Which MCU it is? Likely you quoted the absolute maximum ratings. Likely the MCU is not intended to be used above typical ratings that are near absolute maximum ratings. And you can't light up two LEDs in series with a 5V MCU if two LEDS require 6V. But that's for 20mA, so if you tell which LED it is then you can look at datasheet how much voltage it needs at say 5 or 10mA. Oh and the MCU output won't be 5V either if you draw 20/25mA from it. Jan 31, 2023 at 18:01
• What is the function of the LEDs? The 20mA figure you quote is commonly the maximum current - not the current you need to act as an indicator. Something in the 1mA to 5mA region is usually adequate with modern high-efficiency LEDs. Jan 31, 2023 at 18:16
• Please add info which (a) MCU it is and which (b) LED it is. Preferably, add links to data sheets. And also the purpose of LEDs, because LEDs are rated ad 20mA, but it does not mean you want to use them at 20mA if they are too bright, unless they must be as bright as you possibly want, in which case you might want to either change to a better LED or add transistors to drive more current. Or one LED driver chip so you don't need handful of components between LEDs and MCU. Jan 31, 2023 at 18:28

XY problem.

If your intention is to drive each LED at its full rated current, you will not be able to do so - at least satisfactory. 22 LEDs x 20mA will be 440mA, more than your PIC can sustain.

Then there's the pin current. Look at the data sheet. The drive figure given for a pin is specified at a maximum allowed IR drop that still ensures a valid logic level at a specified current. What does the data sheet say about that? I bet it's a lot less than 40mA. More like 10 or 5mA (datasheet D370 list 6mA.) In essence, the pin has its own series resistance when it's on (Rds(on)) that will limit the current the pin can deliver.

Even if pin drive isn't a problem (it is), you say that you will only have 7 pairs on at a time - that’s 140mA, assuming the pin Rds(on) didn't limit you. Still a lot.

But what if your software malfunctions and lights all of them? Your PIC will be very unhappy, shedding all that heat in its I/O drivers.

In short, it’s a vulnerable design.

You have two choices:

• Reduce your LED currents to below the pin max (3mA each LED)

The first option implies series resistance of 680 ohms or greater, limiting total LED current to 66mA. Very reasonable. You can choose ‘high efficiency’ LEDs and get useful brightness with currents as low as 1 - 5mA. In my experience with making front panels for set-top boxes, 3mA was plenty with an appropriate LED.

If you really need the full 20mA brightness, the second option is the best choice. Further, if you use low-side switching (NPN bipolar or n-FET) you can increase your LED supply voltage if you need to. Then you can run your micro on 3.3V or even 1.8V and save power, assuming those supplies are available.

• How did you get the number 660? Jan 31, 2023 at 22:11
• R = 2V/.003A. So 667 ohm. 680 ohm is nearest 5% value. Jan 31, 2023 at 23:37

The current values you quote for the PIC MCU are the Absolute Maximum Ratings.

It means that when these ratings are exceeded, there may be permanent damage.

It also does not mean that the PIC MCU will properly work up to these values.

If you look at the actual characteristics of the IO pins, you will see that the operation is guaranteed that sinking 10mA has at most 0.6V on pin, and sourcing 6mA has max 0.7V drop from supply.

So if you want to source current into LEDs, you have max 6mA available per pin, and thus 3mA per LED as you want to run them in pairs. For that you can calculate the resistor value, assume that a 3V LED so 2V left over the resistor, and current is 3mA per resistor.

So 7 pairs is 42mA. The MCU should handle that. But if you sinked current, you'd have 10mA per pin available, 5mA per LED. 70mA for 7 pairs.

In case you are thinking why there is so little current available, MCUs are just not intended to drive loads directly. You would generally drive more than a few indicator LEDs at low enough current so it does not load the MCU too much. For any loads, you would drive them through e.g. transistors.

• Very thorough, thanks a ton for working through this! Jan 31, 2023 at 20:47
• I was curious and just set one of my microcontroller pins to stay high and attached two LEDs in parallel using only two 20 ohm resistors and its stayed on for several minutes, how can this be explained? Jan 31, 2023 at 22:28
• @KidWithComputer Explain what? What you expected to happen and why? What currents and voltages you measured (or assumed due to lack of multimeter) that will help us explain something to you? Jan 31, 2023 at 23:57
• I was led to believe that without a really high resistor either the leds or my microcontroller would fry Feb 1, 2023 at 4:55
• @KidWithComputer There are too many unknowns here. You may not have even got 20mA out from MCU to LED and exceeded the absolute maximum ratings, because the MCU IO pin may have output impedance up to 115 ohms. Even if you did exceed the absolute maximum for IO pin and LED, if you exceeded it only by little, there may only be little damage to IO pin and LED. They may have aged worth of 20 years of normal use in 20 minutes, but still work. Please note that the guaranteed safe current ratings from data sheet are for valid logic level voltages, so they likely can be exceeded somewhat. Feb 1, 2023 at 5:37

If the microcontroller can source a decent current as well as sink it, you can usefully connect two LEDs to one pin if they

• never both need to be on at any given time
• are "dark enough" connected in series to the given supply voltage
• never need to be off at the same time, unless you can put that pin in a high impedance state.
(3rd of three-state. Configuring as input doesn't quite cut it:
any voltage that leaves both LEDs off is probably illegal at an input.)

simulate this circuit – Schematic created using CircuitLab

The resistor needs to be about $$\\frac {(V_{LEDs} - min(V_F))}{min(I_F, I_{out} )}\$$.

7 * 20 mA * 2 = 280 ma, more than the quoted limit on total current:

(You can use two resistors for different currents/forward voltages.)

• I'm not sure I follow, what good are LEDs that are never on at any given time? I'm sorry if I should've stated it but I want them to turn on Jan 31, 2023 at 19:15
• I failed to successfully avoid simultaneously. Jan 31, 2023 at 19:45
• Why is this a wiki?
– pipe
Jan 31, 2023 at 20:05
• @pipe: I made this post editable without approval so just about everyone can try and improve it. (I still wince seeing such called wiki.) Jan 31, 2023 at 20:13
• Everyone can still improve it, and this answer is not so much more special than any other answer on the site that it needs special handling. The wiki is a thing of the past that's only used to avoid reputation dumps now.
– pipe
Jan 31, 2023 at 20:43