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.

Beginner Runs in your browser. Free, and no account needed.

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.

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);
  }
}

Parts list

6 parts, plus the jumper wires. Every one is in the editor's parts bin.

How it is wired

7 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Arduino Uno R3 pin 11; Resistor, 220 Ω (1) pin 1
  • Arduino Uno R3 pin 10; Resistor, 220 Ω (2) pin 1
  • Arduino Uno R3 pin 9; Resistor, 220 Ω (3) pin 1
  • Ground: Arduino Uno R3 pin GND; RGB LED pin C
  • RGB LED pin R; Resistor, 220 Ω (3) pin 2
  • RGB LED pin G; Resistor, 220 Ω (2) pin 2
  • RGB LED pin B; Resistor, 220 Ω (1) pin 2

Change it and keep it

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

Free forever. Every board and part. Saves your projects. Works on Chromebooks.