PinoutSearch

RGB LED Color Mixing (Arduino)

Cycle an RGB LED through colors with PWM on three pins. Learn analogWrite and color mixing.

Beginner20 min

What you need

Wiring

Hover a pin or a wire to trace the connection.

Code

rgb_fade.ino
// Cycle an RGB LED through colors using PWM.
// Wiring below is for a COMMON-CATHODE LED. For common-anode, use 255 - value.
const int R = 9, G = 10, B = 11;  // PWM pins (~)

void setColor(int r, int g, int 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(500); // red
  setColor(0, 255, 0);   delay(500); // green
  setColor(0, 0, 255);   delay(500); // blue
  setColor(255, 255, 0); delay(500); // yellow
  setColor(0, 255, 255); delay(500); // cyan
  setColor(255, 0, 255); delay(500); // magenta
}

Steps

  1. Wire each color pin (R/G/B) from D9/D10/D11 through its own 220Ω resistor.
  2. Connect the common (longest) leg to GND for a common-cathode LED.
  3. Upload to cycle colors. If colors look inverted, you have a common-anode LED — use 255 - value.