How HC-SR04 Measures Distance: Step-by-Step
- [HC-SR04 Trig pin] [triggers] [8-cycle 40 kHz ultrasonic burst]
- [Echo pin pulse width] [represents] [round-trip sound travel time in microseconds]
- [Distance (cm)] [equals] [echo duration (µs) divided by 58]
- Trigger: MCU sends a 10 µs HIGH pulse on the Trig pin.
- Transmit: HC-SR04 fires an 8-cycle burst of 40 kHz ultrasonic sound from the TX transducer (left eye).
- Reflect: Sound waves travel outward in a ~15° cone, hit an object, and reflect back.
- Receive: RX transducer (right eye) detects the returning echo.
- Echo Pulse: HC-SR04 drives Echo pin HIGH from burst transmit until echo received. Pulse width = round-trip time in µs.
- Calculate: Distance (cm) = Echo duration (µs) ÷ 58
Distance (cm) = Echo (µs) ÷ 58
Speed of sound ≈ 343 m/s at 20°C. Divide by 2 for one-way trip.
Arduino Wiring & Code: HC-SR04 Distance Reading
Wire Trig to pin 9, Echo to pin 10 (with a voltage divider if using 3.3V MCU). Use pulseIn() to time the Echo pulse:
Frequently Asked Questions
How does an HC-SR04 ultrasonic sensor work?
HC-SR04 sends a 40 kHz ultrasonic burst on a 10 µs Trig pulse. When the echo returns, it outputs a HIGH pulse on the Echo pin whose duration equals the round-trip sound travel time. Distance (cm) = Echo duration (µs) / 58.
What is the formula for HC-SR04 distance calculation?
Distance (cm) = Echo (µs) ÷ 58. In mm: Distance (mm) = Echo (µs) ÷ 5.8. For temperature-corrected accuracy at T°C: Distance (cm) = Echo × (0.0331 + 0.0006 × T) / 2.
What is the maximum and minimum range of the HC-SR04?
Minimum: 2 cm (below this, echo returns before burst completes). Maximum: 400 cm (4 m) for hard flat surfaces. Practical reliable range for most objects: 3–250 cm.
Why is my HC-SR04 giving wrong readings?
Common causes: (1) Object in 2 cm blind zone, (2) angled surface deflecting sound, (3) soft/absorbing materials, (4) power supply noise (add 100µF cap), (5) Echo pin connected directly to 3.3V MCU — use a voltage divider or level shifter.
How do I wire an HC-SR04 to Arduino?
VCC → 5V, GND → GND, Trig → digital output pin, Echo → digital input pin. For 3.3V MCUs: add 1kΩ + 2kΩ voltage divider on Echo line. In code: 10µs HIGH on Trig, then pulseIn(echo, HIGH) / 58.2 = distance in cm.
Conclusion
The HC-SR04 packs a complete sonar system — ultrasonic transmitter, receiver, and timing circuit — into a module costing under a dollar. For obstacle avoidance robots, liquid level detection, parking sensors, and interactive art installations, it is the first sensor most makers should master.
