Back to projects
Tier 3 · Code the Real WorldmediumAge 10+45 min

Traffic Light

Red, yellow, green — change in the right order, just like the streets.

What you’ll learn

  • Sequencing actions with delay().
  • Driving multiple outputs from one microcontroller.
  • Thinking like a programmer — every step in order.

What you need

ItemQty~CostWhere to buy
Arduino Uno (or Nano clone)1GHS 120.00
Online — official Uno is ~GHS 375; a Nano clone is ~GHS 75.
USB cable for the board1GHS 15.00
Probably already in the kit.
Red 5 mm LED1GHS 1.50
Online.
Yellow 5 mm LED1GHS 1.50
Online.
Green 5 mm LED1GHS 1.50
Online.
220 Ω resistors3GHS 6.75
Online.
Half-size breadboard + jumpers1GHS 30.00
Online.
Total~GHS 176.25

Build it step by step

  1. Plant the three LEDs

    Red at e3-f3, Yellow at e6-f6, Green at e9-f9. Long leg to row e, short leg to row f.

  2. Drop a 220 Ω resistor below each LED

    Resistor from f3 to −rail; same for f6 and f9. Cathodes go through the resistor to ground.

  3. Wire the anodes to Arduino pins

    a3 → D2 (red); a6 → D3 (yellow); a9 → D4 (green).

  4. Wire ground

    Arduino GND → −rail. Without this, the LEDs have no return path.

  5. Open the code

    Plug in the Arduino, open the Arduino IDE, paste the code from this page, and hit Upload.

  6. Watch the cycle

    Red 3 s → green 3 s → yellow 1 s → repeat. Real intersections use this exact pattern.

How it works

Each call to digitalWrite(pin, HIGH) puts 5 V on the Arduino pin, which lights the LED. delay(ms) just waits the given number of milliseconds. The loop() function runs forever, so the sequence repeats. Change the timings, change the rhythm.

If something’s not working

One LED never lights
  • · LED is in backwards.
  • · Wrong pin number in the code or wrong row on the board.
All LEDs glow dimly all the time
  • · Forgot to set pinMode to OUTPUT in setup().

Try this next

Add a fourth LED for "walk".

Add a button to skip ahead to the next colour.

The code

Paste this into the Arduino IDE and hit Upload. Each line tells the board what to do, in order — the more lines you read, the more you understand.

// Traffic light — change the LEDs in the right order
void setup() {
  pinMode(2, OUTPUT);  // red
  pinMode(3, OUTPUT);  // yellow
  pinMode(4, OUTPUT);  // green
}

void loop() {
  digitalWrite(2, HIGH); delay(3000);  // red on for 3s
  digitalWrite(2, LOW);
  digitalWrite(4, HIGH); delay(3000);  // green on for 3s
  digitalWrite(4, LOW);
  digitalWrite(3, HIGH); delay(1000);  // yellow on for 1s
  digitalWrite(3, LOW);
}