Built-in project · Arduino Uno R3

RGB LED on pins 3, 5 and 6, on an Arduino Uno

A common-cathode RGB LED with red on pin 3, green on 5 and blue on 6, each through 220 ohm: red, green, blue, white and purple, a second each. The whole build, an Arduino Uno and 4 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 15 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $32. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Mixing colors with three PWM channels
  • Brightness with analogWrite from 0 to 255
  • A common cathode RGB LED
  • Which Uno pins can do PWM
  • Difficulty: Beginner
  • Time: About 15 minutes
  • Board: Arduino Uno R3
  • Parts: 6

How it works

An RGB LED is three small LEDs, red, green and blue, in one lens. Each has its own 220 ohm resistor and its own pin: red on pin 3, green on pin 5 and blue on pin 6. The shared cathode goes to GND.

analogWrite sets each color's brightness from 0 to 255 by switching the pin on and off very fast. Your eye blends the three into one color. Pins 3, 5 and 6 can all do PWM.

The loop shows red, green, blue, white and purple for one second each. Purple is mixed from 170 red and 255 blue. The serial monitor prints each color's name.

Parts list

6 parts, plus the jumper wires. They retail for about $32; on Mokxi they're free.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Resistor, 220 Ω (1) pin 1Arduino Uno R3 pin 6
Resistor, 220 Ω (2) pin 1Arduino Uno R3 pin 5
Resistor, 220 Ω (3) pin 1Arduino Uno R3 pin 3
RGB LED pin CArduino Uno R3 pin GND
Resistor, 220 Ω (3) pin 2RGB LED pin R
Resistor, 220 Ω (2) pin 2RGB LED pin G
Resistor, 220 Ω (1) pin 2RGB LED pin B

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// RGB LED: red, green, blue, white and purple, a second each.
//
//   pin 3 -> 220 ohm -> red leg
//   pin 5 -> 220 ohm -> green leg
//   pin 6 -> 220 ohm -> blue leg
//   the long leg (the common cathode) -> GND
//
// An RGB LED is three LEDs in one lens. analogWrite() sets each one's
// brightness from 0 to 255 by switching the pin on and off very fast (PWM),
// and the eye mixes the three into one color. Pins 3, 5 and 6 can all do PWM.

const int RED_PIN = 3;
const int GREEN_PIN = 5;
const int BLUE_PIN = 6;

void setColor(int red, int green, int blue);

void setup() {
  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: RGB LED, red 3, green 5, blue 6");
}

void setColor(int red, int green, int blue) {
  analogWrite(RED_PIN, red);
  analogWrite(GREEN_PIN, green);
  analogWrite(BLUE_PIN, blue);
}

void show(const char *name, int red, int green, int blue) {
  setColor(red, green, blue);
  Serial.println(name);
  delay(1000);
}

void loop() {
  show("red", 255, 0, 0);
  show("green", 0, 255, 0);
  show("blue", 0, 0, 255);
  show("white", 255, 255, 255);
  show("purple", 170, 0, 255);
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

It sets the three color pins as outputs and starts the serial port at 9600. It prints a line naming which pin is which color.

void setup() {
  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: RGB LED, red 3, green 5, blue 6");
}

setColor sketch.ino

It takes a red, green and blue level and writes each to its pin with analogWrite. Mixing these three numbers makes any color the LED can show.

void setColor(int red, int green, int blue) {
  analogWrite(RED_PIN, red);
  analogWrite(GREEN_PIN, green);
  analogWrite(BLUE_PIN, blue);
}

show sketch.ino

It sets a color with setColor, prints the color's name to the serial monitor and waits one second. The loop uses it for each color.

void show(const char *name, int red, int green, int blue) {
  setColor(red, green, blue);
  Serial.println(name);
  delay(1000);
}

loop sketch.ino

It calls show for red, green, blue, white and purple in turn, then starts over.

void loop() {
  show("red", 255, 0, 0);
  show("green", 0, 255, 0);
  show("blue", 0, 0, 255);
  show("white", 255, 255, 255);
  show("purple", 170, 0, 255);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The colors come out in the wrong order. This project
    Check which resistor goes to which leg. Pin 3 is red, pin 5 is green and pin 6 is blue.
  • Nothing lights up. This project
    The long leg is the shared cathode and goes to GND. Check it is not on a color pin.
  • The colors are wrong or one color never lights. RGB LED
    Check whether your LED is common cathode (shared leg to ground) or common anode (shared leg to 5 V). Common anode works backwards: LOW turns a color on.
  • One color is much brighter than the others, or the LED runs too hot. RGB LED
    Use one resistor per color leg, never one for the shared leg. Typical values are 220 ohm for red and 100 to 150 ohm for green and blue on 5 V.
  • The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
    Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.
  • A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
    Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground.
  • Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
    Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected.
  • Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
    Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.

Change it and keep it

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