Mission 03 · Stage 1

Traffic Light

sequencing multiple outputs with timing

Cycle red, yellow, and green LEDs like a mini traffic signal.

Traffic Light circuit diagram

Pin connections

Part 1Part 2

Arduino

pin 13

Red resistor

pin 2

Red resistor

pin 1

Red LED

anode (+)

Red LED

cathode (-)

Arduino

GND

Arduino

pin 12

Yellow resistor

pin 2

Yellow resistor

pin 1

Yellow LED

anode (+)

Yellow LED

cathode (-)

Arduino

GND

Arduino

pin 11

Green resistor

pin 2

Green resistor

pin 1

Green LED

anode (+)

Green LED

cathode (-)

Arduino

GND

See it

Three lights, one sequence!

Red, yellow, green — only one color on at a time, in order.

Real traffic lights use the same timed sequence idea.

The story

The problem

One LED is not enough — you need multiple outputs in a fixed order.

Think of it like

Like a conductor cueing musicians one at a time.

Meet the parts

Runs your sketch

Arduino

Brain

Loading part…

Red light in the sequence

Red LED

Stop

Loading part…

Yellow light in the sequence

Yellow LED

Caution

Loading part…

Green light in the sequence

Green LED

Go

Loading part…

Protects the red LED

Red resistor

Safety

Loading part…

Protects the yellow LED

Yellow resistor

Safety

Loading part…

Protects the green LED

Green resistor

Safety

Loading part…

How it works

1

Red light

Turn every light off first, then only red stays on for 2 seconds.

allOff();
digitalWrite(PIN_RED, HIGH);
delay(2000);
2

Yellow light

Red goes off; yellow warns for a shorter 800 ms.

allOff();
digitalWrite(PIN_YELLOW, HIGH);
delay(800);
3

Green light

Yellow off, green on — then loop() repeats from red again.

allOff();
digitalWrite(PIN_GREEN, HIGH);
delay(2000);

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!

    Loading part…
  2. 2

    Place Red LED

    Place the Red LED (ledRed) on the breadboard.

    Loading part…
  3. 3

    Place Yellow LED

    Place the Yellow LED (ledYellow) on the breadboard.

    Loading part…
  4. 4

    Place Green LED

    Place the Green LED (ledGreen) on the breadboard.

    Loading part…
  5. 5

    Place Red resistor

    Place the Red resistor (rRed) on the breadboard.

    Loading part…
  6. 6

    Place Yellow resistor

    Place the Yellow resistor (rYellow) on the breadboard.

    Loading part…
  7. 7

    Place Green resistor

    Place the Green resistor (rGreen) on the breadboard.

    Loading part…
  8. 8

    Connect Arduino pin 13 to Red resistor (rRed) 2

    Connect Arduino pin 13 to Red resistor (rRed) 2.

  9. 9

    Connect Red resistor (rRed) 1 to Red LED (ledRed) anode (+)

    Connect Red resistor (rRed) 1 to Red LED (ledRed) anode (+).

  10. 10

    Connect Red LED (ledRed) cathode (-) to Arduino GND

    Connect Red LED (ledRed) cathode (-) to Arduino GND.

  11. 11

    Connect Arduino pin 12 to Yellow resistor (rYellow) 2

    Connect Arduino pin 12 to Yellow resistor (rYellow) 2.

  12. 12

    Connect Yellow resistor (rYellow) 1 to Yellow LED (ledYellow) anode (+)

    Connect Yellow resistor (rYellow) 1 to Yellow LED (ledYellow) anode (+).

  13. 13

    Connect Yellow LED (ledYellow) cathode (-) to Arduino GND

    Connect Yellow LED (ledYellow) cathode (-) to Arduino GND.

  14. 14

    Connect Arduino pin 11 to Green resistor (rGreen) 2

    Connect Arduino pin 11 to Green resistor (rGreen) 2.

  15. 15

    Connect Green resistor (rGreen) 1 to Green LED (ledGreen) anode (+)

    Connect Green resistor (rGreen) 1 to Green LED (ledGreen) anode (+).

  16. 16

    Connect Green LED (ledGreen) cathode (-) to Arduino GND

    Connect Green LED (ledGreen) cathode (-) to Arduino GND.

Try it

  • Run the sim — only one LED should glow at a time.
  • Watch the order: red, yellow, green, then repeat.

Peek at code

Three output pins

const int PIN_RED = 13;
const int PIN_YELLOW = 12;
const int PIN_GREEN = 11;

