GAMERIFTRobotics & Coding Shop kits
HomeKitsFor schoolsAboutGAMERIFT Store ↗
Beginner Kit · Lesson 05 of 12

Rainbow Maker

One bulb, three colours inside, millions of mixes. Today you paint with light.

Unlock your lessons

Enter the access code from the card inside your Beginner Kit (or from your GAMERIFT order email). Your name appears on every page and printout as the licensee.

Don't have a kit yet? Get the Beginner Kit, the code ships inside. Lost your code? Contact us with your order number.

LICENSED TO , PERSONAL USE · © GAMERIFT

BRIEF Every screen you own is doing this

Lean in close to a TV (not too close) and every white pixel is secretly three tiny lamps: red, green and blue. Mix them in different strengths and you get every colour a screen can show. Your kit has one of these three-in-one lamps, the RGB LED, and after last lesson you already know the dimming trick. Three PWM pins, three colours, infinite mixes. Today you build a lamp that can be any colour you want.

Time: 45–60 minutes · Difficulty: 2/5 · You need: Lesson 04 done (PWM is the whole trick here).

KIT What you need

Concept · Four legs, three lamps

The RGB LED is three LEDs sharing one roof. Three of the legs are the red, green and blue inputs. The longest leg is the shared ground (the "common cathode"), everything flows out through it. Hold the LED with the longest leg second from the left: the legs read red, ground, green, blue.

Concept · Functions, or how not to repeat yourself

You could write three analogWrite lines every time you want a colour. Or you could teach the board a new word, setColor(r, g, b), and say it once. A function is exactly that: a recipe you name, then reuse. Big programs are just hundreds of small recipes calling each other.

BUILD Wire it

Unplug the USB. Push the RGB LED's four legs into four separate breadboard rows. Longest leg second from the left.

FromToWhy
R3 pin 9 (~)Resistor → RED leg (leftmost)Red channel, dimmable
R3 pin 10 (~)Resistor → GREEN leg (third)Green channel, dimmable
R3 pin 11 (~)Resistor → BLUE leg (rightmost)Blue channel, dimmable
Longest leg (second)R3 GNDThe shared drain for all three

Three resistors, not one. Each colour needs its own bodyguard because each is its own LED.

CODE Program it

// GAMERIFT Beginner Kit, Lesson 05: Rainbow Maker
// Teach the board a new word: setColor.

const int redPin   = 9;
const int greenPin = 10;
const int bluePin  = 11;

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

// Our new recipe: three strengths in, one colour out.
void setColor(int r, int g, int b) {
  analogWrite(redPin, r);
  analogWrite(greenPin, g);
  analogWrite(bluePin, b);
}

void loop() {
  setColor(255, 0, 0);    // pure red
  delay(800);
  setColor(0, 255, 0);    // pure green
  delay(800);
  setColor(0, 0, 255);    // pure blue
  delay(800);
  setColor(255, 120, 0);  // orange, a mix!
  delay(800);
  setColor(150, 0, 200);  // purple
  delay(800);
  setColor(255, 255, 255);// all three: white-ish
  delay(800);
}
  1. Upload and watch the colour show.
  2. Now invent a colour. Pick three numbers between 0 and 255, add your own setColor line, and predict the colour before you upload. Were you right?

BREAK IT Now break it on purpose

  1. Pull out the green resistor. Watch your whites turn pink and your yellows turn red. One broken channel poisons every mix, this is exactly what a "dead subpixel" on a screen is.
  2. setColor(10, 10, 10). Barely-there moonlight. The difference between 10 and 255 is your eye's astonishing range.
  3. Move the blue wire from pin 11 to pin 12 (not a ~ pin, remember?). Blue becomes all-or-nothing while red and green still glide. A half-broken rainbow teaches more than a working one.

REMIX Make it yours

LOG IT Show and tell

Film 15 seconds of your best colour creation for your lesson log. Stuck? Send a photo of your wiring via contact us.

Next · Lesson 06: Night Light

So far you control the light. Next lesson the room controls it: a sensor that feels darkness arrive and switches your lamp on by itself. Your first automatic machine.

All lessons