What Is the HC-SR04? Ultrasonic Distance Sensor: Calculation and Uses
The HC-SR04 is a popular ultrasonic distance sensor. A 10 µs pulse is sent to Trig, the sensor emits sound; the Echo pin stays high for the echo time. Distance = time × 0.0343 / 2 (cm). It measures ~2–400 cm at ~3 mm resolution; it is 5 V and its Echo output may need a level shifter on a 3.3 V MCU.
The HC-SR04 is a popular ultrasonic distance sensor that measures distance with ultrasonic waves. Being cheap and easy to use, it is a standard part in robotics and obstacle detection. For the concept, see what is a sensor.
What is the HC-SR04 used for?
Obstacle/distance detection, parking sensors, water level, robot navigation. A short pulse is sent to the Trig pin, the sensor emits ultrasound; when the echo returns, the Echo pin stays high for the echo time. Distance is computed from this time.
HC-SR04 pinout (4 pins)
| Pin | Function |
|---|---|
| VCC | +5 V |
| Trig | Trigger (10 µs pulse) |
| Echo | Echo (duration = distance) |
| GND | Ground |
How is distance calculated?
Measure how long Echo stays high (µs). The speed of sound in air is ~343 m/s: Distance (cm) = time (µs) × 0.0343 / 2 (divided by two because it is a round trip).
Highlights
| Parameter | Value |
|---|---|
| Range | ~2 – 400 cm |
| Resolution | ~3 mm |
| Supply | 5 V |
Limitations
- Soft/angled surfaces do not reflect sound → wrong readings.
- Temperature affects the speed of sound (correction needed for precise measurement).
- The Echo output is 5 V; a 3.3 V MCU may need a level shifter.
Equivalents and alternatives
- HC-SR04P / RCWL-1601: 3.3 V-compatible versions.
- VL53L0X: laser (ToF) distance, more precise.
- GP2Y0A21 (Sharp): infrared distance.
Find it in our catalog
Use the HC-SR04 search.