This is my first time doing this, so forgive me for what seems like a very basic thing, I have read through both these articles, and I'm still not clear on the exact math to do.



First off, using this image:


I think I have worked out that I should be doing this:

RPi pin 7 (GPIO 4)   -> RGB LED Common Anode
RPi pin 11 (GPIO 17) -> RGB LED Red -> Resistor -> Ground wire
RPi pin 13 (GPIO 21) -> RGB LED Green -> Resistor -> Ground wire
RPi pin 15 (GPIO 22) -> RGB LED Blue -> Resistor -> Ground wire
RPi pin 6 (Ground)   <- Ground wire

Given this RGB LED (Common Anode):


It seems the Vf is:

Red: 2V 20mA
Green: 3.5V 20mA
Blue: 3.5V 20mA

And I'm trying to calculate what resistor I should be using.. My first question is... are the pins on the GPIO 3.3V or 5V?

The main point I'm trying to wrap my brain around is if I'm supposed to subtract the Vf of the LED from the volts provided. Example:

Assuming the pin output is 3.3V, the Red is Vf of 2V with a current of 20mA, how would I calculate the correct resistance?

3.3V / 0.02A = 165ohms ??
(3.3V - 2.0V) / 0.02A = 65ohms ??

Or, given a 5V GPIO

5.0V / 0.02A = 250ohms ??
(5.0V - 2.0V) / 0.02A = 150ohms ??

Furthermore, with the Green and Blue.. if they have a 3.5 Vf, and the pin output is only 3.3V, do I need a resistor at all?

  • Or maybe it depends on the pins? It would make sense from the picture that 7/11/13/15 are all 3.3V and 12/16/18/22 are all 5V?
    – slf
    Aug 19, 2013 at 21:11

4 Answers 4


The GPIO pins will be damaged by 20mA. You need a transistor or driver IC to drive the LED at full brightness.

A resistor will always cause a voltage drop, so the green and blue will only light dimly from 3.3V

To calculate the resistor, you need to understand that the current through the resistor will be the same as the current through the LED.

So if the LED current is 20mA and the forward voltage is 3.5V. With a 5V supply you would have 1.5V across the resistor (at 20mA)

Ohms law says V = I * R substituting gives

1.5 = 0.02 * R

solve for R

R = 1.5 / 0.02
R = 75ohm

For the red led, you have 2V across the LED and 3V across the resistor (5V supply)

R = 3 / 0.02
R = 150ohm

Those are the minimum values you should use for the resistors

  • Thanks! could you give more details on the transistor calculations?
    – slf
    Aug 22, 2013 at 13:14

don't worry about the voltage, any LED will work with 3.3V as well as with 5V without any problems. just don't go over 30V, mostly for your own health and safety.

regarding the resistor value, unless you're trying to squeeze the maximum brightness out of your design, I'd suggest you'll go with 470ohm or 330ohm or 680ohm, whichever you have around. your LED will be quite visible with resistors going as high as 1-2kOhm, basically for prototyping work I'd use whatever resistor I have around, there's no practical reason to calculate the exact value.

1-2mA usually gives a reasonably bright response, and reduces the chance to stress your GPIO pins beyond their limits, because they are not supposed to source a lot of current.

and if you need maximum brightness, I'd suggest to use a transistor switch (search for "Darlington transistor" -- these are made exactly for controlling LED and similar things).

  • Yes, I am indeed trying to calculate maximum brightness. The LED is the main focus of the project, as it will function as a lamp.
    – slf
    Aug 19, 2013 at 23:32
  • 1
    to get the maximum brightness you should get the power from the 5V rail (USB), attach transistor to the GPIO, and limit the current using the resistor, calculated by R = (5V - Vf) / 0.02 A, that's about 75ohm or 100ohm depending on the color.
    – lenik
    Aug 20, 2013 at 2:01

So the answer to my question is actually that using a Common Anode is unnecessarily complicated for the Pi GPIO.

The better way is to use a Common Cathode, and do this:

RPi pin 11 (GPIO 17) -> Resistor -> RGB LED Red
RPi pin 13 (GPIO 21) -> Resistor -> RGB LED Green
RPi pin 15 (GPIO 22) -> Resistor -> RGB LED Blue
RPi pin 6 (Ground)   <- RGB Common Cathode

As far as calculating the resistor goes, you do need to subtract the forward voltage, but the forward current is not what I expected. The pi gpio pins output 3.3v 16mA. And, you need to use a RGB LED with a forward voltage on all pins something lower than 3.3v so it will actually light up.

Given this new LED:


The calculations for MINIMUM resistance needed:

So the algorithm for a single LED pin is

Min Resistor (ohms) = (3.3V - (Vf of the anode)) / 0.016A

in this case, for the red pin that is

82 ohms ~= (3.3V - 2.0V) / 0.016A

Which is confirmed by the links above.

The "best" way would be to use an external power source, and a transistor controled via the GPIO, but I'm lazy and won't be doing that.

Thank you #raspberrypi on freenode.


R = (Vpower supply - Vled) / Iled

where the Vled and Iled are nominal operating conditions for the LEDs and obviously running the LED and resistor in series.

You should in theory iterate this method however for most DIY situations im under the impressions this should work.

for example a power supply of 5V (standard on a pi) along with a Vled of 3.3V and a Iled of 20mA you would require 85 Ohms however the nearest standard resistors are 82 or 100, either of which should be satisfactory as there is +- 10% on most standard resistors.

so you are using the correct solution in the equation (the second one for each of your 3.3 and 5v examples) online calculators often go one step further and reccommend a standard reistor amount which is why they are often slightly different to your calculations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.