Arduino Learning Studio full lab
0/13 lessons marked complete
● Saved in this browser← Back to homepage

Lesson 5 of 13

Mix colours

Objective: Mix red, green and blue light with three analogWrite() values, and make the same colours on a common-anode LED, where 0 means full.

MODEL OF THIS SKETCHSTOPPED
ARDUINO UNO5VGND~D9~D10~D11220 Ω220 Ω220 ΩRGBcommon cathodelongest leg (common)

The LED is dark until you run.

While the sketch runs, a zoomed-in view shows each pin switching, and a readout shows how much light each colour gives.

Stage 1 of 7 · What am I building?

Explore

A robot uses a coloured light to tell people what it is doing: ready, careful, stop. One RGB LED can make all of them from three numbers. Select each part on the circuit, or below.

Pick a part. 0/5 inspected.

Before you start (optional refreshers)

Component polarity

Some parts only work one way round. An LED's longer leg (anode) goes toward the positive side; reversed, it blocks current and stays dark.

PWM and duty cycle

analogWrite() switches a ~ pin on and off quickly. The ON fraction (0–255 → 0–100%) sets average power. It is not a true analog voltage.

New ideas in this lesson

Additive colour

Red, green and blue light add together: red + green looks yellow, red + blue magenta, green + blue cyan, all three white. PWM sets how much of each colour, so in-between values make in-between colours such as orange.

Common anode and common cathode

An RGB LED's colours share one leg, the longest. Common cathode: it goes to GND and a colour lights while its pin is HIGH. Common anode: it goes to 5V and a colour lights while its pin is LOW, so the sketch writes 255 - value.

Build it for real · Arduino Uno R3

You need

  • Arduino Uno R3 (or compatible) and its USB cable
  • Breadboard and jumper wires
  • Common-cathode RGB LED
  • 3 × 220 Ω resistors

Wiring

  1. RGB LED longest leg (common cathode) → GND.
  2. Red leg → 220 Ω → D9; green leg → 220 Ω → D10; blue leg → 220 Ω → D11.
  3. Use one resistor per colour.

Before you power up: Check your LED type: on a common-anode RGB LED the long leg goes to 5V and the colours invert (255 = off). Never share one resistor between colours.

Tip: The longest leg is the common one, but the order of the colour legs differs between makers: read it from your LED's datasheet. The colours are not equally bright at the same value: compare the mcd figures there too.

The lab models ideal parts. Real circuits add tolerances, noise and loose wires, so check your wiring against this list before you plug in.

Sketch · read-only
// Mix a colour on a common-cathode RGB LED
// Red, green and blue legs to PWM pins 9, 10, 11
// (each through a 220 ohm resistor); longest leg to GND
const int redPin = 9;             // a ~ pin: it can make PWM
const int greenPin = 10;          // a ~ pin: it can make PWM
const int bluePin = 11;           // a ~ pin: it can make PWM

void setup() {
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
}

void loop() {
  // asks for red 255, green 120, blue 0: about orange on a screen
  analogWrite(redPin, 255);       // steady HIGH
  analogWrite(greenPin, 120);     // HIGH 47% of each cycle
  analogWrite(bluePin, 0);        // steady LOW
}

Press ▶ Run to light the LED.