Mission 07 · Stage 1

RGB LED Basics

three digital outputs for RGB channels

Turn red, green, and blue channels on and off with digitalWrite().

RGB LED Basics circuit diagram

Pin connections

Part 1Part 2

Arduino

pin 9

Red resistor

pin 1

Red resistor

pin 2

RGB LED

red (R)

Arduino

pin 10

Green resistor

pin 1

Green resistor

pin 2

RGB LED

green (G)

Arduino

pin 11

Blue resistor

pin 1

Blue resistor

pin 2

RGB LED

blue (B)

RGB LED

common (COM)

Arduino

GND

See it

Let's make a color-changing light!

One tiny light can glow red, then green, then blue, like magic.

Color lights are in phone screens, TVs, and fun party lamps!

The story

The problem

A normal light only shows one color. This special light hides three colors inside!

Think of it like

It's like having three light switches in one bulb — one for red, one for green, one for blue.

Meet the parts

It thinks and tells the light which color to show.

Arduino

The brain

Loading part…

One little bulb that can glow red, green, or blue.

RGB LED

The color light

Loading part…

Keeps the red color safe from too much power.

Red resistor

The protector

Loading part…

Keeps the green color safe from too much power.

Green resistor

The protector

Loading part…

Keeps the blue color safe from too much power.

Blue resistor

The protector

Loading part…

How it works

1

Glow red

Turn all the colors off first, then turn on only red. The light glows red!

allOff();
digitalWrite(PIN_R, HIGH);
delay(500);
2

Glow green

Red turns off and green comes on. Only one color glows at a time.

allOff();
digitalWrite(PIN_G, HIGH);
delay(500);
3

Glow blue

Green turns off and blue glows. Then loop() jumps back to red and starts again!

allOff();
digitalWrite(PIN_B, HIGH);
delay(500);

Then loop back to step 1

Build the circuit

Follow these steps in order. Match the wires to the colors shown.

  1. 1

    Place Arduino

    Place the Arduino (uno) on the breadboard.

    Arduino placed — ready to build!

    Loading part…
  2. 2

    Place RGB LED

    Place the RGB LED (rgb1) on the breadboard.

    Loading part…
  3. 3

    Place Red resistor

    Place the Red resistor (rr) on the breadboard.

    Loading part…
  4. 4

    Place Green resistor

    Place the Green resistor (rg) on the breadboard.

    Loading part…
  5. 5

    Place Blue resistor

    Place the Blue resistor (rb) on the breadboard.

    Loading part…
  6. 6

    Connect Arduino pin 9 to Red resistor (rr) 1

    Connect Arduino pin 9 to Red resistor (rr) 1.

  7. 7

    Connect Red resistor (rr) 2 to RGB LED (rgb1) R

    Connect Red resistor (rr) 2 to RGB LED (rgb1) R.

  8. 8

    Connect Arduino pin 10 to Green resistor (rg) 1

    Connect Arduino pin 10 to Green resistor (rg) 1.

  9. 9

    Connect Green resistor (rg) 2 to RGB LED (rgb1) G

    Connect Green resistor (rg) 2 to RGB LED (rgb1) G.

  10. 10

    Connect Arduino pin 11 to Blue resistor (rb) 1

    Connect Arduino pin 11 to Blue resistor (rb) 1.

  11. 11

    Connect Blue resistor (rb) 2 to RGB LED (rgb1) B

    Connect Blue resistor (rb) 2 to RGB LED (rgb1) B.

  12. 12

    Connect RGB LED (rgb1) COM to Arduino GND

    Connect RGB LED (rgb1) COM to Arduino GND.

Try it

  • Press the green Run button — the light should change red, then green, then blue.
  • Only one color glows at a time!

Peek at code

The three color nicknames

const int PIN_R = 9;
const int PIN_G = 10;
const int PIN_B = 11;

This light has three colors inside, so it needs three pins — one for red, one for green, one for blue.

The allOff() helper

