Echo ranging — echolocation with electronics. An ultrasonic distance sensor measures how far away an object is by shouting at it and listening for the echo. It fires a short burst of ultrasound — sound at 40 kHz, above the 20 kHz limit of human hearing so the ping is silent to us — then starts a precision timer. The sound spreads out in a cone (a real HC-SR04 covers about ±15° around its axis), bounces off the first object it meets, and comes back. When the echo arrives, the timer stops.
Why robots need it: a robot must know how far away walls, obstacles and other robots are before it touches them. Cameras and lasers are complex and expensive; timing a sound echo needs nothing but a speaker, a microphone and a stopwatch — which is why a $2 HC-SR04 module has been on nearly every student robot since 2010.
How it works, step by step:
The sensor fires 8 cycles of 40 kHz ultrasound (about 0.2 ms of sound).
It raises its echo pin and a timer starts counting microseconds.
The pulse travels outward through the air at the speed of sound — about 343 m/s at room temperature.
It hits the object and reflects back (diffusely — in all directions).
The returning echo reaches the microphone; the timer stops.
Distance = speed × time ÷ 2 — because the sound flew there and back.
Where you see it in real engineering
Parking sensors in car bumpers, obstacle detection on robots and drones, liquid-level gauges in industrial tanks — and in nature, bats and dolphins have been running the exact same divide-by-two maths for 50 million years.
🧮 The maths
t = 2d / c · dmeas = cmodel · tmeas / 2 · c = 331.3 + 0.606·T
t — echo (round-trip) time (s)
d — true distance to the object (m)
c — actual speed of sound in air (m/s), T in °C
tmeas — the timer's reading (s) = t + measurement noise, quantised to 1 µs steps
cmodel — speed of sound the sensor assumes when converting time to distance (m/s)
dmeas — the distance the sensor reports (m)
beam-cone rule: echo only if |lateral offset| ≤ d · tan 15° ≈ 0.268 · d
offset — how far the object sits sideways from the sensor axis (m)
d·tan 15° — half-width of the beam cone at that range (m) — narrow up close, wide far away
Worked example: an object at d = 50 cm = 0.5 m, air at T = 20 °C.
c = 331.3 + 0.606·20 = 343.4 m/s.
Echo time t = 2·0.5 / 343.4 = 2.91 ms. A sensor with a fixed 20 °C model reports dmeas = 343.4·0.0029118/2 = 50.0 cm — exact, because the model matches reality. At 40 °C the same object gives t = 2·0.5/355.5 = 2.81 ms, and the uncompensated sensor reports 343.4·0.0028135/2 ≈ 48.3 cm — it now reads ~3.4% short.
Assumptions & limitations: the object is treated as a perfect diffuse reflector (real objects at shallow angles can reflect the sound away — a real cause of missed echoes); no multiple echoes, crosstalk or other objects; measurement noise is modelled as Gaussian with σ = 1.5·10⁻⁴ s (≈ ±2.6 cm of distance jitter) on top of the sensor's 1 µs timer resolution; beyond the 400 cm maximum range the sensor gets no echo at all (its timeout — a driver then reports "nothing" or a max-range sentinel). The beam is a simple ±15° cone. This simulation is an educational approximation of a real HC-SR04 ultrasonic sensor.
🕹 Simulation workspace
Sensor setup
The sensor fires a 40 kHz burst, waits for the echo, and converts the timer reading to distance using its speed-of-sound model. Slider changes apply to the next pulse.
Readouts
True distance—
Echo time—
Measured distance—
Error—
Ready
Echo time is the true physics (2d/c at the actual air temperature). Measured distance is what the sensor reports — it can drift when the model is wrong. Error compares report vs truth. No echo shows as "—".
travelling pulse (expanding ring)returning echoobject inside the beamobject outside the beam → no echoTx burst (scope)
📈 Graphs
Every echo that returns adds one red dot (true distance vs what the sensor reported) — the last 30 measurements are kept. The grey line is a perfect sensor. Dots hugging the line = trustworthy readings; dots sliding off it = the sensor's model of the world is wrong.
🧪 Guided experiments
🏆 Engineering challenges
🌍 Real-world applications
🚗 CarsParking sensors: every bumper hides 40–80 kHz ultrasonic modules. The beeps speed up as the gap shrinks — that's the echo time shrinking in real time.
🤖 RobotsObstacle detection for maze robots, vacuums and drones. Cheap, fast, and works in the dark — but blind to thin or sound-absorbing objects.
🏭 IndustryTank level gauges: the sensor sits on top and times the echo off the liquid surface — non-contact level measurement in chemical plants.
🦇 NatureBats and dolphins echolocate with the same divide-by-two maths, timing calls around 40–120 kHz and building a sound picture of the world.
🔧 Try it with hardwareArduino UNO R4 WiFi + an HC-SR04: VCC→5 V, GND→GND, Trig→D9, Echo→D10. Fire with a 10 µs HIGH pulse on Trig, then duration = pulseIn(Echo, HIGH); distance cm = duration × 0.0170 (≈ c/2 in cm/µs). On an ESP32 (3.3 V logic) put a 1 kΩ/2 kΩ voltage divider on Echo. Bonus: mount the sensor on a servo and sweep a "radar" map of the room.
✅ Check your understanding
📌 What you discovered
Distance from echo time: d = c·t/2 — the ÷2 is there because the sound makes a round trip.
Sound speed depends on air temperature: c = 331.3 + 0.606·T. A sensor with a fixed 20 °C model drifts ≈ ±4% over 0–40 °C — about 7% across the whole sweep.
The beam is a cone: objects outside ±15° are invisible, and up close the cone is only centimetres wide.
Real measurements have noise and timer quantisation — a single reading is a hint, repeated readings give a spread.
"No echo" is ambiguous: nothing there, an object outside the cone, a badly angled surface, or beyond max range.
➡ Engineering takeaway: every sensor reading is a model's answer, not the truth — before trusting a number, ask what the model assumes (temperature, beam width, range) and test where it breaks.