How to use the ultrasonic distance calculator
- Choose Echo time → distance if you have a number from
pulseIn()and want centimetres, or Distance → echo time to see how long the echo will be for a given distance. - Enter the air temperature in °C. Sound travels faster in warm air, so a 35 °C afternoon in May gives a different reading from a 15 °C winter morning.
- Set the maximum distance your robot cares about. The calculator gives you a
pulseIn()timeout that is long enough for that distance and no longer. - Copy the ready Arduino code. The constant and timeout are already filled in with your numbers.
How the HC-SR04 measures distance
The HC-SR04 has two round "eyes": T sends sound and R listens. When the TRIG pin gets a 10 µs high pulse, the module sends a burst of eight 40 kHz pulses (too high for us to hear). It then holds the ECHO pin high until the reflected sound comes back. The length of that ECHO pulse is the time the sound took to reach the object and return.
Formula
The ÷ 2 is there because the sound travels to the object and back. Many Arduino sketches use duration / 58 — that "58" is simply 2 ÷ 0.0343, the round-trip time in µs for 1 cm at about 20 °C.
| Air temperature | Speed of sound | cm per µs | Divisor for cm | Divisor for inch |
|---|---|---|---|---|
| 0 °C | 331.3 m/s | 0.03313 | 60.37 | 153.3 |
| 15 °C | 340.4 m/s | 0.03404 | 58.76 | 149.2 |
| 20 °C | 343.4 m/s | 0.03434 | 58.24 | 147.9 |
| 25 °C | 346.5 m/s | 0.03465 | 57.73 | 146.6 |
| 30 °C | 349.5 m/s | 0.03495 | 57.23 | 145.4 |
| 35 °C | 352.5 m/s | 0.03525 | 56.74 | 144.1 |
| 40 °C | 355.5 m/s | 0.03555 | 56.25 | 142.9 |
Choosing the pulseIn() timeout
By default pulseIn() waits up to one second for an echo. If nothing reflects, your robot freezes for that whole second. Give it a timeout instead: the round-trip time for your maximum distance, plus a small margin for the burst and the module's start-up delay.
For a robot that only cares about obstacles within 200 cm at 30 °C: 2 × 200 ÷ 0.034948 = 11,446 µs. Adding about 1 ms and rounding up gives 13,000 µs. If pulseIn() returns 0, treat it as "nothing in range".
Rated range and accuracy
The HC-SR04 is rated for about 2 cm to 400 cm. Below 2 cm the echo returns while the sensor is still sending its burst. Far away, the echo becomes weak — soft cloth, foam, thin chair legs and surfaces at an angle can disappear long before 4 m. Hard, flat objects facing the sensor give the best results.
Common mistakes
- Forgetting to divide by 2. The echo time covers the trip there and back, so without ÷ 2 every distance comes out double.
- Ignoring temperature. Between 15 °C and 40 °C the speed of sound changes by about 4.5%, which is around 9 cm at 2 m.
- Using int for duration.
pulseIn()returns anunsigned long. Store it in anunsigned long. - No timeout. Without the third argument to
pulseIn(), an empty room stalls your loop for a full second. - Blocking the sensor. Wires or the robot's own chassis in front of the sensor create false echoes. Mount it at the very front, pointing level.
Build a complete obstacle-avoiding robot in our HC-SR04 ultrasonic sensor guide. Choosing a board? Read Arduino vs ESP32 vs Raspberry Pi Pico. To drive the motors or a scanning servo, try the servo pulse width calculator and the wheel RPM and speed calculator, or pick up a ready robot car from our kits.





