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.
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.
Wire the two light stacks
Amber = pin 3
Green = pin 4
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.
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
- Shorten the green time from 4000 ms to 2500 ms.
- Increase the all-red pause to two seconds.
- Add a start-up sequence where both amber LEDs flash twice before normal operation.
- 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.