An RGB LED is really three LEDs (red, green, blue) in one package. By controlling each color's brightness independently with PWM, you can mix any color the LED is capable of — the same principle behind every smart bulb's color picker.
Beginner
⌛ 20 min
🔧 What You'll Need
- Arduino Uno
- 1x common-cathode RGB LED
- 3x 220Ω resistors
- Breadboard
- 5x jumper wires
Wiring the Circuit
For a common-cathode RGB LED (the more common type for hobby projects), connect the longest leg to GND. Connect the red, green, and blue legs through their own 220Ω resistors to PWM pins 9, 10, and 11 respectively. If your LED is common-anode instead, the wiring and the code's HIGH/LOW logic both invert — check the datasheet if you're unsure which type you have.
The Code
const int redPin = 9;
const int greenPin = 10;
const int bluePin = 11;
void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void setColor(int r, int g, int b) {
analogWrite(redPin, r);
analogWrite(greenPin, g);
analogWrite(bluePin, b);
}
void loop() {
setColor(255, 0, 0); // red
delay(1000);
setColor(0, 255, 0); // green
delay(1000);
setColor(0, 0, 255); // blue
delay(1000);
setColor(255, 0, 255); // purple
delay(1000);
}
Each color channel is just an LED being dimmed with PWM, exactly like the potentiometer tutorial's brightness control — only now there are three running simultaneously. The setColor() function is a small helper that sets all three channels in one call, and mixing values (like 255, 0, 255 for purple) blends the colors the same way a screen's pixels do.
💡 Tip: For genuinely accurate colors, an RGB LED's three internal LEDs often aren't equally bright at the same PWM value — if your "white" looks blueish, try lowering the blue channel's value slightly to compensate.
Troubleshooting
- Only one color ever shows, no matter the values: One or two resistors likely aren't making contact — check each color channel's connection individually.
- Colors look inverted (LED is brightest at 0): You have a common-anode LED, not common-cathode — either rewire for common-cathode or invert every value (255 - x) in the code.
- LED stays dim even at 255: Confirm you're using PWM-capable pins (marked with
~), not regular digital pins.
You've now controlled color, not just brightness or position — useful for status indicators, mood lighting projects, or as a component inside a larger build. Next: adding sound instead of light.
Menu