Illustration: on a garage floor a toy car reverses toward the ultrasonic sensor; the red LED on the breadboard is lit, with a buzzer beside it.Sensors
Illustration (made with AI): roughly what the finished project looks like.

Parking Sensor with Sound and Lights

An ultrasonic sensor measures the distance. Three LEDs show how close the obstacle is, and the buzzer beeps faster the closer you get.

Difficulty
Beginner
Time
1 h
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. Connect the sensor

    Connect the HC-SR04's VCC to 5V, GND to ground, TRIG to pin 9 and ECHO to pin 10.

    D5 is the board's digital pin 5, 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.

    • HC-SR04 sensor VCC → Arduino 5V
    • HC-SR04 sensor TRIG → Arduino D9
    • HC-SR04 sensor ECHO → Arduino D10
    • HC-SR04 sensor GND → Arduino GND
  2. LEDs and buzzer

    Connect the green, yellow and red LEDs to pins 5, 6 and 7, each through a 330 Ω resistor. The buzzer's “+” goes to pin 8.

    D5 is the board's digital pin 5, 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.

    • Green LED + → 330 ΩArduino D5
    • Green LED − → Arduino GND
    • Yellow LED + → 330 ΩArduino D6
    • Yellow LED − → Arduino GND
    • Red LED + → 330 ΩArduino D7
    • Red LED − → Arduino GND
    • Buzzer + → Arduino D8
    • Buzzer − → Arduino GND
  3. Upload the code

    The code measures the distance on every pass and lights the LEDs and beeps accordingly.

    Arduino C++
    const int TRIG = 9, ECHO = 10, BUZZER = 8;
    const int GREEN = 5, YELLOW = 6, RED = 7;
    
    void setup() {
      pinMode(TRIG, OUTPUT);
      pinMode(ECHO, INPUT);
      pinMode(GREEN, OUTPUT);
      pinMode(YELLOW, OUTPUT);
      pinMode(RED, OUTPUT);
    }
    
    long distanceCm() {
      digitalWrite(TRIG, LOW);
      delayMicroseconds(2);
      digitalWrite(TRIG, HIGH);
      delayMicroseconds(10);
      digitalWrite(TRIG, LOW);
      long us = pulseIn(ECHO, HIGH, 30000);
      return us / 58;                      // 0 = nothing in range
    }
    
    void loop() {
      long cm = distanceCm();
      bool seen = cm > 0;
      digitalWrite(GREEN,  seen && cm > 50);
      digitalWrite(YELLOW, seen && cm > 20 && cm <= 50);
      digitalWrite(RED,    seen && cm <= 20);
    
      if (seen && cm <= 50) {
        tone(BUZZER, 2000, 60);
        delay(map(cm, 0, 50, 60, 500));    // closer = faster beeps
      } else {
        delay(100);
      }
    }
  4. Try it

    Slowly bring your hand towards the sensor: at 50 cm the yellow LED lights and the beeping starts, at 20 cm the red one.

    Illustration: the finished parking sensor. The ultrasonic sensor faces a toy car and the red LED is lit.

    Illustration, made with AI

  • Home and IoT
    Beginner1 h10+

    A Smart Bin

    Bring your hand close and the lid lifts by itself. An ultrasonic sensor notices the hand, and a servo opens the lid and closes it three seconds after the hand has gone.

    5/5 parts on the site
  • Home and IoT
    Intermediate1 h12+

    A Code Lock with a Keypad

    Type the secret code and press #: with the right code the green light comes on and a servo slides the bolt open, with the wrong one the buzzer beeps three times.

    7/7 parts on the site
  • 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