Each color has its own pin number at the top. Names like PIN_RED make the code easier to read.

allOff() helper

void allOff() {
  digitalWrite(PIN_RED, LOW);
  digitalWrite(PIN_YELLOW, LOW);
  digitalWrite(PIN_GREEN, LOW);
}

Before each new color, every light is turned LOW so only one shows at a time.

Traffic sequence

void loop() {
  allOff();
  digitalWrite(PIN_RED, HIGH);
  delay(2000);
  allOff();
  digitalWrite(PIN_YELLOW, HIGH);
  delay(800);
  allOff();
  digitalWrite(PIN_GREEN, HIGH);
  delay(2000);
}

loop() runs red → yellow → green in order. delay() sets how long each color stays on.

Show full sketch (traffic-light.ino)
const int PIN_RED = 13;
const int PIN_YELLOW = 12;
const int PIN_GREEN = 11;
void allOff() {
  digitalWrite(PIN_RED, LOW);
  digitalWrite(PIN_YELLOW, LOW);
  digitalWrite(PIN_GREEN, LOW);
}
void setup() {
  pinMode(PIN_RED, OUTPUT);
  pinMode(PIN_YELLOW, OUTPUT);
  pinMode(PIN_GREEN, OUTPUT);
  allOff();
}
void loop() {
  allOff();
  digitalWrite(PIN_RED, HIGH);
  delay(2000);
  allOff();
  digitalWrite(PIN_YELLOW, HIGH);
  delay(800);
  allOff();
  digitalWrite(PIN_GREEN, HIGH);
  delay(2000);
}

Quick quiz

Q1. Where does repeating work belong?

  • A. loop()
  • B. setup()
  • C. pinMode only
Why: Correct—loop() runs again and again.

Q2. Why turn the other lights OFF before turning one ON?

  • A. Only one color should show at a time
  • B. To save pin numbers
  • C. Because delay() requires it
Why: Correct — sequencing means one active output at each step.

Code lab — try on your own

  1. Make red stay on longer — change its delay(2000) to delay(3000).

    Hint: Find the delay right after digitalWrite(PIN_RED, HIGH).

  2. Make yellow faster — change delay(800) to delay(400).

    Hint: The yellow delay is on the line after PIN_YELLOW turns on.

Code walkthrough

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

Program overview

Technical

Sketches have globals, then setup() once, then loop() forever.

In this project

Cycle red, yellow, and green LEDs like a mini traffic signal.

Why here

Read from top to bottom. Hover words or lines for help!

const int PIN_RED = 13;
const int PIN_YELLOW = 12;
const int PIN_GREEN = 11;
void allOff() {
  digitalWrite(PIN_RED, LOW);
  digitalWrite(PIN_YELLOW, LOW);
  digitalWrite(PIN_GREEN, LOW);
}

setup()

Technical

Runs one time when the board turns on.

In this project

Sets up pins and libraries for Traffic Light.

Why here

One-time setup belongs here—not in loop().

void setup() {
  pinMode(PIN_RED, OUTPUT);
  pinMode(PIN_YELLOW, OUTPUT);
  pinMode(PIN_GREEN, OUTPUT);
  allOff();
}

loop()

Technical

Runs again and again after setup() is done.

In this project

This is the main action you see in Traffic Light.

Why here

Repeating work (blink, read sensors) goes here.

void loop() {
  allOff();
  digitalWrite(PIN_RED, HIGH);
  delay(2000);
  allOff();
  digitalWrite(PIN_YELLOW, HIGH);
  delay(800);
  allOff();
  digitalWrite(PIN_GREEN, HIGH);
  delay(2000);
}

Try this: Change numbers in loop(), then compile and run the simulator.

pinMode

Technical

Tells a pin if it listens or drives something.

In this project

Gets the Traffic Light circuit ready in the simulator.

Why here

Goes in setup() because we only set pins once at the start.

  pinMode(PIN_RED, OUTPUT);

digitalWrite

Technical

Turns a pin ON or OFF.

In this project

Controls lights, motors, or buzzers in Traffic Light.

Why here

Goes in loop() so it can keep changing while the program runs.

  digitalWrite(PIN_RED, LOW);

delay

Technical

Waits for some time. Nothing else runs during the wait.

In this project

Controls speed so you can see Traffic Light in the simulator.

Why here

Right after an action that should stay the same for a moment.

  delay(2000);