Board and part

Arduino Uno RGB LED Simulator: Wiring, Code and a Live Circuit

This is a common cathode RGB LED wired to an Arduino Uno through three 220 ohm resistors and stepping through red, green, blue and white live in your browser.

  • 5 V logic
  • No library: analogWrite()
  • Colors on 9, 10, 11
  • Common cathode, 3 x 220 ohm
Arduino Uno · RGB LEDlive0.000 s 0.00x
Click to open it in the editor
The circuit itself, running on the simulator with the sketch below. Press what can be pressed; click anything else to open it in the editor.
Connections

Wiring the RGB LED to the Arduino Uno

The three color pins go to pin 9, pin 10 and pin 11, all of which can do PWM, and the long leg goes to ground.

Every connection in the circuit above, with the board’s own pin names.

RGB LED pinArduino Uno pinWhy
R (red)pin 9The red anode, through its own 220 ohm resistor. The pin must be able to do PWM.
G (green)pin 10The green anode, through its own resistor.
B (blue)pin 11The blue anode, through its own resistor.
C (common cathode)GNDThe longest leg joins all three cathodes and goes to ground.
Three 220 ohm resistorsOne in each color leadEach color is its own LED and needs its own resistor.

Power

Each color is a separate LED behind its own 220 ohm resistor, so each pin drives only a few milliamps at 5 V. This is a common cathode part: HIGH lights a color. A common anode part has its long leg on 5V and lights on LOW, so analogWrite values must be inverted.

Pins to leave alone on the Arduino Uno

Pins 0 and 1 are the USB serial port on a real Uno, so anything wired there fights the upload and the serial monitor. Pin 13 carries the onboard LED.

On a real bench

On a real bench the Uno stands beside the breadboard: the RGB LED goes into the breadboard and male-to-male jumpers run from the Uno’s female headers to its rows. Power the Uno from USB while you build, and unplug it before you move a wire.

The sketch

The code, and what it does.

The sketch the circuit above runs. Open it in the editor, change a line and press Run: it compiles in your browser.

RGB LED on Arduino Uno pin 9, pin 10, pin 11
const int RED_PIN = 9;     // pin 9
const int GREEN_PIN = 10;   // pin 10
const int BLUE_PIN = 11;    // pin 11

void setColor(int r, int g, int b, const char* name) {
  analogWrite(RED_PIN, r);
  analogWrite(GREEN_PIN, g);
  analogWrite(BLUE_PIN, b);
  Serial.println(name);
  delay(400);
}

void setup() {
  Serial.begin(9600);
  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
}

void loop() {
  setColor(255, 0, 0, "Red");
  setColor(0, 255, 0, "Green");
  setColor(0, 0, 255, "Blue");
  setColor(255, 255, 255, "White");
}

analogWrite() sends a PWM signal whose duty is the value from 0 to 255, so each color can be set to any brightness and the three mix into any color. 255 on all three makes white, and 0 on all three turns the LED off.

setColor() writes the three duties, prints the color’s name and waits 400 ms, so the loop steps red, green, blue and white about every two seconds.

Serial.begin(9600) opens the serial monitor. Serial goes over the USB cable, and the serial monitor in the editor reads it at whatever baud rate the sketch sets.

Pitfalls

Common mistakes with the RGB LED on the Arduino Uno

Using one resistor for all three colors

The three LEDs have different voltage drops, so on one shared resistor the color that drops least takes most of the current and the mix looks wrong. Give each color its own resistor.

Mixing up common cathode and common anode

A common cathode part has its long leg on ground and lights when a pin goes HIGH. A common anode part has its long leg on 5V and lights when a pin goes LOW. With the wrong type everything looks inverted: white when you wanted off.

Choosing pins without PWM

analogWrite() only makes a smooth brightness on pins with PWM hardware. On a pin without it the color is either fully on or fully off. On the Arduino Uno these three pins are PWM pins.

FAQ

Questions and answers

How do I connect an RGB LED to an Arduino Uno?

Put a 220 ohm resistor in each color lead, wire them to pin 9, pin 10 and pin 11, and join the longest leg to GND for a common cathode part. The sketch then sets each color with analogWrite().

How do I tell a common cathode from a common anode LED?

Connect the long leg to ground and a color leg to 5 V through a resistor. If the color lights, it is common cathode. If nothing lights, try the long leg on power and the color leg on ground.

How do I make other colors?

Mix the three duties: setColor(255, 128, 0, ...) is orange and setColor(128, 0, 255, ...) is violet. The eye does not see brightness in a straight line, so a gamma table gives smoother fades.

Why is the white tinted?

The three LEDs are not equally bright, so equal duties are not white. Lower the strongest color, usually green or blue, until the mix looks right.

Build your own RGB LED project

Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.