Built-in project · Arduino Uno R3

RGB color mixer on an Arduino Uno

An RGB LED on three PWM pins, each through its own resistor: analogWrite fades it through ten named colors, including a white balanced for the red die. 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.

Step by step, with the wiring and the common mistakes: Arduino RGB LED: Wiring and Color Mixing Code

RGB color mixer · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

What you'll learn

  • Mixing colors with PWM on three pins
  • How an RGB LED works
  • Fading smoothly between two values
  • Structs for named colors
  • Common cathode versus common anode
  • Difficulty: Beginner
  • Time: About 15 minutes
  • Board: Arduino Uno R3
  • Parts: 6

How it works

An RGB LED is three LEDs under one lens. Pins 9, 10 and 11 drive the red, green and blue legs through 220 Ω resistors, and the common leg goes to ground. Your eye mixes the three into one color.

analogWrite sets how bright each one is, from 0 to 255. The sketch fades from one named color to the next over one second, holds it for one second, and prints the color name and its three numbers.

There are two whites. With all three at 255 the light looks pinkish, because red gets more current than the others. The balanced white turns red down to 170.

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 11
Resistor, 220 Ω (2) pin 1Arduino Uno R3 pin 10
Resistor, 220 Ω (3) 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
// RGB color mixer: one RGB LED, three PWM pins, every color in between.
//
//   pin 9   the red leg, through 220 ohm
//   pin 10  the green leg, through 220 ohm
//   pin 11  the blue leg, through 220 ohm
//   the long leg (the common cathode) to GND
//
// An RGB LED is three LEDs under one lens. analogWrite() sets how bright each
// one is, from 0 (off) to 255 (full), and your eye mixes the three into one
// color. Red and green make yellow, green and blue make cyan, all three make
// white.
//
// The sketch fades from one named color to the next over one second, holds it
// for a second, and prints its name and its three numbers when it gets there.
//
// # Why there are two whites
//
// The red die needs about 1.8 V and the green and blue ones about 3 V. On the
// same 220 ohm resistors from a 5 V pin, red gets about 15 mA and the other
// two about 10, so "all three at 255" comes out pinkish. The balanced white
// turns red down to two thirds to make up for it. On a real LED you can do
// the same in code, or give red a bigger resistor.
//
// # Common anode
//
// This is a common-cathode LED: HIGH lights a color. On a common-anode LED
// the long leg goes to 5 V and LOW lights a color, so every value is flipped:
// write 255 - value instead.

const int RED_PIN = 9;
const int GREEN_PIN = 10;
const int BLUE_PIN = 11;

struct Color {
  const char *name;
  uint8_t r, g, b;
};

const Color COLORS[] = {
    {"red", 255, 0, 0},
    {"orange", 255, 40, 0},
    {"yellow", 255, 160, 0},
    {"green", 0, 255, 0},
    {"cyan", 0, 255, 255},
    {"blue", 0, 0, 255},
    {"purple", 120, 0, 255},
    {"magenta", 255, 0, 255},
    {"white (all 255)", 255, 255, 255},
    {"balanced white", 170, 255, 255},
};
const int COUNT = sizeof(COLORS) / sizeof(COLORS[0]);

const int FADE_STEPS = 50;
const int STEP_MS = 20;
const int HOLD_MS = 1000;

int r = 0, g = 0, b = 0;

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

void fadeTo(const Color &c) {
  for (int step = 1; step <= FADE_STEPS; step++) {
    show(r + (c.r - r) * step / FADE_STEPS,
         g + (c.g - g) * step / FADE_STEPS,
         b + (c.b - b) * step / FADE_STEPS);
    delay(STEP_MS);
  }
  r = c.r;
  g = c.g;
  b = c.b;
}

void setup() {
  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
  Serial.begin(115200);
  Serial.println("Mokxi Uno: RGB color mixer");
}

void loop() {
  for (int i = 0; i < COUNT; i++) {
    fadeTo(COLORS[i]);
    Serial.print(COLORS[i].name);
    Serial.print(": ");
    Serial.print(COLORS[i].r);
    Serial.print(", ");
    Serial.print(COLORS[i].g);
    Serial.print(", ");
    Serial.println(COLORS[i].b);
    delay(HOLD_MS);
  }
}

Code walkthrough

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

show sketch.ino

Writes three brightness values to the red, green and blue pins with analogWrite. Everything else in the sketch goes through it.

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

fadeTo sketch.ino

Moves from the current color to a new one in FADE_STEPS steps of STEP_MS each. At each step, it works out a point part of the way between the old and new value for each color. At the end it stores the new color in r, g and b.

void fadeTo(const Color &c) {
  for (int step = 1; step <= FADE_STEPS; step++) {
    show(r + (c.r - r) * step / FADE_STEPS,
         g + (c.g - g) * step / FADE_STEPS,
         b + (c.b - b) * step / FADE_STEPS);
    delay(STEP_MS);
  }
  r = c.r;
  g = c.g;
  b = c.b;
}

loop sketch.ino

Goes through the COLORS list. For each color it fades to it, prints the name and its three numbers, then holds for HOLD_MS.

void loop() {
  for (int i = 0; i < COUNT; i++) {
    fadeTo(COLORS[i]);
    Serial.print(COLORS[i].name);
    Serial.print(": ");
    Serial.print(COLORS[i].r);
    Serial.print(", ");
    Serial.print(COLORS[i].g);
    Serial.print(", ");
    Serial.println(COLORS[i].b);
    delay(HOLD_MS);
  }
}

Troubleshooting

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

  • The colors are wrong, such as red showing as blue This project
    Red is on pin 9, green on 10 and blue on 11. Check each resistor goes to the matching LED leg.
  • Colors look inverted, or the LED is bright when it should be off This project
    You may have a common anode LED. Connect its common leg to 5 V and write 255 minus each value.
  • 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.