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
| Item✓ | Qty | ~Cost | Where to buy | |
|---|---|---|---|---|
| Arduino Uno (or Nano clone) | Arduino Uno (or Nano clone) | 1 | GHS 120.00 | Online — official Uno is ~GHS 375; a Nano clone is ~GHS 75. |
| USB cable for the board | USB cable for the board | 1 | GHS 15.00 | Probably already in the kit. |
| Red 5 mm LED | Red 5 mm LED | 1 | GHS 1.50 | Online. |
| Yellow 5 mm LED | Yellow 5 mm LED | 1 | GHS 1.50 | Online. |
| Green 5 mm LED | Green 5 mm LED | 1 | GHS 1.50 | Online. |
| 220 Ω resistors | 220 Ω resistors | 3 | GHS 6.75 | Online. |
| Half-size breadboard + jumpers | Half-size breadboard + jumpers | 1 | GHS 30.00 | Online. |
| TotalTotal | ~GHS 176.25 | |||
Build it step by step
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.
Drop a 220 Ω resistor below each LED
Resistor from f3 to −rail; same for f6 and f9. Cathodes go through the resistor to ground.
Wire the anodes to Arduino pins
a3 → D2 (red); a6 → D3 (yellow); a9 → D4 (green).
Wire ground
Arduino GND → −rail. Without this, the LEDs have no return path.
Open the code
Plug in the Arduino, open the Arduino IDE, paste the code from this page, and hit Upload.
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);
}