LED Traffic Light (Arduino)
Cycle red, yellow, and green LEDs like a traffic light. Great practice for digital outputs and timing.
Beginner20 min
What you need
Wiring
Hover a pin or a wire to trace the connection.
Code
traffic_light.ino
// Cycle three LEDs like a traffic light.
const int RED = 2, YELLOW = 3, GREEN = 4;
void setup() {
pinMode(RED, OUTPUT);
pinMode(YELLOW, OUTPUT);
pinMode(GREEN, OUTPUT);
}
void loop() {
digitalWrite(RED, HIGH); delay(3000); digitalWrite(RED, LOW); // stop
digitalWrite(GREEN, HIGH); delay(3000); digitalWrite(GREEN, LOW); // go
digitalWrite(YELLOW, HIGH); delay(1000); digitalWrite(YELLOW, LOW); // slow
}
Steps
- Wire each LED from its pin (D2/D3/D4) through a 220Ω resistor; all cathodes to GND.
- Red = D2, Yellow = D3, Green = D4.
- Upload the sketch and watch the cycle.
- Tweak the delay values to change the timing.