How-to · Beginner · 3 min read
RGB LEDs
Common Anode vs Common Cathode, Resistors and Colour Mixing
By Geekobot Learning Team · Updated 24 Oct 2026
On this page
An RGB LED is three LEDs (red, green, blue) in one package with a shared fourth pin, the longest leg. On a common-cathode LED that pin goes to ground and you drive each colour HIGH; on a common-anode LED it goes to the supply and you drive each colour LOW. Give each colour its own resistor on a PWM pin, and mix any colour by setting the three duty cycles.
Key specs at a glance
- Pins
- 4: R, common, G, B (longest leg is common)
- Forward voltage
- Red about 2.0 V; green and blue about 3.0 to 3.2 V
- Current
- 20 mA per colour maximum for 5 mm LEDs; 5 to 10 mA is plenty
- Resistors at 5 V
- About 150 Ω red, 100 Ω green and blue for 20 mA; 220 to 330 Ω for 10 mA
- Control
- Three PWM pins
- Common anode
- Logic inverted: 0 = full on, 255 = off
Common anode or common cathode? How to tell
Look for the longest leg; it is the common pin. Connect it to GND, then touch one of the other legs to 3.3 V or 5 V through a 220 Ω resistor. If a colour lights, it is common cathode. If not, connect the long leg to the supply and touch the other legs to GND through the resistor: if it lights now, it is common anode. A multimeter's diode test does the same job.
Choosing resistor values
Each colour needs its own resistor because red has a lower forward voltage than green and blue. Using Ohm's law, R = (Vsupply - Vf) / I:
(5 - 2.0) / 0.010 = 300 Ω, so use 330 Ω.
(5 - 3.1) / 0.010 = 190 Ω, so use 220 Ω.
blue and green have only about 0.2 V of headroom, so use 33 to 68 Ω and expect dimmer blue, or use a 5 V supply with transistors.
Many tutorials use 220 Ω on all three; it works, but the colours will not be balanced. Fine-tune white balance in code instead. The LED resistor calculator does the arithmetic.
Arduino code for colour mixing
Use three PWM pins (marked ~ on an Uno).
const int R = 9, G = 10, B = 11;
const bool COMMON_ANODE = false; // set true for common-anode LEDs
void setColor(int r, int g, int b) {
if (COMMON_ANODE) { r = 255 - r; g = 255 - g; b = 255 - b; }
analogWrite(R, r);
analogWrite(G, g);
analogWrite(B, b);
}
void setup() {
pinMode(R, OUTPUT); pinMode(G, OUTPUT); pinMode(B, OUTPUT);
}
void loop() {
setColor(255, 0, 0); delay(700); // red
setColor(0, 255, 0); delay(700); // green
setColor(0, 0, 255); delay(700); // blue
setColor(255, 120, 0); delay(700); // orange
setColor(128, 0, 255); delay(700); // purple
}ESP32 and Pico notes
On the ESP32 with Arduino core 3.x, analogWrite() works on most output pins; on older cores, use ledcAttach(pin, 5000, 8) and ledcWrite(pin, value). On the Pico, any GPIO can do PWM. Both are 3.3 V, so pick small resistors for green and blue (see above). For brighter or larger RGB LEDs and strips, switch each colour with a logic-level MOSFET; see transistors and MOSFETs and PWM.
Plain RGB LED or WS2812 addressable LED?
A plain RGB LED needs three PWM pins and three resistors per LED, so it suits one or two status lights. A WS2812B has a controller chip inside: hundreds can be chained on one data pin, each set to any colour. For anything more than two or three colour LEDs, addressable LEDs are simpler.
Try it: LED resistor calculator
Change the values and the diagram and results update as you type.
Common mistakes to avoid
- Using one shared resistor on the common pin, so colours change brightness as others turn on.
- Forgetting to invert values for common-anode LEDs.
- Using non-PWM pins and getting only on or off.
- Driving green and blue at 3.3 V with 220 Ω and wondering why they are dim.
- Looking straight into a bright LED at full current.
Frequently asked questions
How do I know if my RGB LED is common anode or common cathode?
Connect the longest leg to ground and touch another leg to 5 V through a 220 Ω resistor. If it lights, it is common cathode; if not, it is probably common anode.
What resistor should I use for an RGB LED?
At 5 V, about 330 Ω for red and 220 Ω for green and blue gives roughly 10 mA each. Use one resistor per colour.
How do I make white with an RGB LED?
Turn all three colours on. Because the colours have different brightness, you may need to reduce red or green in code to get a neutral white.
Can I connect an RGB LED without resistors?
No. Without resistors the current is limited only by the pin, which can damage both the LED and the microcontroller.
Why does my RGB LED show the wrong colours?
The pin order differs between makers. Check which leg is which colour by testing each one, then change the pin numbers in code.
Parts for this guide
Calculators
Terms in this guide
Projects that use it
Put this into practice: 367 step-by-step builds in our project library use parts from this guide.
Keep learning
Sources
- Typical 5 mm RGB LED datasheets (forward voltage and current ratings)
- Arduino analogWrite and ESP32 LEDC documentation
Written by the Geekobot team from manufacturer datasheets. Always check the datasheet for the exact part and variant you buy.




