Ultrasonic Distance Calculator (HC-SR04)

Convert HC-SR04 echo time to distance (cm, m, inch) and back, corrected for air temperature, with the Arduino pulseIn() constant and timeout.

  • Free, no signup
  • Works on mobile
  • Formula + worked example

How to use the ultrasonic distance calculator

  1. 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.
  2. 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.
  3. 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.
  4. 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

v (m/s) = 331.3 + 0.606 × T(°C) v (cm/µs) = v (m/s) ÷ 10,000 distance (cm) = echo time (µs) × v (cm/µs) ÷ 2 echo time (µs)= 2 × distance (cm) ÷ v (cm/µs)

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 temperatureSpeed of soundcm per µsDivisor for cmDivisor for inch
0 °C331.3 m/s0.0331360.37153.3
15 °C340.4 m/s0.0340458.76149.2
20 °C343.4 m/s0.0343458.24147.9
25 °C346.5 m/s0.0346557.73146.6
30 °C349.5 m/s0.0349557.23145.4
35 °C352.5 m/s0.0352556.74144.1
40 °C355.5 m/s0.0355556.25142.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 an unsigned long. Store it in an unsigned 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.

Frequently asked questions

How do I convert HC-SR04 echo time to distance?+

Multiply the echo time in microseconds by the speed of sound in cm/µs and divide by 2. At 20 °C that is distance (cm) = duration × 0.0343 ÷ 2, or simply duration ÷ 58.24.

Why do Arduino sketches divide the duration by 58?+

58 is the round-trip time in microseconds for 1 cm at about 20 °C: 2 ÷ 0.0343 ≈ 58.3. Dividing by it turns the echo time straight into centimetres. For inches the divisor is about 148.

Does temperature affect ultrasonic distance readings?+

Yes. The speed of sound is about 331.3 + 0.606 × T m/s, so it rises roughly 0.18% per °C. Between 15 °C and 40 °C that is about 4.5% — around 9 cm of error at 2 m if you ignore it.

What timeout should I use with pulseIn() for the HC-SR04?+

Use the round-trip time for your maximum distance plus about 1 ms. For 400 cm at 20 °C the echo takes 23,295 µs, so a timeout of 25,000 µs works. pulseIn() returns 0 if the timeout passes without an echo.

What is the range of the HC-SR04 ultrasonic sensor?+

It is rated for about 2 cm to 400 cm. Closer than 2 cm the echo returns while the sensor is still transmitting. Far away, soft, small or angled objects may not reflect enough sound to be detected.

From the Thinking Robot store

Shop all
All tools
WhatsApp