Illustration: car and pedestrian lights on a breadboard with a button and a buzzer, and a zebra crossing with a toy car and figure.Lights and displays
Illustration (made with AI): roughly what the finished project looks like.

Pedestrian 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.

Difficulty
Beginner
Time
45 min
Age
10+
Parts
6, 6 on the site

Written by the RoboHub team. A question? Write to us.

Variations

Choose a variation and the parts list updates.

Instructions

  1. The car lights

    Connect the red, yellow and green LEDs through 330 Ω resistors to pins 4, 3 and 2, the same way as in “A Traffic Light with Three LEDs”.

    D2 is the board's digital pin 2, GND is ground. Red wires are power, black ground, the other colours signals (on a real servo the ground wire is often brown). In the steps, what a step adds is in colour and what is already connected in grey.

    • Cars: red + → 330 ΩArduino D4
    • Cars: red − → Arduino GND
    • Cars: yellow + → 330 ΩArduino D3
    • Cars: yellow − → Arduino GND
    • Cars: green + → 330 ΩArduino D2
    • Cars: green − → Arduino GND
  2. The pedestrian lights

    Connect the pedestrian red LED to pin 6 and the green one to pin 5, again through resistors.

    D2 is the board's digital pin 2, GND is ground. Red wires are power, black ground, the other colours signals (on a real servo the ground wire is often brown). In the steps, what a step adds is in colour and what is already connected in grey.

    • Pedestrians: red + → 330 ΩArduino D6
    • Pedestrians: red − → Arduino GND
    • Pedestrians: green + → 330 ΩArduino D5
    • Pedestrians: green − → Arduino GND
  3. Button and buzzer

    One leg of the button goes to pin 7, the other to ground. On a four-leg button use two diagonally opposite legs, so you can't get it wrong. No resistor needed: the code switches on the board's internal one (INPUT_PULLUP). Connect the buzzer's “+” to pin 8 and “−” to ground.

    D2 is the board's digital pin 2, GND is ground. Red wires are power, black ground, the other colours signals (on a real servo the ground wire is often brown). In the steps, what a step adds is in colour and what is already connected in grey.

    • Button 1 → Arduino D7
    • Button 2 → Arduino GND
    • Buzzer + → Arduino D8
    • Buzzer − → Arduino GND
  4. Upload the code

    The car green always lasts at least eight seconds: press the button earlier and the light waits until those eight seconds are up.

    Arduino C++
    const int CAR_RED = 4, CAR_YELLOW = 3, CAR_GREEN = 2;
    const int WALK_RED = 6, WALK_GREEN = 5;
    const int BUTTON = 7, BUZZER = 8;
    
    void setup() {
      for (int pin = 2; pin <= 6; pin++) pinMode(pin, OUTPUT);
      pinMode(BUTTON, INPUT_PULLUP);       // pressed = LOW, no resistor needed
      pinMode(BUZZER, OUTPUT);
    }
    
    void cars(bool red, bool yellow, bool green) {
      digitalWrite(CAR_RED, red ? HIGH : LOW);
      digitalWrite(CAR_YELLOW, yellow ? HIGH : LOW);
      digitalWrite(CAR_GREEN, green ? HIGH : LOW);
    }
    
    void walkers(bool green) {
      digitalWrite(WALK_GREEN, green ? HIGH : LOW);
      digitalWrite(WALK_RED, green ? LOW : HIGH);
    }
    
    void loop() {
      cars(false, false, true);            // cars drive...
      walkers(false);
      unsigned long greenSince = millis();
      while (digitalRead(BUTTON) == HIGH) delay(10);   // ...until someone presses the button
    
      tone(BUZZER, 1000, 100);             // "request received"
      while (millis() - greenSince < 8000) delay(10);  // cars keep at least 8 s of green
      cars(false, true, false);  delay(2000);          // yellow
      cars(true, false, false);  delay(1000);          // red
    
      walkers(true);                       // pedestrians cross
      for (int i = 0; i < 20; i++) {       // 10 seconds of ticking
        tone(BUZZER, 1500, 40);
        delay(500);
      }
      for (int i = 0; i < 5; i++) {        // green flashes: hurry up
        digitalWrite(WALK_GREEN, LOW);   delay(300);
        digitalWrite(WALK_GREEN, HIGH);  delay(300);
      }
      walkers(false);
      delay(1000);
      cars(true, true, false);  delay(1500);           // red + yellow, then green again
    }
  5. Try it

    Press the button: the buzzer beeps once, the cars get yellow and then red, and the pedestrians get green. After ten seconds the green starts flashing, and then it all starts again.

  • Games
    Beginner45 min10+

    A Two-Player Reaction Game

    An LED lights at a random moment. Whoever presses their button first wins. Built from Student Kit parts in 45 minutes.

    6/6 parts on the site
  • Lights and displays
    Beginner30 min8+

    A Colour-Changing Lamp with an RGB LED

    An RGB LED holds three small LEDs: red, green and blue. Mix their brightness and you get any colour, while the lamp slowly changes from one colour to the next.

    5/5 parts on the site
  • Sensors
    Beginner25 min8+

    A Light That Turns On by Itself in the Dark

    A photoresistor measures the light. When the room gets dark the LED turns on by itself and goes off again in daylight, just like street lighting.

    6/6 parts on the site