Car parking sensor detects the distance between car and the obstacles around it. A buzzer beeps faster or slower depending on whether the car is close or away from the obstacle to provide perception to the driver. We can design a similar system using a ultrasonic range sensor and an active buzzer.
Ultrasonic range sensor works on the principle of echo. It sends a pulse of ultrasonic wavelength and listens for its echo. Once it detects an echo, we can calculate the distance based on amount of time the echo took to receive.
For the beeping sound, we will use active buzzer. An active buzzer has inbuilt oscillator circuit which creates a beep when supplied by a dc current. However we need the beeping interval to change depending on the distance, hence we will play with the delay function to adjust the beeps.

Components
Connections
Connect the battery block Vs pin to the Vs pin of the microcontroller block. Connect any of black (negative) pins on the battery block to one of the negative (black) pin on the microcontroller block.

Sonar block has 4 terminals. Connect the red terminal to the 5V pin and black terminal to the negative pin of the battery block. The yellow terminal with blue wire is for trigger and yellow terminal with yellow wire is for echo. Connect the trigger terminal to pin D5 & echo terminal to pin D6 of the microcontroller block.
Connect the active buzzer red terminal to microcontroller pin D3 and the black terminal to negative of microcontroller block.
Coding
/*
Makernova Robotics 2025
In this program we use an ultrasonic sensor to measure distance to obstacle and depending on the distance, we make the beeping sound of an active buzzer faster or slower
*/
#define trigger 5 // define trigger pin as D5
#define echo 6 // define echo pin as D6
#define buzzer 3 // define buzzer pin as D3
#define beep_start 100 // Distance at which beeping starts. It is set at 100cm
#define min_distance 5 //Min distance at which it beeps continuously. It is set at 5cm
#define c 0.0343 // Definition of sound speed (centimetres / microsecond)
long echotime; // Define variable for tempo
float dist; // Define variable for distance
void setup() {
pinMode(trigger, OUTPUT); // Set trigger as output
pinMode(echo, INPUT); // Set echo as input
pinMode(buzzer, OUTPUT); // Set buzzer as output
}
void loop() {
// Before measurement, the trigger is set to low level
digitalWrite(trigger, LOW);
delayMicroseconds(5);
// Send one pulse (trigger goes high level for 10 microseconds)
digitalWrite(trigger, HIGH);
delayMicroseconds(10);
digitalWrite(trigger, LOW);
// Reading echo by using pulseIn function, which returns the duration of the impulse
//(in microseconds). The acquired data is then divided by 2 since the pulse travels from
// and to the sensor
echotime = pulseIn(echo, HIGH) / 2;
dist = echotime * c; // convert time to distance
if (dist < beep_start) { // if the distance is less than 1 meter
tone(buzzer, 1000); //creates a beep of frequency 1000Hz
delay(40);
// if distance is less than min level, below condition is not true resulting in continuous beep
// for 1 meter distance, the delay between beeps is 400ms. At 5cm, delay reduces to 20ms
if (dist > min_distance) { // check if the distance is between min and max distance
noTone(buzzer); // stop the beep and wait for amount proportional to distance
delay(dist * 4); // Shorter the distance shorter is the delay for not beeping
}
}
// Waits 50 milliseconds before another measurement
delay(50);
}


