CYTHEIA AIOpen the editor

RGB Mood Light

RGB LED cycling through colors using PWM.

Open this circuit and run it

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

Wiring

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

Open this circuit and run it

Next challenge

More projects in LED & Lighting