IntermediateArduinoARDUINO UNOHC-SR04ULTRASOUND SENSOR

HC‑SR04 Ultrasound Distance Measurement with Arduino – Parking Alert (LCD & Buzzer)

Learn HC‑SR04 ultrasound distance measurement with Arduino Uno. Build a parking alert system with 16x2 LCD and buzzer – shows distance and beeps faster as you approach.

SolderHub6/4/2026 20 min read 2 views
HC‑SR04 Ultrasound Distance Measurement with Arduino – Parking Alert (LCD & Buzzer)

Introduction

In this project we are going learn the basic of how a ultrasound sensor works ,How to display the measured value in LCD 16X2 . We also add a buzzer beeping sound when certain distance reached. That is exactly what we will build in this tutorial: a parking alert system using HC‑SR04 ultrasound distance measurement.

Using an Arduino Uno, an HC‑SR04 ultrasonic sensor, a 16x2 I2C LCD, and a buzzer, you will create a system that:

  • Measures distance from 2 cm to 400 cm using ultrasound.
  • Displays the distance on an LCD screen in real time.
  • Beeps faster as objects approach – just like a car’s parking alert.
  • Shows a “TOO CLOSE!” warning when an object is within 20 cm.

What you will learn

  • How an HC‑SR04 ultrasonic sensor measures distance using sound waves.
  • How to connect and read the sensor with pulseIn().
  • How to map a physical value (distance) to a timing interval (buzzer speed).
  • How to update the LCD without slowing down the buzzer (non‑blocking timing with millis()).

After completing this project, we will have a practical parking alert gadget and a solid foundation for obstacle‑avoiding robots, smart dustbins, and distance alarms.


Components

6 items
NameQtyLink
Arduino Uno R3×1
HC‑SR04 Ultrasonic Sensor ×1
16x2 LCD with I2C backpack ×1
Passive buzzer (5V) ×1
Jumper wires (male‑to‑female)×1
Breadboard×1

How the HC‑SR04 Ultrasound Distance Measurement Works

The HC‑SR04 works like a tiny bat or sonar. It sends out a short burst of high‑frequency sound (ultrasound) and listens for its echo. The time between sending and receiving tells us how far away an object is.

The four pins

PinNamePurpose
VCCPowerConnect to 5V
TrigTriggerSend a 10‑microsecond HIGH pulse to start measurement
EchoEchoGoes HIGH when sound is sent, stays HIGH until echo returns
GNDGroundConnect to GND

The measurement formula

Sound travels at approximately 0.034 cm per microsecond (µs) through air. The pulseIn() function measures the total round‑trip time in µs.

distance_cm = (duration_µs × 0.034) / 2

We divide by two because the sound travels to the object and back – we only need the one‑way distance.

Why the buzzer beeps faster when you get closer

We map the distance to a delay value. Closer distance → shorter delay between beeps → faster beeping. This gives a natural, intuitive parking alert.


Step‑by‑Step Wiring

We will connect all three components to the Arduino Uno. The LCD and buzzer share power and ground. The I2C LCD uses only two data pins (SDA and SCL), leaving plenty of pins free.

Pin connection table

Arduino UnoHC‑SR04I2C LCDBuzzerNotes
5VVCCVCCVCC (red wire)Use the breadboard red power rail
GNDGNDGNDGND (black wire)Use the breadboard blue ground rail
A4 (SDA)SDAYellow wire (I2C data)
A5 (SCL)SCLGreen wire (I2C clock)
Pin 9TrigBlue wire – trigger signal
Pin 8EchoPurple wire – echo signal
Pin 11+ (long leg)Signal to turn buzzer on/off

Buzzer polarity

Most passive buzzers have no polarity (they work either way). If your buzzer has a + sign, connect that side to pin 11 and the other side to GND.


Understanding the Code (Non‑Blocking Design)

