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:
Temperature Compensation Physics & Speed of Sound
The common formula distance = duration / 58.2 assumes a fixed speed of sound of 343.2 m/s at exactly 20°C (68°F). However, the speed of sound through dry air varies with temperature according to the thermodynamic ideal gas equation:
In outdoor robotics operating at -10°C, sound slows down to 325 m/s (causing a 5.3% distance error). In a summer ambient temperature of 40°C, sound speeds up to 355 m/s. Interfacing a digital temperature sensor like the DS18B20 or BMP280 with your microcontroller allows dynamic recalculation of the acoustic propagation constant, boosting range accuracy to ±1 mm across thermal extremes.
Acoustic Reflection Constraints: Angle of Incidence and Blind Zones
Ultrasonic transducers produce mechanical pressure waves rather than electromagnetic photons. When sound strikes a flat target at an incident angle greater than 15° to 20° relative to normal, specular reflection bounces the acoustic energy away from the receiver transducer, causing complete echo loss and timeout errors.
Furthermore, the HC-SR04 exhibits a 2 cm to 3 cm physical blind zone. During transmission of the 8-cycle 40 kHz burst (which lasts approximately 200 µs), the piezoelectric crystal continues ringing mechanically. If an obstacle is closer than 3 cm, the return echo strikes the transducer while it is still mechanically ringing down from transmission, making the receiver unable to discriminate the echo from internal transducer excitation.
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.
