Learning Goals 5 min
- Explain how the HC-SR04 ultrasonic sensor measures distance: send a sound pulse, time the echo, divide.
- State the speed of sound at room temperature (~343 m/s, or roughly 29 microseconds per centimetre, one way) and use it to predict echo times for known distances.
- Identify the limits of an ultrasonic sensor — minimum range, maximum range, beam angle, what materials it can't see — before you wire a single thing.
Warm-Up 10 min
Cluster D opens with the HC-SR04 — the little blue board with two silver cylinders that look like robot eyes. They're not eyes; they're a tiny speaker and a tiny microphone, both working at 40 kHz — far above what humans can hear. The sketch shouts at a wall in ultrasound, listens for the echo, and uses the time-of-flight to compute how far away the wall is.
An everyday analogy
Bats do exactly this. So do submarines. So do reversing cars (the "beep beep beep" that speeds up as you approach a kerb is an HC-SR04's big, expensive cousin). The principle:
- Send a short, sharp sound.
- Start a stopwatch.
- Stop the stopwatch when the echo arrives.
- Distance = (time × speed of sound) ÷ 2.
Divide by 2 because the sound travelled out and back. We'll spend today on the physics; tomorrow on the pulseIn() function that actually does the timing in code; the day after on the distance maths.
New Concept · Time-of-flight measurement 25 min
The four-pin module
| Pin | Role | Connect to |
|---|---|---|
| VCC | +5 V power | +5 V rail |
| TRIG | You pulse this HIGH for 10 µs to start a measurement | D9 (any digital pin) |
| ECHO | The module pulses this HIGH for as long as the echo took | D10 (any digital pin) |
| GND | Ground | GND rail |
Two control wires — TRIG (you talk to it) and ECHO (it talks back). Plus power and ground.
What happens during a single measurement
- You set TRIG HIGH for 10 microseconds, then LOW. This tells the chip to start.
- The chip blasts an 8-cycle 40 kHz "ping" out of the transmit cylinder.
- The chip immediately raises ECHO HIGH and starts its own internal stopwatch.
- The ping travels through the air, hits something, and bounces back.
- The receive cylinder picks up the returning ping.
- The chip drops ECHO LOW.
So the width of the ECHO pulse — the time it stayed HIGH — equals the round-trip time of the ping. Measuring that pulse width is what tomorrow's lesson (pulseIn()) is about. Today is the physics.
The speed of sound — the magic constant
At 20 °C in dry air, sound travels at 343 metres per second. Some equivalent forms that are handy to memorise:
- 343 m/s = 34 300 cm/s = 34.3 cm per millisecond = 0.0343 cm per microsecond.
- Inverted: ~29.1 microseconds per centimetre, one way.
- For a round trip: ~58 microseconds per centimetre.
That 58 µs/cm is the number we'll plug into the distance formula in two days. For now, memorise it — it's the bridge between the time the chip reports and the distance you want.
Worked numerical examples
If the object is 10 cm away:
time = distance × 58 µs/cm
= 10 × 58
= 580 µs
So ECHO stays HIGH for 580 microseconds.If ECHO reports 1740 µs:
distance = time / 58
= 1740 / 58
= 30 cmThat's really the whole physics of the sensor: multiply (or divide) by 58.
Where the speed of sound varies
Speed depends slightly on temperature: warmer air = faster sound. The precise formula:
v (m/s) = 331.4 + 0.6 × T(°C)At 20 °C → 343.4 m/s. At 30 °C → 349.4 m/s (1.7% faster). At −10 °C → 325.4 m/s (5% slower).
For classroom Malaysia (25–30 °C), using a fixed 343 m/s overestimates distance by ~1%. For a 30 cm reading, that's a 3 mm error — well below our other error sources. Ignore it for now; revisit if you ever build a precision rangefinder.
Limits of the sensor
- Minimum range: ~2 cm. Closer than that, the ping and echo overlap and the chip gets confused.
- Maximum range: ~400 cm (4 m) in lab conditions, more like 2–3 m in practice. Beyond that the echo is too weak to detect.
- Beam angle: the ping spreads in a ~15° cone. Anything outside that cone won't be seen, even if it's nearby. Anything inside competes — closest wins.
- Soft / fluffy / angled surfaces absorb or deflect the ping and produce no usable echo. Curtains, foam, the side of a tilted book — all invisible.
- Hard, flat, perpendicular surfaces give the strongest echo. A wall, a textbook held flat, a metal sheet — all easy.
Worked Example · Predict the echo times 15 min
No wiring today — we're building intuition before code. Work through these as a class, on paper or a whiteboard. The point is to feel comfortable converting between time and distance before you ever touch pulseIn().
Predictions to compute
- Object 5 cm in front of the sensor → ECHO pulse width is ___ µs.
- Object 50 cm in front of the sensor → ECHO pulse width is ___ µs.
- Object 100 cm in front of the sensor → ECHO pulse width is ___ µs.
- Object 1 metre in front of the sensor → ECHO pulse width is ___ ms (note units!).
- Object at the maximum range (4 m) → ECHO pulse width is ___ ms.
Reveal the answers
- 5 cm × 58 µs/cm = 290 µs
- 50 × 58 = 2900 µs (≈ 3 ms)
- 100 × 58 = 5800 µs (≈ 5.8 ms)
- 1 m = 100 cm → 5.8 ms (same as above)
- 400 cm × 58 µs/cm = 23 200 µs ≈ 23 ms
Notice how reasonable these are: even the longest measurement takes less than 25 ms. Your Arduino loop can comfortably run at 40 Hz (one measurement every 25 ms) — fast enough that fans, robot wheels, even your hand waving toward the sensor all look smooth.
Reverse predictions
The sensor reports ECHO pulse widths. Convert these to distances:
- ECHO = 1160 µs → distance = ___ cm
- ECHO = 4640 µs → distance = ___ cm
- ECHO = 11 600 µs → distance = ___ cm (or m)
- ECHO = 38 000 µs → distance = ___ cm — and is this within the sensor's range?
Reveal the answers
- 1160 / 58 = 20 cm
- 4640 / 58 = 80 cm
- 11 600 / 58 = 200 cm = 2 m
- 38 000 / 58 ≈ 655 cm = 6.55 m. That's well beyond the sensor's reliable 4 m max — most likely a noise echo or the sensor's timeout. Real sketches treat any reading > 400 cm as "invalid" (we'll handle this in L02-23).
The reflection table
Predict which of these targets would give a strong echo, a weak one, or none at all. Discuss as a class.
| Target | Expected echo |
|---|---|
| Hardback book held flat at 30 cm | ? |
| Hardback book held at 45° at 30 cm | ? |
| Couch cushion at 30 cm | ? |
| Glass of water at 30 cm | ? |
| Stretched cling film at 30 cm | ? |
| Your open palm at 30 cm | ? |
| A cat in front of the sensor | ? |
Reveal one set of predictions
- Hardback book, flat: strong, clean echo. The reference case.
- Hardback book, 45°: weak or none. Ping bounces sideways instead of back.
- Couch cushion: weak. Foam absorbs most of the energy.
- Glass of water: strong, but a little jumbled. The smooth glass reflects well.
- Cling film: very weak. Thin and pliable — vibrates and absorbs.
- Open palm: medium. Skin reflects okay but the surface isn't flat.
- Cat: very poor. Fur absorbs; the body curves. Plus the cat moves. The sensor cannot do object recognition; it just measures distance to whatever bounced.
Try It Yourself 15 min
Goal: Compute the ECHO times for these objects (assume 58 µs/cm):
- Your eraser at 8 cm.
- Your shoe at 25 cm.
- Across-the-room wall at 350 cm.
- A point 3 cm above the sensor (right at the minimum range).
Reveal
- 8 × 58 = 464 µs
- 25 × 58 = 1450 µs
- 350 × 58 = 20 300 µs ≈ 20 ms
- 3 × 58 = 174 µs — right at the edge of what the sensor can resolve cleanly
Goal: The temperature in the room is 30 °C (not 20). Calculate the new speed of sound, the new microseconds-per-centimetre, and the percent error introduced by using the fixed 58 µs/cm.
Reveal
v = 331.4 + 0.6 × 30 = 349.4 m/s = 34 940 cm/s
1 cm round-trip: 2 / 34 940 × 1 000 000 = 57.24 µs/cm
Error: (58 - 57.24) / 57.24 = 1.3%1.3% means a measured 100 cm is really 98.7 cm. For most projects this is invisible. For a precision rangefinder it's worth correcting with the temperature.
Goal: Two HC-SR04 modules, side by side, both pointed at a wall 50 cm away. Module A fires; module B's receiver picks up A's echo a few microseconds later. Explain what kind of false reading B might report — and how you'd schedule the two modules to avoid the problem.
Reveal
Module B might report a phantom distance based on the echo arrival from module A's ping — which travelled slightly less far (it didn't have to round-trip from B's position). B reports something like 47 cm when the real distance is 50 cm. Worse, the phantom could be wildly wrong if A's ping reaches B directly without bouncing (B reports a few-cm distance based on the direct line between A and B).
Fix: stagger the measurements. Fire A, wait 25 ms (long enough for any A-ping to die out), then fire B, wait 25 ms, repeat. Maximum measurement rate halves but the readings stay honest. For more than two sensors, the principle scales — the slot time is the maximum out-and-back of the longest range.
Mini-Challenge · Build a paper rangefinder calculator 15 min
Today is theory day, so today's challenge is theory too. In pairs, design (on paper) a short reference card that someone could tape to their lab bench to help them debug an HC-SR04 sketch quickly. The card should have:
- The conversion formula, in both directions:
µs → cmandcm → µs. - A small table of 5 common distances (5, 10, 25, 50, 100 cm) with their predicted ECHO times.
- A "limits" box: minimum range, maximum range, beam angle.
- A "what could go wrong" box with three bullet points (sensor sees the floor, sensor saturates near the wall, sensor sees nothing because target is soft, etc.).
Aim for a card that fits on half a sheet of A4. Hand-drawn is fine — the point is the content, not the visuals.
It's done when:
- A classmate (not your partner) can read the card and use it to predict an ECHO time for a 1.5 m distance without asking you for help.
- The "what could go wrong" box mentions at least one material the sensor can't see.
- You can hand the card to a teacher who's never seen an ultrasonic sensor and they understand the rules within a minute.
Recap 5 min
The HC-SR04 measures distance using sound's round-trip time. You trigger a ping with a 10 µs pulse on TRIG, and the chip raises ECHO for exactly as long as the round-trip took. At 343 m/s (the speed of sound at 20 °C), the conversion is ~58 µs per centimetre round-trip — the single number you need to memorise. The sensor works best on hard, flat, perpendicular surfaces; struggles with soft, angled, or absorbent targets; and is bounded to roughly 2 cm minimum and 4 m maximum. Tomorrow we use pulseIn() to measure that ECHO pulse in code; the day after we wrap the whole thing in a clean distance function.
- Ultrasonic
- Sound waves above human hearing — typically above 20 kHz. The HC-SR04 uses 40 kHz, well clear of human (and most pet) hearing.
- Time-of-flight (TOF)
- Measuring distance by timing how long a signal takes to reach an object and return. Used by ultrasonic, laser, radar.
- Round-trip time
- How long it takes a pulse to travel out and bounce back — what ECHO reports. Always twice the one-way travel time.
- TRIG / ECHO
- The HC-SR04's two control pins. You pulse TRIG HIGH for 10 µs to start; the chip pulses ECHO HIGH for the duration of the round trip.
- Beam angle
- The cone of space the sensor "sees". For the HC-SR04 it's ~15° wide. Anything outside the cone is invisible; anything inside competes — closest wins.
- Acoustic absorption
- The tendency of soft, fluffy materials to absorb sound energy rather than reflect it. Why curtains, foam and cats are hard targets.
- Speed of sound (in air)
- ~343 m/s at 20 °C, increasing by ~0.6 m/s per °C. We use the 20 °C value (= 58 µs/cm round-trip) as a good-enough constant.
- Minimum / maximum range
- The HC-SR04 is reliable from ~2 cm to ~400 cm. Below 2 cm: ping and echo overlap. Above 400 cm: echo too weak to detect.
Homework 5 min
Conversion drill. Work through these on paper — show the multiplication or division each time, not just the answer:
- An object is 12 cm in front of the sensor. ECHO pulse width = ?
- An object is 75 cm in front of the sensor. ECHO pulse width = ?
- An object is 2.5 m in front of the sensor. ECHO pulse width = ?
- ECHO reports 2320 µs. Distance = ?
- ECHO reports 14 500 µs. Distance = ? Is this within the sensor's range?
- ECHO reports 87 µs. Distance = ? Is this within the sensor's range?
- Bonus: The temperature is 40 °C (a hot day in the sun). Recompute (3) using the temperature-corrected speed of sound, and compare to your original answer. By how many millimetres do they differ?
Bring back next class:
- Your 7 worked answers (with the maths shown).
- Your reference card from the mini-challenge (or a fresh polished version).
- No sketch this lesson — tomorrow we write the first one.