The code does four things repeatedly, without ever using delay() (except for the short 50 ms beep itself). This ensures the LCD updates smoothly and the buzzer timing stays accurate.

  1. Measure distance – trigger the sensor, read the echo pulse, calculate centimeters.
  2. Map distance to beep delay – closer object → smaller delay → faster beeps.
  3. Update LCD every 200 ms, show distance and warning message.
  4. Control buzzer using millis() – beep at the calculated interval without blocking other tasks.

This non‑blocking approach is essential for any project that must handle multiple tasks simultaneously.


Arduino Code

Copy and paste the code below into your Arduino IDE. Then select Arduino Uno and the correct Port, and click Upload.

C++

Key parts of the code explained

SectionPurpose
#include <LiquidCrystal_I2C.h>Allows easy control of the I2C LCD.
map(distanceCm, 0, 400, 50, 1000)Converts a distance (0–400 cm) into a beep delay (50–1000 ms).
millis() - lastBeepTime >= beepDelayMsNon‑blocking timer – triggers a beep only when the time interval has passed.
displayInterval = 200LCD refreshes every 200 ms – fast enough for smooth updates, slow enough to avoid flicker.

Upload and Test

  1. Connect your Arduino Uno to your computer via USB.
  2. Select the correct board and port in the Arduino IDE.
  3. Click Upload.
  4. Open the Serial Monitor (optional) – set baud rate to 9600 to see debug messages.

What you should observe

Object distanceLCD first lineLCD second lineBuzzer behavior
No object (out of range)Distance: 400.0 cmSafe distanceSilent
> 100 cmDistance: 120.0 cmSafe distanceSlow, regular beeps (≈1 per second)
20–100 cmDistance: 45.0 cmSafe distanceBeeps get faster as you move closer
< 20 cmDistance: 12.0 cm>> TOO CLOSE! <<Very fast beeps (≈20 per second)

Move your hand or a book in front of the sensor and watch the LCD change and the buzzer speed increase. This is exactly how a real parking alert system behaves.

Troubleshooting

ProblemMost Likely CauseHow to Fix
LCD backlight is on but no textWrong I2C addressChange 0x27 to 0x3F in the code, or run an I2C scanner sketch.
LCD shows solid white blocksContrast potentiometerTurn the small blue screw on the back of the LCD with a screwdriver.
Distance always shows 0 cm or 400 cmSensor not triggered correctlyCheck wiring: Trig → pin 9, Echo → pin 8, VCC → 5V, GND → GND.
Buzzer stays silentWrong pin or no powerEnsure buzzer is connected to pin 11 and GND. Try flipping the buzzer wires (no polarity).
Buzzer beeps only once then stopsCode has a blocking delay() that stops everythingOur code uses a short delay(50) only inside the beep – that is fine. If you modified the code, remove long delays.
Distance readings jump around (jitter)Sensor placed on unstable surface or near reflective objectsAdd a small delay between readings, or take an average of 3–5 readings.

Extensions – Make It Your Own

Once your parking alert system works, try these upgrades to learn even more:

  1. Add an LED bar graph – Connect 3–5 LEDs to digital pins. Light up more LEDs as the object gets closer (e.g., 1 LED at 100 cm, 5 LEDs at 10 cm).
  2. Add a push button – Let the user toggle between centimeters and inches, or mute the buzzer.
  3. Change the beep frequency – Instead of just beeping faster, use tone() to make the pitch higher as you get closer.
  4. Mount the sensor on a servo – Sweep the sensor left and right to detect objects in a wider area (radar style).
  5. Log data – Connect an SD card module to record distances over time (see our SD Card Data Logger tutorial).

Conclusion

Congratulations – you have just built a fully functional parking alert system using HC‑SR04 ultrasound distance measurement with Arduino! You learned:

  • How to measure distance with an HC‑SR04 sensor using pulseIn().
  • How to map a physical value to a timing interval (distance → beep delay).
  • How to display information on an I2C LCD.
  • How to control a buzzer without blocking the rest of your code (non‑blocking design with millis()).

This project is the foundation for many others: obstacle‑avoiding robots, smart dustbins, liquid level monitors, and more. Keep experimenting, and don’t hesitate to share your build with the SolderHub community.


Related tutorials:

Happy making!

Related projects