Learning Goals 5 min
By the end of this lesson you will be able to:
- Identify the four pins of a 5 mm RGB LED — red, green, blue, and a shared common pin — by length and position, and tell at a glance whether yours is common cathode or common anode.
- Wire either type onto a breadboard with three 220 Ω resistors (one per colour channel), one shared connection for the common pin, and three signal wires back to digital pins on the Arduino.
- Drive each colour on and off with
digitalWrite, and combine two or three channels at once to produce the seven "primary" digital colours: red, green, blue, yellow, cyan, magenta and white.
Warm-Up 10 min
Cluster B and C used three separate LEDs (red, yellow, green) for the traffic light. They worked beautifully, but they took up half your breadboard. Cluster E begins by collapsing that pile of LEDs into a single dome the size of one regular LED. Inside it: three tiny LEDs, glued back-to-back, sharing one pin. With three Arduino outputs and a little colour theory, you can paint any colour on the visible spectrum from one little component.
Quick-fire puzzle
Look at any backlit screen — your phone, your laptop, a TV. Hold a magnifying glass close to a white area. You'll see something surprising: there is no white. There are only tiny red, green and blue dots, all lit at full brightness. Your eye blends them into white from a normal viewing distance.
- If "white" on your phone is actually red+green+blue all on, what do you think "yellow" is, in terms of those three?
- What about "magenta" (a hot pink)?
- And what's the only "colour" the screen makes by turning everything off?
Reveal the answer
- Yellow = red + green. No blue at all. Counterintuitive — you might expect yellow to need its own colour — but every yellow you see on a screen is your eye fusing a red pixel and a green pixel into one perceived colour.
- Magenta = red + blue. No green. Magenta isn't even on the rainbow (you can't find a "magenta wavelength" of light); it's an entirely invented colour that exists only as a brain trick of mixing two ends of the spectrum.
- Black. Black is the absence of light. Your eyes see "black" when no LED is shining at all.
That's the same trick your RGB LED uses. Three little chips of red, green and blue, sitting under one diffusing dome that blends them. Today you wire it; tomorrow's lesson uses PWM to mix any colour on the spectrum.
New Concept — three LEDs in one dome 15 min
The big idea — one package, three colours, four pins
An RGB LED looks like a regular 5 mm LED from the outside, but with four legs instead of two. Inside the clear or diffused plastic you can sometimes see three tiny coloured chips arranged in a triangle. Each chip is its own LED — red, green, and blue — and each one wants its own current limit. So instead of one resistor, this part needs three.
Three colour chips × two pins each = six pins, right? No — they save space by sharing one pin between all three. That shared pin is called the common. The other three pins are the individual R, G, and B control pins.
Two flavours: common cathode vs common anode
There are two ways to share the common pin, and you need to know which one you have. Get this wrong and the LED won't light at all — or worse, it'll light "inverted" (the off bits glow and the on bits go dark).
Identifying the pins on a real RGB LED
Hold the LED with all four legs pointing down and the flat side of the dome facing you. From this angle, the longest leg is always the common pin. The standard pinout (followed by almost every hobby-grade RGB LED from kit shops) is:
- Leg 1 (short) — Red
- Leg 2 (longest) — Common
- Leg 3 (short) — Green
- Leg 4 (short) — Blue
So with the common leg as your reference, Red is the leg before it (one step left) and Green and Blue are the two legs after it. Worth double-checking against your kit's datasheet if you have one — a small number of brands flip Red and Blue.
The "which type do I have?" test
If the bag didn't say, you can find out in 30 seconds with one resistor and the Arduino's 5 V and GND pins:
- Put a 220 Ω resistor in series with the common leg.
- Touch the other end of the resistor to +5 V. Touch each colour leg in turn to GND through a wire.
- If the colour lights: you have a common anode LED (current flows in through the common, out through the colour pin to ground).
- If nothing lights: swap the connections — common to GND through the resistor, colour pins to +5 V. If they light now, you have a common cathode.
That's it. Five seconds per pin, all three colours tested, you know exactly which type you have. Most kits sold for Arduino starter packs are common cathode — but always check.
The three-resistor rule
Don't try to save a part by using only one resistor on the common pin. The three LEDs inside have different forward voltages (red is around 2 V, green and blue closer to 3 V) and different ideal currents. A single shared resistor would let one channel hog all the current, washing out the others. One 220 Ω resistor per colour channel, always. This is also why a tidy RGB circuit looks slightly busier than the three-LED rig from L01-15 — same number of resistors, but all bunched at one component.
Why it matters
The RGB LED is the smallest version of the same trick that powers every full-colour display on the planet — phones, TVs, monitors, LED strips, stage lighting, even the screen you're reading this on. The differences are only of scale (millions of pixels instead of one) and brightness control (PWM levels instead of on/off, which is L01-31's job). Master the wiring of one RGB LED today, and you understand the foundation of every colour-mixing system.
Worked Example — wire it and cycle the colours 20 min
You're going to wire your RGB LED, identify which type you have, write a sketch that cycles each colour on and off, and finally show all seven combinations of the three channels.
Step 1 — Identify your LED
Run the "which type?" test from the New Concept. Find your longest leg (the common). Probe each colour against +5 V and GND with a 220 Ω resistor in series. Write down on a sticky note: common cathode or common anode. The rest of this lesson assumes common cathode (the kit default); if yours is common anode, you'll invert every HIGH/LOW in the sketches — note that for now and we'll address it in the questions.
Step 2 — Wire the breadboard
Pick a quiet patch of the breadboard with at least seven free columns. The RGB LED's four legs straddle the trough across rows E and F, like the buttons in L01-16 — but with all four legs in one row, not two-and-two. Pop it in so the legs land in adjacent columns; you'll get one full column per leg in rows A–E (above the trough) for the colour resistors and signal wires.
Step 3 — The "primary digital colours" sketch
With three on/off channels there are exactly 2 × 2 × 2 = 8 possible combinations, including all-off ("black"). That gives us seven visible "colours" — the corner colours of the RGB cube — which is what we'll cycle through today.
// RGB LED — cycle the seven digital primary colours
const int RED_PIN = 9;
const int GREEN_PIN = 10;
const int BLUE_PIN = 11;
void setColour(int r, int g, int b) {
digitalWrite(RED_PIN, r);
digitalWrite(GREEN_PIN, g);
digitalWrite(BLUE_PIN, b);
}
void setup() {
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
}
void loop() {
setColour(HIGH, LOW, LOW); delay(600); // red
setColour(LOW, HIGH, LOW); delay(600); // green
setColour(LOW, LOW, HIGH); delay(600); // blue
setColour(HIGH, HIGH, LOW); delay(600); // yellow (R+G)
setColour(LOW, HIGH, HIGH); delay(600); // cyan (G+B)
setColour(HIGH, LOW, HIGH); delay(600); // magenta (R+B)
setColour(HIGH, HIGH, HIGH); delay(600); // "white" (all three)
}Step 4 — Upload and watch the colour cube
- Upload the sketch. The LED should cycle: red → green → blue → yellow → cyan → magenta → white → red…
- The white isn't a pure white — it's a slightly pinkish or bluish white depending on which chip in your LED is strongest. That's normal at full-brightness digital control; PWM in L01-31 will let you balance it properly.
- If the cycle looks wrong — for example, "red" looks blue and "blue" looks red — you've probably mis-identified the pins. Swap your D9 and D11 wires to fix it without touching the code.
- If every colour is on at the wrong moment (off when you expect on, and vice versa), you have a common-anode LED but you wired it as common-cathode. Two options: (a) move the common leg from GND to +5 V and swap every
HIGH/LOWin the sketch, or (b) use a quick helper:
Now the rest of the sketch still reads "HIGH means on" — the helper hides the inversion.void setColour(int r, int g, int b) { digitalWrite(RED_PIN, !r); // invert for common anode digitalWrite(GREEN_PIN, !g); digitalWrite(BLUE_PIN, !b); }
Trace the cube on paper
The eight combinations of three on/off bits map cleanly to colour names. Fill in the table — work it out from the worked example or from your knowledge of light mixing.
| R | G | B | Colour name |
|---|---|---|---|
| LOW | LOW | LOW | ____ (no light) |
| HIGH | LOW | LOW | ____ |
| LOW | HIGH | LOW | ____ |
| LOW | LOW | HIGH | ____ |
| HIGH | HIGH | LOW | ____ |
| LOW | HIGH | HIGH | ____ |
| HIGH | LOW | HIGH | ____ |
| HIGH | HIGH | HIGH | ____ |
If you can fill in all eight rows, you've understood the digital "colour cube" — the foundation for L01-31's smooth PWM mixing.
Try It Yourself — three RGB sketches 20 min
Goal: Type-driven colour picker. Reuse the L01-26 input pattern. Type r, g, b, y, c, m, w, or k (for black/off) and the RGB LED instantly shows that colour.
Plan: standard input pattern, then a switch/case (L01-28) with eight branches. Each branch calls setColour(...) with the right three HIGH/LOWs.
const int RED_PIN = 9;
const int GREEN_PIN = 10;
const int BLUE_PIN = 11;
void setColour(int r, int g, int b) {
digitalWrite(RED_PIN, r);
digitalWrite(GREEN_PIN, g);
digitalWrite(BLUE_PIN, b);
}
void setup() {
Serial.begin(9600);
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
Serial.println("Colours: r g b y c m w k");
}
void loop() {
if (Serial.available() > 0) {
char ch = Serial.read();
switch (ch) {
case 'r': setColour(HIGH, LOW, LOW); break;
case 'g': setColour(LOW, HIGH, LOW); break;
case 'b': setColour(LOW, LOW, HIGH); break;
case 'y': setColour(HIGH, HIGH, LOW); break;
case 'c': setColour(LOW, HIGH, HIGH); break;
case 'm': setColour(HIGH, LOW, HIGH); break;
case 'w': setColour(HIGH, HIGH, HIGH); break;
case 'k': setColour(LOW, LOW, LOW); break;
}
}
}Questions:
- Why is "k" the letter for black, not "B"? ____ (Hint:
bis taken — and printers have always used "K" for black, the "key" colour in CMYK.) - Type
w. Does the white look pure white, or slightly tinted? Why does that happen at this point in the lesson? ____ - If your LED is common anode, what one helper change makes this sketch work without rewriting the switch? ____
Goal: A two-state warning beacon. The LED alternates between red and blue like an emergency vehicle — 200 ms red, 200 ms blue, forever. No serial.
Plan: in loop(), call setColour(HIGH, LOW, LOW), delay(200), setColour(LOW, LOW, HIGH), delay(200). Two function calls and two delays — that's it.
Questions:
- You only ever turn on one colour at a time. Do you still need three resistors? Why? ____
- What happens to the colour if you reduce the delays to
10 mseach? Predict before trying. ____ (Hint: persistence of vision.) - Add a faint purple middle state by setting
(HIGH, LOW, HIGH)for 50 ms between each colour change. Is the resulting blink-blink-blink animation more "police car" or more "disco"? ____
Goal: Rebuild your L01-29 Serial Light Show, but with one RGB LED instead of three separate LEDs. Same modes (b c a s), same speed control (1–9), same ? help — but every step now shows a colour instead of an LED choice. For example: chase mode goes red → green → blue → red → green → blue.
Plan: open your L01-29 sketch. Change three lines:
- Rename the pin constants from
RED_PIN/YELLOW_PIN/GREEN_PINtoRED_PIN/GREEN_PIN/BLUE_PIN(since the RGB LED has blue, not yellow). - Rename
setLedstosetColour— the call signature is identical, so the body of the show needs no changes. - Rewire: pull the three separate LEDs out, plug the RGB LED in across the trough as in today's worked example, run three signal wires.
That's it. The whole framework you built in L01-29 — non-blocking loop, command parser, animation engine, all of it — keeps working unchanged. This is the payoff for separating "what to show" from "how to show it".
Questions:
- How many lines did you actually have to change in the L01-29 sketch? ____
- What does this suggest about the value of helper functions like
setLeds/setColour? ____ - The "alternate" mode now goes red+blue vs. green (instead of red+green vs. yellow). Why does it look different physically even though the
HIGH/LOWpattern is the same? ____ (Hint: with three separate LEDs they were in separate spots; here they all share one dome.)
Mini-Challenge — the colour-cube cycler 10 min
"Hit every corner of the cube, in order"
The eight combinations of three on/off bits are the eight corners of a "colour cube". Build a sketch that cycles through all eight, printing the colour name to the Serial Monitor with each step, in exactly this order: black → red → green → blue → yellow → cyan → magenta → white. Each step lasts 800 ms.
Your task:
- Reuse
setColour(r, g, b). - In
loop(), eight pairs of "call setColour + print name + delay" — but find a way to avoid copy-pasting the same three lines eight times. Hint: think about extracting "show one colour for 800 ms" into a helper. - The Monitor should print:
…then repeat fromBLACK RED GREEN BLUE YELLOW CYAN MAGENTA WHITEBLACKforever.
It works if:
- The LED visibly hits all eight states in order, and the Monitor names match each one.
- Your
loop()body has only one call per step (not three) — proof that the helper is doing the work.
Reveal one valid sketch
// Colour-cube cycler
const int RED_PIN = 9;
const int GREEN_PIN = 10;
const int BLUE_PIN = 11;
void setColour(int r, int g, int b) {
digitalWrite(RED_PIN, r);
digitalWrite(GREEN_PIN, g);
digitalWrite(BLUE_PIN, b);
}
void show(int r, int g, int b, const char* name) {
setColour(r, g, b);
Serial.println(name);
delay(800);
}
void setup() {
Serial.begin(9600);
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
}
void loop() {
show(LOW, LOW, LOW, "BLACK");
show(HIGH, LOW, LOW, "RED");
show(LOW, HIGH, LOW, "GREEN");
show(LOW, LOW, HIGH, "BLUE");
show(HIGH, HIGH, LOW, "YELLOW");
show(LOW, HIGH, HIGH, "CYAN");
show(HIGH, LOW, HIGH, "MAGENTA");
show(HIGH, HIGH, HIGH, "WHITE");
}The show() helper bundles the three things every step needs (set the LED, print the name, wait), so loop() reads as one line per colour. The mysterious const char* type is just C++'s "this is the address of a piece of text" — for now treat it as "a string parameter" and we'll lift the hood properly in Level 2. Notice how the data-vs-code shape of this sketch is much nicer than eight repeated blocks would be: the eight calls are pure data describing the cycle, and the engine is one tiny helper.
Recap 5 min
An RGB LED is three regular LEDs glued together inside one dome with one shared pin. Two flavours: common cathode (shared pin to GND, drive each colour HIGH to light it) and common anode (shared pin to +5 V, drive each colour LOW to light it — inverted). The longest leg is the common; the other three are R, G, and B in standard order. Use three 220 Ω resistors, one per channel — never one shared. With only HIGH/LOW you can already paint seven visible colours plus black; PWM in L01-31 unlocks the rest of the spectrum.
- RGB LED
- A single 4-pin LED package containing three colour chips — red, green and blue — sharing one pin. The 5 mm hobby version is the same physical size as a standard LED but with twice the legs.
- Common cathode
- An RGB LED whose shared pin is the negative end of all three internal LEDs. Wire that pin to GND; drive each colour pin HIGH to light it. The kit-default flavour.
- Common anode
- An RGB LED whose shared pin is the positive end. Wire that pin to +5 V; drive each colour pin LOW to light it. Logic is inverted compared to common cathode.
- Common pin
- The shared leg of an RGB LED. Always the longest of the four legs on a standard hobby part.
- Three-resistor rule
- Each colour channel needs its own current-limiting resistor (typically 220 Ω). One shared resistor on the common pin gives unbalanced colours and is a real-world bug.
- Digital primary colours
- The eight combinations of three on/off channels: black, red, green, blue, yellow, cyan, magenta, white. The "corners" of the RGB colour cube. Today's full palette without PWM.
Homework 5 min
The single-LED traffic light. Replace the three separate-LED traffic light from L01-15 with one RGB LED. The behaviour stays the same — red for 3 seconds, then green for 3 seconds, with a yellow blink in between — but it all happens in one dome.
- Use your worked-example wiring (R on D9, G on D10, B on D11).
- Use
setColour(r, g, b)as the helper. - Cycle: 3 s red → 1 s yellow → 3 s green → 1 s yellow → repeat.
- The yellow blink uses
HIGH, HIGH, LOW. You're getting yellow "for free" from the dome's mixer — no third physical LED needed.
Also: a design reflection on paper.
- How many physical components does the RGB version use compared to the L01-15 version? Count: LEDs, resistors, wires (rough estimate). ____
- What's worse about the RGB version? Think about what happens if the LED fails, what happens if one colour is brighter than another in your part, and how easily you can see each colour from across the room. ____
- If you had ten LEDs to wire (a long ten-position progress bar, say), would you reach for ten separate LEDs or ten RGB LEDs? Why? ____
- Look closely at your RGB LED in a dark room while it's running. Can you see the three separate chips inside the dome? Where are they positioned relative to each other? ____
Bring back next class:
- The saved
.inofile (call itrgb-traffic-light). - A 15-second phone video showing one full red → yellow → green → yellow cycle.
- Your four written reflection answers, in your notebook.
Heads up for next class: L01-31 "Mixing Colours" finally introduces analogWrite() — instead of HIGH/LOW (two values per channel), each colour now accepts a number from 0 to 255. That's 256³ = over 16 million possible colours from the same RGB LED you wired today. Today's seven-colour cube becomes a fully painted sphere.