Home › Wiring › RGB LED with Arduino Uno

RGB LED with Arduino Uno

How to connect an RGB LED to an Arduino Uno: the wiring, a sketch that works, and a simulator to try it in your browser before you build it.

RGB LED wired to an Arduino Uno in Zen Circuit Lab

5 mm RGB LED · common cathode · red 1.9 V, green 2.1 V (pure green ≈ 3 V in many parts), blue 3.0 V · 20 mA per colour

Wiring

RGB LED pinArduino Uno
RD9 ~ (through a 220 Ω resistor)
Common − (longest)GND
GD10 ~ (through a 220 Ω resistor)
BD11 ~ (through a 220 Ω resistor)

Run it in the labFree, no account needed. The same wiring, with the sketch already on the board.

Code

From the project Arduino RGB colour mixer: three knobs, one RGB LED. It also compiles in the Arduino IDE with the libraries it includes.

// RGB colour mixer: three pots (A0, A1, A2) set red, green and blue on a common-cathode RGB LED (D9, D10, D11, PWM).
const int R_PIN = 9, G_PIN = 10, B_PIN = 11;
unsigned long shownAt = 0;

void setup() {
  Serial.begin(9600);
  pinMode(R_PIN, OUTPUT); pinMode(G_PIN, OUTPUT); pinMode(B_PIN, OUTPUT);
}

void loop() {
  int r = analogRead(A0) / 4, g = analogRead(A1) / 4, b = analogRead(A2) / 4;   // 0..1023 to 0..255
  analogWrite(R_PIN, r); analogWrite(G_PIN, g); analogWrite(B_PIN, b);
  if (millis() - shownAt >= 1000) { shownAt = millis(); Serial.print("R "); Serial.print(r); Serial.print("  G "); Serial.print(g); Serial.print("  B "); Serial.println(b); }
  delay(20);
}

For teachers

The project runs in the browser on a laptop or a phone, so every student can build and test it before the class touches a real board. Assign it on the teacher's page and set what counts as done: a prediction, a hidden fault found, or a passing test sheet.

Related