Ultrasonic parking sensor

Build a parking sensor that beeps faster as an object gets closer, just like the one in a car.
  • Level:Beginner
  • Duration: 45 min
  • Ages 10+
  • Platform: Arduino
Community project EEMENTOR team

Components used

Ultrasonic parking sensor – EEMENTOR STEM project

Cars beep faster and faster as you reverse towards a wall. In this project you build the same thing: an ultrasonic sensor measures the distance, and a buzzer beeps more quickly the closer something gets. Below 10 cm it sounds a constant warning tone.

What you need

  • UNO R3 board and USB cable (UNO R3 Starter Kit)
  • Ultrasonic sensor HC-SR04 (UNO R3 Starter Kit)
  • Active buzzer module (45-in-1 Sensor Kit)
  • Breadboard and jumper wires

Wiring

Breadboard wiring diagram: Ultrasonic parking sensor
Breadboard view. Check each connection against the wiring table below.
From To
Sensor VCC 5V (breadboard + rail)
Sensor Trig UNO pin 9
Sensor Echo UNO pin 10
Sensor GND GND (breadboard − rail)
Buzzer S UNO pin 8
Buzzer + (middle) 5V (breadboard + rail)
Buzzer − GND (breadboard − rail)
UNO 5V and GND Breadboard + and − rails

Build it step by step

  1. Push the ultrasonic sensor and the buzzer module into the breadboard, as in the diagram.
  2. Connect the UNO's 5V and GND to the breadboard's power rails.
  3. Wire both modules to the rails and to pins 8, 9 and 10.
  4. Upload the code and open the Serial Monitor at 9600 baud.
  5. Move your hand slowly towards the sensor and listen to the beeps speed up.

The code

const int TRIG_PIN = 9;
const int ECHO_PIN = 10;
const int BUZZER_PIN = 8;

const int WARNING_CM = 10;  // constant tone below this distance
const int RANGE_CM = 100;   // silent above this distance

float readDistanceCm() {
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  long duration = pulseIn(ECHO_PIN, HIGH, 30000);
  if (duration == 0) return -1;      // nothing in range
  return duration * 0.0343 / 2;
}

void setup() {
  Serial.begin(9600);
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  pinMode(BUZZER_PIN, OUTPUT);
}

void loop() {
  float distance = readDistanceCm();
  Serial.println(distance);

  if (distance < 0 || distance > RANGE_CM) {
    digitalWrite(BUZZER_PIN, LOW);   // far away: silent
    delay(100);
    return;
  }

  if (distance < WARNING_CM) {
    digitalWrite(BUZZER_PIN, HIGH);  // very close: constant tone
    delay(100);
    return;
  }

  // The closer the object, the shorter the pause between beeps
  int pause = map((int)distance, WARNING_CM, RANGE_CM, 60, 600);
  digitalWrite(BUZZER_PIN, HIGH);
  delay(50);
  digitalWrite(BUZZER_PIN, LOW);
  delay(pause);
}

How it works

The sensor sends an ultrasonic pulse and times the echo. The sketch turns that time into centimetres, then uses map() to turn the distance into a pause: 100 cm gives a 600 ms pause between beeps, 10 cm gives only 60 ms. Below 10 cm the buzzer stays on, and above 100 cm it stays quiet.

Make it your own

  • Change WARNING_CM and RANGE_CM to suit a garage, a desk or a robot.
  • Add an LED that lights up together with the buzzer.
  • Mount the sensor on a toy car to make a reversing alarm.

Troubleshooting

  • It beeps all the time: the sensor may be seeing the table. Point it slightly upwards, at a clear area.
  • No sound at all: check that you used the active buzzer, and try swapping HIGH and LOW if your module turns on with LOW.

Learn the basics first

New to these parts? These lessons explain them step by step: Measure distance with an ultrasonic sensor and Make a tilt alarm with a buzzer.

0 comments

Leave a comment

Please note, comments need to be approved before they are published.

Build it yourself

Get the parts used in this project, tested by our engineers.

UNO R3 Starter Kit | Arduino IDE Compatible | Free Tutorials $48.00

Shop the parts

More projects

  • Pulse visualiser

    • Level:Intermediate
    • Duration: 45 min
    • Platform: Arduino

    by EEMENTOR team

  • Flame alarm

    • Level:Beginner
    • Duration: 40 min
    • Platform: Arduino

    by EEMENTOR team

Community projects

Built something awesome? Share it.

Send us your project and we'll feature it in the hub, credited to you, so other makers can learn from it.

  1. Sign in and tell us about your build
  2. Our team reviews your submission
  3. We publish it in the hub, credited to you