Built-in project · Arduino Uno R3

RGB LED module: red 11, blue 10, green 9, on an Arduino Uno

An RGB LED with red on pin 11, blue on 10 and green on 9, each through 220 ohm: each color fades up and down in turn with analogWrite. 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

  • How an RGB LED has three colors in one package
  • Fading with analogWrite
  • Writing a helper function that takes a pin
  • Matching colors to the right pins
  • Difficulty: Beginner
  • Time: About 15 minutes
  • Board: Arduino Uno R3
  • Parts: 6

How it works

An RGB LED holds three LEDs in one: red, green and blue, with a shared leg to ground. Each color has its own 220 ohm resistor and its own PWM pin: red on 11, blue on 10 and green on 9.

The sketch fades one color at a time. It ramps a color from off to full in 4 ms steps, then back down, then turns it off. It goes through red, green and blue, and prints the color name before each one. Then it starts over.

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 Ω (3) pin 1Arduino Uno R3 pin 11
Resistor, 220 Ω (1) pin 1Arduino Uno R3 pin 10
Resistor, 220 Ω (2) pin 1Arduino Uno R3 pin 9
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
// An RGB LED module: fade each color up and down in turn.
//
//   R -> 220 ohm -> pin 11   B -> 220 ohm -> pin 10   G -> 220 ohm -> pin 9
//   the common leg -> GND
//
// analogWrite sets each color's share of the time switched on, 0 to 255.

const int RED = 11;
const int BLUE = 10;
const int GREEN = 9;

void fade(int pin) {
  for (int v = 0; v < 255; v++) {
    analogWrite(pin, v);
    delay(4);
  }
  for (int v = 255; v > 0; v--) {
    analogWrite(pin, v);
    delay(4);
  }
  analogWrite(pin, 0);
}

void setup() {
  pinMode(RED, OUTPUT);
  pinMode(BLUE, OUTPUT);
  pinMode(GREEN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: RGB LED on pins 11, 10 and 9");
}

void loop() {
  Serial.println("red");
  fade(RED);
  Serial.println("green");
  fade(GREEN);
  Serial.println("blue");
  fade(BLUE);
}

Code walkthrough

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

fade sketch.ino

Takes a pin number. The first loop raises the value v from 0 to 254 with analogWrite, waiting 4 ms each step, so the color brightens. The second loop counts back down to 1. It then writes 0 to turn the color off.

void fade(int pin) {
  for (int v = 0; v < 255; v++) {
    analogWrite(pin, v);
    delay(4);
  }
  for (int v = 255; v > 0; v--) {
    analogWrite(pin, v);
    delay(4);
  }
  analogWrite(pin, 0);
}

setup sketch.ino

Sets RED (11), BLUE (10) and GREEN (9) as outputs, then starts serial at 9600 and prints a title.

void setup() {
  pinMode(RED, OUTPUT);
  pinMode(BLUE, OUTPUT);
  pinMode(GREEN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: RGB LED on pins 11, 10 and 9");
}

loop sketch.ino

Prints a color name and calls fade for it, in the order red, green, blue. Because fade does the waiting, loop stays short.

void loop() {
  Serial.println("red");
  fade(RED);
  Serial.println("green");
  fade(GREEN);
  Serial.println("blue");
  fade(BLUE);
}

Troubleshooting

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

  • The colors come out in the wrong order This project
    Red is on pin 11, blue on 10 and green on 9. Check that each leg is on the right pin.
  • One color never shows This project
    Check that color's resistor and that the common leg goes to GND.
  • 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.