Warm air is fast air
Every ultrasonic sketch multiplies by 343 metres per second, and that figure is only right at 20 °C. In a cold garage the same wall reads several centimetres further away than it did on the bench.
How much it actually costs
Set a temperature and a distance, and read the gap.
Sound moves at about 343 m/s in dry air at 20 °C, and roughly 0.6 m/s faster for every degree above that. It is close enough to a straight line across anything this sensor is rated for that the correction is one multiply.
The error is proportional. At 2 % out, a target at 20 cm is wrong by 4 mm and a target at 3 m is wrong by 6 cm — so the same sketch is fine for a proximity switch and misleading for a tank gauge, with no change in the code.
Where it bites
A garage at 2 °C. About 3 % slower than the constant assumes, so everything reads about 3 % further away. On a 2.5 m ceiling that is 8 cm.
A car interior in summer. 50 °C air is about 5 % faster, and everything reads short by the same fraction.
A freezer. The part is rated to −40 °C, which is well inside a domestic freezer, and at −20 °C the speed is down near 319 m/s. A sketch using 343 there is out by 7 %.
A heated room in winter. Nothing at all. Twenty degrees plus or minus three is a fraction of a per cent, and correcting for it is effort spent on a number smaller than the sensor's own ±1 cm.
The fix, and when to bother
float speed = 343.0 + 0.6 * (celsius - 20.0); // m/s
float cm = (us / 2.0) * (speed / 10000.0); // us -> cm
Two lines and a thermometer. The thermometer does not need to be quick or precise — air temperature moves slowly and one degree of error is 0.2 % of distance, which is well under everything else going on.
The question is whether the project measures or compares. A door that opens under 50 cm has its threshold set in the same air the measurement happens in, so the drift cancels and the correction buys nothing. Anything that reports a distance to a person, or logs one to compare with last week's, wants it.
One more thing the library does
The supported library uses 343 m/s in its GPIO path and 340 m/s in its 1-Wire path. That is a 0.9 % disagreement between two functions of the same library — under a centimetre at three metres, and far smaller than the temperature effect on this page, but worth knowing if you ever compare two boards in two modes and find they do not quite agree.
When it does not work
The room cooled. Sound slows by about 0.6 m/s for every degree, so a reading calibrated at 20 °C reads long when the air is colder and short when it is warmer. Over a couple of metres and ten degrees that is a few centimetres, which is enough to notice and not enough to look like a fault.
Read the air temperature and compute the speed rather than using a constant: speed = 343 + 0.6 × (celsius − 20), then distance = microseconds ÷ 2 × speed, in whatever units you are working in. A DHT22 or an LM75 beside the sensor is enough, and neither needs to be fast — air temperature changes slowly.
Much less than temperature. Very humid air carries sound slightly faster, but the effect across the whole range from dry to saturated is under a per cent at room temperature — smaller than this sensor's own accuracy. Correct for temperature and ignore humidity.
Only if the project cares about absolute distance. A parking sensor or a light that comes on when you walk past is comparing against a threshold you set in the same room, so the error cancels. A tank gauge or anything reporting a measurement to a person does care, and wants a thermometer.
Five ways a working sensor looks broken, and which check separates them.
When every reading is zero →Edit this page — content/books/ultrasonic-sensor/warm-air-is-fast-air.mdx
Questions about this product
See what other owners have asked, and read their solutions.
Ultrasonic Distance Sensor
Loading discussions…
Discuss this article
Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.