Espurna
EN
SensorsBasicultrasonictime of flightHC-SR04distance

Ultrasonic distance sensor

In 30 secondsIt sends a pulse and measures how long the echo takes: d = c · t / 2. The speed of sound depends on temperature, so if you don't compensate for it the reading drifts.

Also available in: Català · Castellano

Time of flight

d = c · t / 2: divided by 2 because the pulse goes there and back.

  • Sound: c ≈ 343 m/s at 20 °C. At 1 m, the echo takes 2 m / 343 ≈ 5.8 ms.
  • Light: at 1 m, 2 m / 3·10⁸ ≈ 6.7 ns.

That’s why an ultrasonic sensor costs a few euros and a lidar quite a lot more: it has to measure nanoseconds.

Temperature matters

c ≈ 331.3 + 0.606 · T (m/s, T in °C). If the sensor assumes 20 °C and it’s 0 °C, the distance is off by a few centimetres per metre.

In the demo

Change the temperature and watch the error. Compare with the lidar: sound in milliseconds, light in nanoseconds.

Interactive demo

Related concepts