TL;DR:
Distance = speed of sound × echo time ÷ 2, because the sound travels there and back.
The speed of sound changes with temperature, which slightly changes the result.
Wide beams, soft materials and angled surfaces can hide obstacles or give false readings.
A small robot rolls toward a wall and stops a hand's width away, with no camera at all. It uses a pair of small round "eyes" that are really a speaker and a microphone for sound too high for you to hear. This article shows how an ultrasonic sensor turns a short click into a distance, and why it sometimes fails.
How it measures
The sensor sends out a short burst of ultrasound, which is sound above the range of human hearing. The sound travels through the air, reflects off an object and returns. A timer measures the echo time. Because the sound went out and back, the one-way distance is half of what the sound travelled:
distance = (speed of sound × echo time) ÷ 2
Worked example: an echo after 5.8 ms means about 1 m
At room temperature, around 20 °C, sound travels at about 343 m/s. The echo returns after 5.8 ms.
Convert:
5.8 ms = 0.0058 s.Total path:
343 × 0.0058 = 1.989 m.Halve it for one way:
1.989 ÷ 2 = 0.995 m, about 1 m.
Check going the other way: sound needs 1 m ÷ 343 m/s = 2.9 ms to travel 1 m, so out and back takes 2 × 2.9 = 5.8 ms. It matches.
The reverse is a handy rule: each millisecond of echo corresponds to about 343 × 0.001 ÷ 2 = 0.17 m, so 17 cm of distance.
The speed of sound changes with temperature
Sound is faster in warm air. A common approximation is v ≈ 331.3 + 0.606 × T metres per second, with T in °C. At 20 °C that gives 343.4 m/s. At 30 °C it gives about 349.5 m/s. If your code assumes 343 m/s and the air is 30 °C, a true 5.8 ms echo means a real distance of 349.5 × 0.0058 ÷ 2 = 1.014 m instead of 0.995 m, a difference of about 2 cm. For many robots that is fine; for a precise measurement, add a temperature sensor and correct the speed.
Cone width
An ultrasonic sensor does not send a thin beam like a laser. The sound spreads in a cone, often tens of degrees wide. The sensor reports the first echo that returns, which comes from the nearest object anywhere inside the cone. A table leg near the edge of the cone can appear as if it were straight ahead, and a thin pole may be missed if it is narrower than the beam at that range, because the weak reflection is lost.
Soft and angled surfaces
Soft surfaces. Fabric, foam and curtains absorb sound, so the echo is weak or missing and the robot may think the way is clear.
Angled surfaces. A flat wall hit head-on sends the echo back to the sensor. A wall at a steep angle sends it off to the side, like a billiard ball, and nothing returns.
Small or thin objects. They reflect too little sound.
Close objects. The sensor cannot listen while it is still sending, so there is a minimum distance, often a few centimetres, below which readings are unreliable.
Other sources of error
Several ultrasonic sensors firing at the same time can hear each other. Fire them one after another.
Noisy motors or fans can add false echoes.
Echoes from earlier pulses may arrive late and be mistaken for the new one. Leave a short gap between pulses.
Tip: Take several readings and use the median. A single false echo then does not make the robot jump.
A simple stopping rule
A typical use is to stop a robot before it hits something. Suppose the robot drives at 0.3 m/s and the sensor updates 20 times a second. Between readings the robot moves 0.3 ÷ 20 = 0.015 m, or 1.5 cm. If you want it to stop 20 cm from the wall, setting the trigger at 25 cm leaves room for that 1.5 cm step, a reading error of a couple of centimetres, and the distance the robot needs to slow down.
Always test the stop with the real robot and a soft object, such as a cardboard box, before you rely on it near people or furniture. A sensor that works on a wall may miss a chair leg.
Pick the right sensor for the job
Ultrasonic sensors are cheap, simple, and work in the dark. They are a good fit for rough obstacle detection and stopping near a wall. For narrow objects, soft surfaces or accurate mapping, a LiDAR or a depth camera is better. Many robots use ultrasonic sensors as a low-cost backup.
Common mistakes
Forgetting to halve the distance.
Mixing up milliseconds and microseconds.
Mounting the sensor so that the robot's own body is inside the cone.
Trusting one reading near the limit of the range.
Your plan for this week
Point an ultrasonic sensor at a wall at 50 cm, 1 m and 2 m, measured with a ruler, and compare the readings. Repeat with a pillow in front of the wall. The difference shows you the soft-surface problem. DuckyHelper can talk through the echo-time arithmetic with you if you want to check your numbers.
Frequently asked questions
Why do I need to halve the time?
The sound travels to the object and back, so the measured path is twice the distance.
Can ultrasonic sensors see through glass?
Sound reflects off glass, so they can detect it, unlike many optical sensors. Angled glass can still deflect the echo away.