void allOff() {
  digitalWrite(PIN_R, LOW);
  digitalWrite(PIN_G, LOW);
  digitalWrite(PIN_B, LOW);
}

This little helper turns all three colors off, so each new color starts fresh.

The color cycle

void loop() {
  allOff();
  digitalWrite(PIN_R, HIGH);
  delay(500);
  allOff();
  digitalWrite(PIN_G, HIGH);
  delay(500);
  allOff();
  digitalWrite(PIN_B, HIGH);
  delay(500);
}

loop() shows red, then green, then blue, each with the same short wait. Then it starts over!

Show full sketch (rgb-led-basics.ino)
const int PIN_R = 9;
const int PIN_G = 10;
const int PIN_B = 11;
void allOff() {
  digitalWrite(PIN_R, LOW);
  digitalWrite(PIN_G, LOW);
  digitalWrite(PIN_B, LOW);
}
void setup() {
  pinMode(PIN_R, OUTPUT);
  pinMode(PIN_G, OUTPUT);
  pinMode(PIN_B, OUTPUT);
}
void loop() {
  allOff();
  digitalWrite(PIN_R, HIGH);
  delay(500);
  allOff();
  digitalWrite(PIN_G, HIGH);
  delay(500);
  allOff();
  digitalWrite(PIN_B, HIGH);
  delay(500);
}

Quick quiz

Q1. Which part runs again and again?

  • A. loop()
  • B. setup()
  • C. pinMode only
Why: Yes! loop() runs again and again, forever.

Q2. How many pins does this color light need?

  • A. Three — one for each color
  • B. One pin for everything
  • C. None — it just knows
Why: Yes! Red, green, and blue each get their own pin.

Code lab — try on your own

  1. Show each color quicker — change every delay(500) to delay(250).

    Hint: There are three delay lines inside loop(), one after each color.

  2. Add a little note (comment) on the line that turns red on.

    Hint: It is the line right after the first allOff() inside loop().

Code walkthrough

A line-by-line tour of the sketch — the same steps as in Robot Gurukul Studio.

Program overview

Big idea

Every Arduino program has a top part, a setup() part that runs once, and a loop() part that runs again and again.

In this project

One special light glows red, then green, then blue, forever.

Tip

Read from the top to the bottom. Tap any word or line if you need help!

const int PIN_R = 9;
const int PIN_G = 10;
const int PIN_B = 11;
void allOff() {
  digitalWrite(PIN_R, LOW);
  digitalWrite(PIN_G, LOW);
  digitalWrite(PIN_B, LOW);
}

setup()

Big idea

setup() runs one time when the board turns on.

In this project

It gets pins 9, 10, and 11 ready for the red, green, and blue colors.

Why here

Things we do only once go inside setup().

void setup() {
  pinMode(PIN_R, OUTPUT);
  pinMode(PIN_G, OUTPUT);
  pinMode(PIN_B, OUTPUT);
}

loop()

Big idea

loop() runs again and again, forever.

In this project

This is where the light changes color from red to green to blue.

Why here

Things that repeat go inside loop().

void loop() {
  allOff();
  digitalWrite(PIN_R, HIGH);
  delay(500);
  allOff();
  digitalWrite(PIN_G, HIGH);
  delay(500);
  allOff();
  digitalWrite(PIN_B, HIGH);
  delay(500);
}

Try this: Change a delay number inside loop(), then press Run to make each color last longer.

pinMode

Big idea

pinMode tells a pin if it will listen or push power out.

In this project

It makes pin 9 ready to push power to the red color.

Why here

It goes in setup() because we only set it once.

  pinMode(PIN_R, OUTPUT);

digitalWrite

Big idea

digitalWrite turns a pin ON or OFF.

In this project

ON lights up a color, OFF turns it dark. Here it turns the red color off.

Why here

It goes in loop() so the colors can keep changing.

  digitalWrite(PIN_R, LOW);

delay

Big idea

delay means wait. Nothing else happens while it waits.

In this project

It keeps each color glowing long enough for you to see it.

Why here

Right after we turn a color on.

  delay(500);