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

| 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
- Push the ultrasonic sensor and the buzzer module into the breadboard, as in the diagram.
- Connect the UNO's 5V and GND to the breadboard's power rails.
- Wire both modules to the rails and to pins 8, 9 and 10.
- Upload the code and open the Serial Monitor at 9600 baud.
- 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_CMandRANGE_CMto 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