10 Min Read • Updated June 2026

How Ultrasonic Sensors Work: HC-SR04, Echo Timing & Arduino Distance Calculation Explained

The HC-SR04 is the most popular distance sensor in robotics and Arduino projects — it measures 2–400 cm with ±3 mm accuracy using 40 kHz sound pulses. This guide explains the physics, the math, Arduino wiring, and the accuracy tricks that most tutorials miss.

HC-SR04 ultrasonic sensor module with two transducer eyes and concentric sound wave circles showing Trig and Echo signal timing diagram
Operating Frequency

40 kHz (ultrasonic)

Measuring Range

2 cm – 400 cm

Blind Zone

< 2 cm (unreliable)

Accuracy

±3 mm at close range

Beam Angle

~15° cone

Supply Voltage

5V DC, ~15 mA

How HC-SR04 Measures Distance: Step-by-Step

Definition: An ultrasonic sensor measures distance by timing how long a 40 kHz sound burst takes to travel from the transmitter transducer to an object and back to the receiver transducer, then computing distance from the speed of sound.
  • [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]
  1. Trigger: MCU sends a 10 µs HIGH pulse on the Trig pin.
  2. Transmit: HC-SR04 fires an 8-cycle burst of 40 kHz ultrasonic sound from the TX transducer (left eye).
  3. Reflect: Sound waves travel outward in a ~15° cone, hit an object, and reflect back.
  4. Receive: RX transducer (right eye) detects the returning echo.
  5. Echo Pulse: HC-SR04 drives Echo pin HIGH from burst transmit until echo received. Pulse width = round-trip time in µs.
  6. 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:

const int trigPin = 9; const int echoPin = 10; void setup() { pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); Serial.begin(9600); } void loop() { // Send 10µs trigger pulse digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Read echo duration and calculate distance long duration = pulseIn(echoPin, HIGH); float distanceCm = duration / 58.2; Serial.print("Distance: "); Serial.print(distanceCm); Serial.println(" cm"); delay(100); }
Accuracy Tip: Take 5 median readings per measurement cycle to reject outlier spikes. Use the Kalman filter for smooth tracking in moving robotics applications.

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.

📚 References & Sources

Was this article helpful?

Tap a star to rate — no account needed

Related Resources