Learn Arduino UNO · Lesson 3

Build a two-way traffic-light controller.

Six LEDs turn the simple output lessons into a recognisable control project. You will build two red-amber-green light stacks and program a safe timed sequence so only one direction receives green at a time.

45 minutes6 digital outputsTimed control sequence

What you need

  • Arduino UNO
  • Breadboard
  • 2 red LEDs, 2 amber LEDs, 2 green LEDs
  • 6 × 220–330 Ω resistors
  • Jumper wires

Goal: create a repeating two-direction traffic-light cycle with an all-red safety pause between directions.

▶ YOUTUBE DEMO PLACEHOLDERSuggested title: “Arduino Traffic Lights — 6 LED Breadboard Project”

Wire the two light stacks

Direction ARed = pin 2
Amber = pin 3
Green = pin 4
Direction BRed = pin 5
Amber = pin 6
Green = pin 7

Each output pin connects through its own resistor to an LED anode. Connect all six LED cathodes to the breadboard ground rail, then connect that rail to Arduino GND.

Safety rule for the program: never allow both green LEDs to be on together. The all-red pause makes the sequence easier to understand and mirrors the idea of clearing a junction before releasing the other direction.

The complete sequence

const int aRed = 2;
const int aAmber = 3;
const int aGreen = 4;
const int bRed = 5;
const int bAmber = 6;
const int bGreen = 7;

void setup() {
  pinMode(aRed, OUTPUT);
  pinMode(aAmber, OUTPUT);
  pinMode(aGreen, OUTPUT);
  pinMode(bRed, OUTPUT);
  pinMode(bAmber, OUTPUT);
  pinMode(bGreen, OUTPUT);
}

void allOff() {
  digitalWrite(aRed, LOW);
  digitalWrite(aAmber, LOW);
  digitalWrite(aGreen, LOW);
  digitalWrite(bRed, LOW);
  digitalWrite(bAmber, LOW);
  digitalWrite(bGreen, LOW);
}

void loop() {
  allOff();
  digitalWrite(aRed, HIGH);
  digitalWrite(bRed, HIGH);
  delay(1000);

  digitalWrite(aAmber, HIGH);
  delay(1000);
  digitalWrite(aRed, LOW);
  digitalWrite(aAmber, LOW);
  digitalWrite(aGreen, HIGH);
  delay(4000);

  digitalWrite(aGreen, LOW);
  digitalWrite(aAmber, HIGH);
  delay(1200);
  digitalWrite(aAmber, LOW);
  digitalWrite(aRed, HIGH);
  delay(1000);

  digitalWrite(bAmber, HIGH);
  delay(1000);
  digitalWrite(bRed, LOW);
  digitalWrite(bAmber, LOW);
  digitalWrite(bGreen, HIGH);
  delay(4000);

  digitalWrite(bGreen, LOW);
  digitalWrite(bAmber, HIGH);
  delay(1200);
  digitalWrite(bAmber, LOW);
  digitalWrite(bRed, HIGH);
  delay(1000);
}

Read it as a state sequence

At any moment the six outputs form one state: for example A green while B red. The delays decide how long each state lasts. We are still using the same digitalWrite() skill as the first LED lesson; the project feels more advanced because several outputs must cooperate.

This is a teaching model rather than a design for controlling real roads. Real traffic-control systems use fail-safe hardware, monitoring and regulated timing requirements.

Experiments

  1. Shorten the green time from 4000 ms to 2500 ms.
  2. Increase the all-red pause to two seconds.
  3. Add a start-up sequence where both amber LEDs flash twice before normal operation.
  4. Draw a timing chart showing which LEDs should be on during each stage before changing the code.

Challenge

Add a simple pedestrian phase using one extra LED. Decide exactly where it belongs in the sequence and make sure both traffic directions are red while the pedestrian LED is on.