1
Configure three outputs
pinMode() configures pins 8, 9 and 10 to drive three LEDs through separate resistors.
Set the red, green and amber light durations, assign Arduino pins and watch the simulated traffic lights change. Compare breadboard wiring with a circuit schematic.
// Experiment 02: Traffic lights const int redPin = 8; const int greenPin = 9; const int amberPin = 10; const unsigned long redTime = 5000; const unsigned long greenTime = 5000; const unsigned long amberTime = 2000; void setup() { pinMode(redPin, OUTPUT); pinMode(greenPin, OUTPUT); pinMode(amberPin, OUTPUT); } void loop() { digitalWrite(redPin, HIGH); delay(redTime); digitalWrite(redPin, LOW); digitalWrite(greenPin, HIGH); delay(greenTime); digitalWrite(greenPin, LOW); digitalWrite(amberPin, HIGH); delay(amberTime); digitalWrite(amberPin, LOW); }
The Arduino activates one output at a time. When the programmed delay finishes, it switches that LED off and activates the next. The loop() function repeats the sequence.
pinMode() configures pins 8, 9 and 10 to drive three LEDs through separate resistors.
Only the red LED is ON for 5 seconds, then it switches OFF.
Only the green LED is ON for 5 seconds, then it switches OFF.
The amber LED stays ON for 2 seconds, then the sequence restarts.
delay() calls; more advanced projects use millis() so the Arduino can respond to pedestrian buttons while timing the lights. Actual traffic signal sequences and safety interlocks vary by jurisdiction.