Arduino Traffic Light Project Code
mediumDigital outputArduino (C++)
Three LEDs in a fixed sequence — a classic PR-4 question that tests timing and order.
The question
Red LED on pin 11, yellow on pin 3, green on pin 5. Show green for 3000 ms, then yellow for 1000 ms, then red for 5000 ms, and repeat. Only one LED should be on at a time.
The sketch
sketch.ino
void setup() {
pinMode(11, OUTPUT);
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
}
void loop() {
digitalWrite(5, HIGH);
delay(3000);
digitalWrite(5, LOW);
digitalWrite(3, HIGH);
delay(1000);
digitalWrite(3, LOW);
digitalWrite(11, HIGH);
delay(5000);
digitalWrite(11, LOW);
}Wiring
Each LED → 220Ω → its pin (11 red, 3 yellow, 5 green); cathodes → GND.
How it works
- 1All three pins are set as OUTPUT.
- 2Green is on, then off; yellow is on, then off; red is on, then off — each for its own delay.
- 3Turning each LED off before the next one comes on keeps only one light on at a time.
Common mistakes
- Forgetting to turn the previous LED off, so two lights stay on.
- Mixing up the pins, so the colours appear in the wrong order.
Now solve a similar question yourself
A new Arduino question every time, checked instantly.
