How Ultrasonic Ranging Works
Send a ping and time the echo to find distance.
How Ultrasonic Ranging Works is a free Arduino & IoT Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Arduino & IoT Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Seeing with Sound
An ultrasonic sensor measures distance the way a bat does: it sends out a sound pulse and listens for the echo bouncing back. 🦇
Too High to Hear
The pulse is ultrasonic, around 40 kHz, far above what your ears can detect. The sensor works silently while still using real sound waves.
Send, Then Listen
The trick is timing. The sensor fires a quick ping, then starts a stopwatch and waits for that same sound to return after hitting an object.
The Round Trip
The sound travels out to the object and all the way back, so the measured time covers a round trip, twice the actual distance to the target.
Sound Has a Speed
Sound moves through air at about 343 meters per second. Because that speed is known, time can be turned directly into distance.
Time to Distance
Distance equals speed times time. Knowing how long the echo took, you can compute exactly how far the sound traveled before bouncing.
// distance in cm, time in microseconds
distance = duration * 0.0343 / 2;Why You Divide by Two
Since the echo covers the path twice, you halve the result. That single division converts a round trip into the real one-way distance.
Microseconds Matter
These pulses return in microseconds, so the sensor times very tiny intervals. Tinier time differences simply mean a closer object.
The HC-SR04
The popular HC-SR04 module does all this for you. It has one transmitter that chirps and one receiver that catches the returning echo.
Its Useful Range
This sensor reliably reads from about 2 cm to 400 cm. Closer than 2 cm or farther than that, and the readings stop being trustworthy.
Where Echoes Fail
Soft or angled surfaces can scatter the sound away, so a flat, solid target gives the cleanest, most accurate distance readings.
Quick Check
Think about the path the sound takes.
You Get It Now
Ultrasonic ranging is just timed echoes: ping, wait, and convert the round-trip time into distance. Next you will wire the sensor up. ✅
Frequently asked questions
Is the “How Ultrasonic Ranging Works” lesson free?
Yes — the full text of “How Ultrasonic Ranging Works” is free to read here on the web, and the Arduino & IoT Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Arduino & IoT Academy course, upgrade to CoddyKit PRO.
What will I learn in “How Ultrasonic Ranging Works”?
Send a ping and time the echo to find distance. You practise Arduino & IoT Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Arduino & IoT Academy?
No prior experience is required. Arduino & IoT Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “How Ultrasonic Ranging Works” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Arduino & IoT Academy lesson?
Yes. Every Arduino & IoT Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- How Ultrasonic Ranging Works
- Trigger & Echo Wiring
- pulseIn to Time the Echo
- Build a Proximity Alarm