Mix colours with the RGB LED module

Drive the red, green and blue parts of the RGB LED module with PWM and mix any colour you like.
  • Level:Beginner
  • Duration: 35 min
  • Ages 10+
  • Platform: Arduino

What you'll need

Tools for this lesson

Helpful for this build. Open a tool to see tips and more projects that use it.

Mix colours with the RGB LED module – EEMENTOR STEM™ lesson

An RGB LED is really three LEDs in one package: red, green and blue. By changing how bright each one is, you can mix almost any colour, just like the pixels on a screen.

What you'll learn

  • What PWM (pulse-width modulation) is
  • How to use analogWrite()
  • How colours are mixed from red, green and blue

What you need

  • UNO R3 board (or any Arduino-compatible board) and USB cable
  • RGB LED module from the 45-in-1 kit
  • Breadboard
  • 3 resistors, 220 Ω
  • Jumper wires

Wiring

Check the labels on your module: R, G, B and − (the common ground). A 220 Ω resistor in each colour line protects the LED, even if your module already has resistors on board.

Breadboard wiring diagram: Mix colours with the RGB LED module
Breadboard view. Check each connection against the wiring table below.

Build it step by step

Plug in all the parts first, then add the wires in this order. In each picture the new wires are bright and the ones from earlier steps are faded. Click a picture to see it larger.

Step 1 of 2: GND from the UNO to the blue rail
Step 1. GND from the UNO to the blue rail
Step 2 of 2: RGB LED module: pins 9, 10 and 11
Step 2. RGB LED module: pins 9, 10 and 11
From Through To
UNO pin 9 220 Ω resistor Module R
UNO pin 10 220 Ω resistor Module G
UNO pin 11 220 Ω resistor Module B
Module − UNO GND
FREE

Build this circuit in EEMENTOR Lab

Wire it yourself in your browser before you touch a real wire. No sign-up, nothing to install.

The parts for this circuit laid out in EEMENTOR Lab, ready to wireBuild it yourself
  1. Wire the parts using the wiring table above. Pins with no wire show red.
  2. Press Run and watch the LED change colour.
  3. Then try this lesson’s sketch: paste it into the Code tab and press Compile & Run.

EEMENTOR Lab is free and in beta. Results are a guide; a real build can differ slightly. About the Lab

The sketch

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

void setColor(int red, int green, int blue) {
  analogWrite(RED_PIN, red);     // 0 = off, 255 = full brightness
  analogWrite(GREEN_PIN, green);
  analogWrite(BLUE_PIN, blue);
}

void setup() {
  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
}

void loop() {
  setColor(255, 0, 0);    // red
  delay(1000);
  setColor(0, 255, 0);    // green
  delay(1000);
  setColor(0, 0, 255);    // blue
  delay(1000);
  setColor(255, 255, 0);  // yellow
  delay(1000);
  setColor(0, 255, 255);  // cyan
  delay(1000);
  setColor(255, 0, 255);  // magenta
  delay(1000);
  setColor(255, 255, 255); // white
  delay(1000);
}

What you should see

The RGB LED shows red, green, blue, yellow, cyan, magenta and white for one second each, with a chart of when pins 9, 10 and 11 are on.

How it works

PWM waves for analogWrite values 255, 128, 64 and 0 next to an LED at matching brightness, and red, green and blue light circles mixing into yellow, cyan, magenta and white.

Pins 9, 10 and 11 support PWM, marked with a ~ on the board. PWM switches the pin on and off very fast, hundreds of times per second. analogWrite(pin, 128) keeps it on about half the time, which your eye sees as half brightness. Mixing red and green gives yellow, green and blue gives cyan, and all three together give white.

Try this next

  • Make a smooth rainbow by slowly changing the values in a for loop.
  • Combine this with the night light lesson: the darker the room, the bluer the light.

Troubleshooting

  • A colour is missing: check that colour's resistor and wire, and that it goes to pin 9, 10 or 11.
  • The colours are inverted (white when it should be off): you have a common-anode LED. Connect its common pin to 5V instead of GND, and use 255 - value in setColor().