BeginnerSensors
Ultrasonic Parking Distance Beeper
Measure nearby objects with an HC-SR04 and make a buzzer beep faster as an object gets closer.
Parts from the database
- HC-SR04 ร 1Ultrasonic distance module
- ATmega328P-PU ร 1Use on a 5 V Arduino-compatible development board
Other supplies
- 5 V Arduino-compatible development board using ATmega328PCommon build supply
- piezo buzzerCommon build supply
- breadboardCommon build supply
- jumper wiresCommon build supply
- USB data cableCommon build supply
Amazon links may be affiliate links. As an Amazon Associate, Electronics Hobby Hub earns from qualifying purchases.
Connections
- HC-SR04 VCC to 5 V and GND to GND; TRIG to D9 and ECHO to D10 on a 5 V Uno-compatible board.
- Piezo buzzer positive to D3 and negative to ground. Use a small piezo buzzer, not a loudspeaker.
- If using a 3.3 V board instead, level-shift the HC-SR04 ECHO output before connecting it to the MCU.
Build steps
- Upload the sketch and open Serial Monitor at 9600 baud.
- Place a flat target 20โ150 cm from the sensor; keep the sensor face clear.
- Confirm the displayed distance changes consistently as you move the target.
- Listen for beeps that speed up below the selected threshold; tune the thresholds for your enclosure.
Software and firmware setup
const int trigPin=9, echoPin=10, buzzerPin=3;
void setup(){ Serial.begin(9600); pinMode(trigPin,OUTPUT); pinMode(echoPin,INPUT); pinMode(buzzerPin,OUTPUT); }
void loop(){
digitalWrite(trigPin,LOW); delayMicroseconds(3);
digitalWrite(trigPin,HIGH); delayMicroseconds(10); digitalWrite(trigPin,LOW);
unsigned long us=pulseIn(echoPin,HIGH,30000);
if(us==0){ noTone(buzzerPin); Serial.println("No echo"); delay(100); return; }
float cm=us*0.0343f/2.0f; Serial.println(cm);
if(cm<100){ int interval=map((int)cm,5,100,80,700); tone(buzzerPin,2200,40); delay(max(interval,80)); }
else { noTone(buzzerPin); delay(100); }
}Test your build
A target moving from about 100 cm toward the sensor should produce progressively faster chirps. The result is a bench demonstrator, not an automotive parking safety system.
Troubleshooting
- If readings are zero, check TRIG/ECHO order and the 5 V supply.
- Soft fabric and angled surfaces reflect ultrasound poorly; test with a flat cardboard book.
- If the buzzer is silent, check whether it is an active buzzer or a passive piezo; this sketch uses tone().
Safety and limitations
This is only a tabletop demo. Do not use it to guide a real vehicle or rely on it to prevent a collision.
Manufacturer and platform documentation
Community Notes for This Project
These are unverified visitor contributions, separate from the build guide and manufacturer specifications. Verify technical advice against the linked datasheets before relying on it.
Set up the comments service to load community notes.