Lights and displaysPedestrian Crossing with a Button
Cars have green until a pedestrian presses the button. Then the cars get red, the pedestrians get green and a buzzer ticks, just like at a real crossing.
Lights and displaysBuild a small model of a real traffic light: green, flashing green, yellow, red and finally red with yellow, just like on the street.
Written by the RoboHub team. A question? Write to us.
Choose a variation and the parts list updates.
On the breadboard, place the red, yellow and green LEDs from top to bottom. The LED's long leg is “+”, the short one is “−”.
Connect each LED's long leg through a 330 Ω resistor: red to pin 4, yellow to pin 3, green to pin 2. Take the short legs to the breadboard's ground rail and connect the rail to the board's GND.
The lights() function sets all three LEDs at once, and loop() goes through the phases one after another.
const int RED = 4, YELLOW = 3, GREEN = 2;
void setup() {
pinMode(RED, OUTPUT);
pinMode(YELLOW, OUTPUT);
pinMode(GREEN, OUTPUT);
}
void lights(bool red, bool yellow, bool green) {
digitalWrite(RED, red ? HIGH : LOW);
digitalWrite(YELLOW, yellow ? HIGH : LOW);
digitalWrite(GREEN, green ? HIGH : LOW);
}
void loop() {
lights(false, false, true); delay(5000); // green
for (int i = 0; i < 3; i++) { // green flashes: about to change
lights(false, false, false); delay(500);
lights(false, false, true); delay(500);
}
lights(false, true, false); delay(2000); // yellow
lights(true, false, false); delay(5000); // red
lights(true, true, false); delay(1500); // red + yellow: get ready
}delay() takes milliseconds: 5000 is five seconds. Try keeping the red on longer than the green.

Illustration, made with AI
Illustration, from above: an LED traffic light at a play-mat crossroads, red lit, with toy cars waiting.
Step 4: Change the timing
Lights and displaysCars have green until a pedestrian presses the button. Then the cars get red, the pedestrians get green and a buzzer ticks, just like at a real crossing.
GamesAn LED lights at a random moment. Whoever presses their button first wins. Built from Student Kit parts in 45 minutes.
Lights and displaysEveryone starts here: the LED built into the board turns on and off once a second. You'll learn how to install the Arduino IDE, write code and upload it to the board.