pulseIn to Time the Echo
Measure the echo pulse and convert to cm.
pulseIn to Time the Echo is a free Arduino & IoT Academy lesson on CoddyKit — lesson 3 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.
Fire the Pulse
To take a reading you first send a clean trigger pulse. The HC-SR04 expects a short HIGH on Trig to start a new measurement.
Start From Clean
Set Trig LOW for a couple of microseconds first. This clears any leftover state so your trigger pulse is crisp and reliable.
digitalWrite(trigPin, LOW);
delayMicroseconds(2);A Ten-Microsecond Pulse
Now drive Trig HIGH for 10 microseconds, then back LOW. That exact width is what the sensor needs to launch its ultrasonic burst.
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);Meet pulseIn
The function pulseIn waits for a pin to go HIGH, then times how long it stays HIGH, returning that duration in microseconds.
Time the Echo
Call pulseIn on the Echo pin watching for HIGH. The number it returns is the full round-trip time of your sound pulse.
long duration = pulseIn(echoPin, HIGH);It Waits for You
pulseIn is blocking: it pauses your sketch until the echo arrives or times out. For simple distance projects that pause is perfectly fine.
Turn Time Into cm
Multiply the duration by the speed of sound and divide by two. The result is a clean distance in centimeters.
float cm = duration * 0.0343 / 2;Where That Number Comes From
Sound travels about 0.0343 cm per microsecond. That constant is just the speed of sound rewritten in units that match pulseIn.
Print It Out
Send the value to the Serial Monitor so you can watch the distance change as you move your hand toward the sensor.
Serial.print(cm);
Serial.println(" cm");Give It a Breather
Add a short delay between readings, around 60 milliseconds. This stops one echo from leaking into the next measurement.
delay(60);Handle a Timeout
If nothing reflects, pulseIn returns zero. Checking for that lets you skip junk readings instead of printing a false distance.
Quick Check
Think about what pulseIn measures.
From Pulse to Distance
Trigger, time with pulseIn, then convert and print. You can now read range in code. Next, make the sensor do something useful. ✅
Frequently asked questions
Is the “pulseIn to Time the Echo” lesson free?
Yes — the full text of “pulseIn to Time the Echo” 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 “pulseIn to Time the Echo”?
Measure the echo pulse and convert to cm. 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 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “pulseIn to Time the Echo” 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