Learn

Mix any color with an Arduino and an RGB LED

Beginner

Or see every lesson.

  • 228parts on the bench
  • 31boards running now
  • 1.00xreal time, on every board
Arduino Uno: RGB color mixerlive0.000 s 0.00x
Click to open it in the editor
The RGB color mixer example on a simulated ATmega328P: three PWM pins, three resistors, ten named colors.

An RGB LED looks like one LED, but inside there are three: a red one, a green one and a blue one, under one lens. Turn each one up or down and your eye mixes them into a single color. Red and green make yellow. Green and blue make cyan. All three make white.

The circuit above is an Arduino Uno with a common-cathode RGB LED on pins 9, 10 and 11, each through its own 220 ohm resistor. The sketch fades through ten named colors and prints each one’s three numbers in the serial monitor. It runs in your browser, so you can change the numbers and see the new color right away.

What you need

  • An Arduino Uno
  • A common-cathode RGB LED (four legs, the longest is the shared one)
  • Three 220 ohm resistors
  • A breadboard and jumper wires

Wiring

Part and pinGoes toNote
RGB LED RPin 9Through a 220 ohm resistor
RGB LED GPin 10Through its own 220 ohm resistor
RGB LED BPin 11Through its own 220 ohm resistor
RGB LED common (longest leg)GNDOn a common-anode LED this goes to 5 V instead

Three PWM pins, three numbers

Each color gets its own pin, and each pin must be able to do PWM, so analogWrite() can set a brightness from 0 to 255. On an Uno the PWM pins are 3, 5, 6, 9, 10 and 11, the ones marked with a ~. This circuit uses 9, 10 and 11.

A color is then just three numbers. Orange here is 255 red, 40 green and 0 blue. Purple is 120, 0, 255. The sketch keeps a list of named colors. It fades from one to the next in 50 small steps, so you see every color in between.

Setting a color, from the RGB color mixer example
const int RED_PIN = 9;
const int GREEN_PIN = 10;
const int BLUE_PIN = 11;

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

void setup() {
  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
  show(255, 40, 0);  // orange
}

void loop() {
}

Why "all 255" is not quite white

Watch the LED when the serial monitor prints "white (all 255)". It looks a little pink. The red part of the LED needs about 1.8 V to light, while the green and blue parts need about 3 V. With the same 220 ohm resistors on 5 V pins, red gets about 15 mA. Green and blue get about 10 mA each, so red wins.

The next color, "balanced white", turns red down to 170 and leaves the others at 255. That evens it out, and the LED looks white. You can balance a real LED the same way in code, or give the red leg a bigger resistor so it gets less current.

Common cathode or common anode?

There are two kinds of RGB LED. On a common-cathode LED, like this one, the long leg goes to GND and a HIGH pin lights a color. On a common-anode LED, the long leg goes to 5 V and a LOW pin lights a color.

If you wire a common-anode LED and every color comes out backward, with white where you wanted black, that is why. Wire its long leg to 5 V and write 255 - value instead of value. In the editor, click the LED and set common to anode to try it.

Try it in the editor

Add your own color to the COLORS list, like {"pink", 255, 60, 80}, and run it again.

Put a potentiometer on A0 and use map() to turn its reading into a red value from 0 to 255. Add two more for green and blue and you have a color picker.

Change STEP_MS from 20 to 5 and the fades speed up. Change it to 60 and they slow down.

Common mistakes

One resistor in the common leg instead of three. The colors change brightness as others turn on, because they share the current. Give each color its own resistor.

Using a pin without PWM. analogWrite() on a pin that cannot do PWM only turns it fully on or off, so colors jump instead of mixing.

A common-anode LED wired like a common-cathode one. Nothing lights, or the colors are inverted.

Mixing up the legs. The order is often red, common, green, blue, with the common leg the longest, but check your part’s datasheet.

Questions

Which pins should I use for an RGB LED on an Arduino Uno?

Any three PWM pins: 3, 5, 6, 9, 10 or 11. This page uses 9, 10 and 11, each through its own 220 ohm resistor, with the common leg to GND.

How do I make yellow, orange or purple?

Mix the three numbers. Yellow is red and green together, orange is full red with a little green, and purple is blue with some red. In this circuit orange is 255, 40, 0 and purple is 120, 0, 255.

Is an RGB LED the same as a NeoPixel?

No. A plain RGB LED needs three pins and three resistors. A NeoPixel (WS2812) has a tiny chip inside and takes one data wire for a whole strip. Our ESP32 NeoPixel tutorial covers those.

Build this for real

Open the editor, change a value and watch the number move with it. Nothing to install, and no account needed.

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