Ultrasonic sensor
An ultrasonic sensor measures distance by sending out a chirp of sound too high to hear and timing how long its echo takes to come back. The sound goes there and back, so the distance is the speed of sound times the time, halved: a 1.2 ms echo means 343 × 0.0012 ÷ 2 ≈ 0.206 m.
d = v × t ÷ 2
where d is the distance, v the speed of sound, about 343 m/s in air at 20 °C, and t the echo’s time.
The popular HC-SR04 measures from about 2 cm to 4 m this way.
What is an ultrasonic sensor?
An ultrasonic sensor is a distance sensor that works like a bat or a ship’s sonar, timing an echo of sound pitched above human hearing.
The common HC-SR04 module has two round cans, one to send and one to listen, and measures from about 2 cm to 4 m. Figure 1 shows how the echo’s time grows with distance: about 5.8 ms for every meter away.
100 cm: the sound goes there and back, 200 cm, at 343 m/s, so the echo pin stays high for 2 × 1 m ÷ 343 m/s ≈ 5.83 ms.
How does an ultrasonic sensor measure distance?
An ultrasonic sensor measures distance by sending a short burst of 40 kHz sound, waiting for the echo and timing the gap; the distance is half the time multiplied by the speed of sound.
The cans are piezoelectric transducers: a voltage makes them vibrate, and a returning sound makes them produce a voltage. A chip on the module sends eight cycles of 40 kHz, then listens and signals how long the echo took.
How do you use an HC-SR04 with an Arduino?
To use an HC-SR04 with an Arduino, connect VCC to 5 V, GND to ground, and Trig and Echo to two digital pins; pulse Trig high for 10 µs, then time how long Echo stays high.
On an Arduino, pulseIn on the Echo pin returns microseconds, and dividing by 58 gives centimeters. Echo outputs 5 V, so a 3.3 V board needs a voltage divider on it, or a 3.3 V version such as the HC-SR04P. Leave about 60 ms between readings so old echoes die away.
What can’t an ultrasonic sensor see?
An ultrasonic sensor can’t see targets that send the sound elsewhere: walls tilted away from it, soft materials that absorb sound, and objects much narrower than its beam.
Sound bounces like light off a mirror, so a surface more than about 15° off square reflects the echo away, as Figure 2 shows. It also can’t see closer than about 2 cm, and two sensors chirping at once can hear each other’s echoes.
Angled wall: tilted more than about 15° off square, the wall bounces the sound away like a mirror, so no echo returns; the sensor reads nothing, or something farther away.
Does temperature affect an ultrasonic sensor?
Temperature does affect an ultrasonic sensor: sound travels about 0.6 m/s faster for every degree Celsius, so a fixed speed in the code reads slightly long in the cold and short in the heat.
Between 0 and 35 °C the error is a few percent, as Figure 3 shows. For better accuracy, measure the air’s temperature with a temperature sensor and use v = 331.3 + 0.606 × T.
0 °C: sound travels at 331.3 + 0.606 × 0 = 331.3 m/s, so code that assumes 343 m/s reads the 100 cm target as 104 cm, 3.5 cm long.
Ultrasonic or infrared distance sensor?
Ultrasonic sensors ignore color and light and reach several meters; infrared sensors are smaller and faster but depend on how much light the target reflects.
An IR obstacle sensor can miss a black object that an ultrasonic sensor sees easily, while ultrasound struggles with curtains and foam. Laser time-of-flight sensors time light instead of sound for precise readings.
How do you turn an echo into a distance?
To turn an echo into a distance, multiply the echo time by 343 m/s and halve it; with an Arduino’s microseconds, divide by 58 for centimeters.
Example 1: distance from an echo
An HC-SR04’s echo pin stays high for 5.8 ms. How far away is the object?
- Given
- an echo pulse 5.8 ms long, with sound at 343 m/s
- Find
- the distance to the object
- Solution
- d = v × t ÷ 2 = 343 m/s × 0.0058 s ÷ 2 = 0.9947 m
- Answer
- 99.5 cm
Example 2: an Arduino’s reading in centimeters
An Arduino times the echo with pulseIn and gets 2,900 microseconds. How far away is the object?
- Given
- pulseIn returning 2,900 µs from the echo pin
- Find
- the distance in centimeters
- Solution
- Sound takes about 58 µs for each centimeter there and back, 2 ÷ 0.0343 cm/µsd = 2,900 ÷ 58 = 50 cm
- Answer
- 50 cm
Summary
Key equations
- d = v × t ÷ 2 distance from an echo
- v = 331.3 + 0.606 × T speed of sound, m/s at T °C
- cm = µs ÷ 58 an HC-SR04 at 343 m/s
Key terms
- Ultrasound
- Sound above about 20 kHz.
- Echo
- The reflected chirp.
- Transducer
- Turns signals into sound and back.
- Beam angle
- How wide it looks.
Practice problems
-
An echo takes 12 ms. How far away is the object?
d = 343 m/s × 0.012 s ÷ 2 ≈ 2.06 m.
-
Why does the distance formula divide by 2?
The echo time covers the trip to the object and back, twice the distance.
-
pulseIn returns 1,160 µs from an HC-SR04. How far is the object?
1,160 µs ÷ 58 µs per cm = 20 cm.
-
Why might a sensor miss a wall that is tilted away from it?
Sound reflects like light off a mirror, so a tilted wall sends the echo away from the sensor.
-
How fast does sound travel in air at 0 °C?
v = 331.3 + 0.606 × 0 ≈ 331 m/s; it rises about 0.6 m/s per degree.
FAQ
What is the range of an HC-SR04?
About 2 cm to 4 m, with a beam about 15° wide, best on hard, flat targets facing it.
Can humans hear an ultrasonic sensor?
No: it chirps at 40 kHz, about twice as high as the top of human hearing. Some dogs and cats can.
Can an ultrasonic sensor work underwater?
Not an HC-SR04. Waterproof sonar sensors exist, and they use the speed of sound in water, about 1,480 m/s.
Why does my ultrasonic sensor read 0 or a huge number?
No echo came back in time: the target is too far, angled, soft, or too close, or the wiring is loose.
Does an HC-SR04 work on 3.3 V?
The classic HC-SR04 needs 5 V and outputs 5 V on Echo; use a divider, or a 3.3 V version such as the HC-SR04P.
Why 58 microseconds per centimeter?
Sound covers 0.0343 cm per microsecond, and the echo travels each centimeter twice: 2 ÷ 0.0343 ≈ 58.
Can I use two ultrasonic sensors together?
Yes, if they take turns: fire one, wait for its echo, then the other, so neither hears the other’s chirp.