Robotics from zero · Lab
How does an ultrasonic sensor measure distance?
It sends a short burst of sound too high to hear and times the echo. The sound travels to the object and back, so the distance is the speed of sound × the time ÷ 2. Air temperature, soft surfaces and angles change the result.
A lab for Chapter 3: explore distance measurement, noise and error. Return to the parent chapter →
Sensor reads 99.88 cm
- Echo time
- 5,824 µs
- Real distance
- 100 cm
- Error
- -0.12 cm
Five readings
- 100.18 cm
- 99.68 cm
- 111.88 cmspike
- 99.88 cm
- 99.48 cm
Median: 99.88 cm
This is a model. Its numbers explain an idea. They are not safety settings for a real robot.
Under the hood
The sensor cannot see. It only knows how long the sound took. Everything else is arithmetic.
distance = speed of sound × echo time ÷ 2
- Speed of sound: about 331.3 + 0.606 × T metres per second in dry air, where T is the temperature in °C.
- Echo time: from the moment the pulse leaves until the echo returns, in microseconds (µs). One µs is a millionth of a second.
- ÷ 2: the sound travels to the object and back, so it covers the distance twice.
Worked example with the model's starting values
- Speed of sound at 20 °C: 331.3 + 0.606 × 20 = 343.42 m/s
- Echo from 100 cm: 2 × 1 m ÷ 343.42 m/s = 5,824 µs
- The code assumes 343 m/s: 343 × 5,824 µs ÷ 2 = 99.88 cm
- Error: -0.12 cm, because the code assumes a slightly different speed.
What cold air does
At 0 °C sound travels at 331.3 m/s, so the echo from 100 cm takes longer. Code that still assumes 343 m/s reads 103.53 cm, 3.53 cm too far.
Why the median, not the average
Five readings of the same object: 100.18 · 99.68 · 111.88 · 99.88 · 99.48 cm. One echo bounced off something else. The average is 102.22 cm, pulled up by the spike. The median, the middle value, is 99.88 cm.
Real-world catch
- Soft materials such as cloth and foam absorb sound. Their echo can be too weak to detect.
- An angled surface reflects the pulse away from the sensor, like light off a tilted mirror. The sensor may get no echo at all.
- The beam is a cone, not a line. The HC-SR04 datasheet gives a measuring angle of about 15°, so a thin object at the side can be missed.
- Two ultrasonic sensors close together can hear each other's pulses and report wrong distances.
Check yourself
Predict first, then open the answer. Nothing is graded.
At 20 °C the echo takes 2,912 µs. About how far away is the object?
Show the answer
343.42 m/s × 2,912 µs ÷ 2 is about 50 cm. Without the ÷ 2 you would get twice the real distance.
The air cools from 20 °C to 0 °C. The code still assumes 343 m/s. Does it read too near or too far?
Show the answer
Too far. Sound is slower in cold air, so the echo arrives later and the code turns the longer time into a longer distance: 103.53 cm instead of 100 cm.
The sensor faces a cushion 150 cm away and returns nothing. Is the sensor broken?
Show the answer
Probably not. A soft surface absorbs much of the sound. In this model a soft cloth returns an echo only up to 100 cm. Try it in the model.
From model to hardware
Reading this kind of sensor takes a few lines. This is logic to read, not wiring instructions.
trigger_pulse(10) # start a measurement: a 10 µs pulse
t = echo_high_time_us() # how long the echo pin stays high
d = 343 * t / 1e6 / 2 * 100 # d = v × t ÷ 2, in cm
d = median(last_5_readings(d)) # ignore a single spike
The HC-SR04 datasheet describes the steps: a trigger pulse of at least 10 µs, a burst of 8 ultrasonic cycles at 40 kHz, then an echo pulse as long as the sound's round trip. It gives a range of 2 cm to 400 cm.
The datasheet's own formula uses 340 m/s. This lesson uses 343 m/s, the value for about 20 °C. The 1% difference is the same kind of error as in the cold-air example.
Wait more than 60 ms between measurements, as the datasheet suggests, so an old echo is not mistaken for a new one.
Terms in this lesson
- Ultrasound
- Sound above about 20 kHz, too high for people to hear.
- Echo
- Sound that bounces back from a surface.
- Time of flight
- Measuring distance by timing how long a signal takes to travel.
- Speed of sound
- How fast sound moves through air. It rises with temperature.
- Median
- The middle value of sorted readings. A single spike does not move it.
- Measurement cycle
- The time between one reading and the next.
Return to the parent chapter →