RGB Mood Light
RGB LED cycling through colors using PWM.
- Beginner
- LED & Lighting
- 3 parts
Opens in your browser with the parts, wiring and code already in place. Press Run to watch it work, then change anything you like.
How it works
An RGB LED is three LEDs (red, green and blue) in one package, sharing a ground. Pins 9, 10 and 11 drive the red, green and blue channels, and all three can do PWM: the pin is switched on and off so fast that the average level looks like any value from 0 (off) to 255 (full brightness). Mixing three brightness levels makes any colour. Real RGB LEDs need a resistor on each channel; this simulation leaves them out to keep the circuit simple.
loop() runs three fades one after another. In the first, red rises from 0 to 255 while green falls from 255 to 0, in steps of 10 milliseconds, so the light moves from green to red. The second fades green up and blue down, and the third fades blue up and red down. Together they glide through the colours of the rainbow and round again.
Parts
- Arduino Uno
- RGB LED
- GND
Wiring
- Arduino Uno D9 → RGB LED R (Red)
- Arduino Uno D10 → RGB LED G
- Arduino Uno D11 → RGB LED B
- RGB LED GND → GND
Arduino code
// RGB Mood Light
int r = 0, g = 0, b = 0;
void setup() {
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
Serial.begin(9600);
}
void loop() {
for (int i = 0; i < 256; i++) {
analogWrite(9, i);
analogWrite(10, 255 - i);
delay(10);
}
for (int i = 0; i < 256; i++) {
analogWrite(10, i);
analogWrite(11, 255 - i);
delay(10);
}
for (int i = 0; i < 256; i++) {
analogWrite(11, i);
analogWrite(9, 255 - i);
delay(10);
}
}
Try changing it
- Slow the colour change down: change delay(10) to delay(30) in all three loops.
- Pause on each pure colour: add delay(1000); after the end of each for loop.
Next challenge
More projects in LED & Lighting
- LED BlinkerClassic Arduino blink: LED on pin 13 with resistor to GND.
- Breadboard BlinkerThe same blinking LED, built the way you would on a real breadboard: parts pushed into…
- Traffic LightRed, yellow, green LEDs cycling like a traffic light.
- Potentiometer LED DimmerTurn the knob to control LED brightness with PWM.