Lesson 13: Distance Sensing: Ultrasonic and InfraredNew-age
A robot that cannot sense distance will drive into walls. Distance sensing is the first step from a machine that moves to a machine that navigates, and the two commonest sensors — ultrasonic and infrared — teach two different ways of measuring the world. An ultrasonic sensor shouts and listens for the echo; an infrared sensor shines light and watches for the reflection. Each has strengths, each has blind spots, and choosing between them is the lesson.
You will wire an ultrasonic sensor, measure distance with it, and learn why its readings are sometimes wrong — soft surfaces, angled walls and out-of-range targets all fool it. Then you will compare it with an infrared sensor and decide which fits which job.
- Ultrasonic sensor — a sensor that measures distance by timing the echo of a sound pulse.
- Infrared (IR) sensor — a sensor that measures distance or presence using reflected infrared light.
- Echo — the sound pulse reflected back from an object to the sensor.
- Speed of sound — about 343 metres per second in air, used to convert echo time to distance.
- Range — the span of distances a sensor can measure, such as 2 cm to 400 cm.
- Blind spot — a distance too close for a sensor to measure, such as under 2 cm for an ultrasonic sensor.
- Beam angle — the spread of a sensor’s signal; a wide beam sees more but locates less precisely.
- Timeout — a limit on how long to wait for an echo, so the program does not freeze.
How an ultrasonic sensor works
An HC-SR04 ultrasonic sensor has two “eyes”: one sends a short burst of ultrasound, inaudible to humans, and the other listens for the echo. The microcontroller triggers the burst, measures how long the echo takes to return, and converts the time to a distance: distance = (time × speed of sound) ÷ 2, divided by two because the sound travels there and back. At 343 m/s, sound takes about 29 microseconds to travel one centimetre and back, so a 580-microsecond echo means the object is 10 cm away.
The sensor’s range is about 2 cm to 400 cm. Closer than 2 cm, the echo returns before the sensor is ready to listen — a blind spot. Further than 400 cm, the echo is too weak to hear. The measurement is also affected by temperature, because the speed of sound changes with temperature, but for a classroom robot the error is small — about 1% per 5 °C — and can usually be ignored.
Wiring and reading the HC-SR04
Wire the sensor’s VCC to 5 V, GND to ground, TRIG to a digital pin and ECHO to another digital pin. To take a reading, set TRIG HIGH for 10 microseconds, then LOW, then measure how long ECHO stays HIGH with pulseIn(). That duration, in microseconds, is the echo time. Divide by 58 to get centimetres — the shortcut for (time × 343) ÷ 2 ÷ 10,000, which works out to time ÷ 58.
Always use a timeout with pulseIn(), such as pulseIn(ECHO, HIGH, 30000) — 30 milliseconds, which covers the sensor’s full 400 cm range. Without a timeout, a missing echo (no object in range) makes pulseIn() wait a full second, freezing the program. With a timeout, a missing echo returns 0, which your code should treat as “out of range”, not as “0 cm”.
What fools an ultrasonic sensor
Soft surfaces absorb sound: a cushion, a curtain or a person in a thick coat returns a weak echo, so the sensor reports a longer distance or nothing at all. Angled surfaces reflect the sound away instead of back, so a wall at 45 degrees can look like open space. And a narrow object, such as a chair leg, may fall between the sensor’s pulses or outside its beam, which is about 15 degrees wide. A robot that trusts a single ultrasonic reading will eventually hit something it could not see.
Infrared distance sensors
An infrared sensor, such as the Sharp GP2Y0A21YK, shines infrared light and measures the angle of the reflected light to estimate distance. Its range is shorter — about 10 cm to 80 cm — but it is fast, it works on small objects, and it does not depend on sound. Its weakness is the surface: black surfaces absorb infrared and look further away than they are, while shiny surfaces reflect it and look closer. Sunlight, which contains infrared, can also confuse it outdoors.
Worked example — measuring a distance
The echo time is 1160 microseconds. Distance = 1160 ÷ 58 = 20 cm. Check: sound travels 1160 µs × 343 m/s = 0.398 m there and back, so one way is 0.199 m, or about 20 cm — the shortcut agrees. Now check the code that produces it:
digitalWrite(TRIG, LOW); delayMicroseconds(2); digitalWrite(TRIG, HIGH); delayMicroseconds(10); digitalWrite(TRIG, LOW); long t = pulseIn(ECHO, HIGH, 30000); int cm = t / 58; // 0 means out of range
Common mistakes
- Treating a 0 reading as 0 cm. It means “no echo” — out of range, not touching.
- Using pulseIn() without a timeout, so a missing echo freezes the program for a second.
- Trusting a single reading. Take several and use the median.
- Pointing the sensor at an angled wall and concluding the way is clear. Angled surfaces reflect sound away.
Discussion questions
- An ultrasonic sensor measures 20 cm to a cushion and 20 cm to a wall. Is it measuring the same thing in both cases? What is different?
- Why is the median of three readings better than the average when one reading is wild?
- A robot has both an ultrasonic sensor and a bumper switch. What is each one for, and why does the robot need both?
Practice questions
- An echo takes 580 microseconds. How far away is the object? Answer: 10 cm. Distance = time ÷ 58, so 580 ÷ 58 = 10 cm. Explanation: Divide microseconds by 58.
- Why divide the echo time by two when calculating distance? Answer: Because the sound travels to the object and back. The echo time covers both journeys, so half of it is the one-way distance. Explanation: There and back.
- What does a pulseIn() reading of 0 mean? Answer: No echo was heard within the timeout — nothing in range, not an object at 0 cm. Explanation: 0 means “nothing heard”.
- Why take the median of three readings instead of the average? Answer: The median ignores a single wild reading. An average would be dragged off by it — one reading of 400 cm among 20 cm readings would corrupt the average. Explanation: Median resists outliers.
- A black surface looks further away to an infrared sensor than a white one at the same distance. Why? Answer: Black absorbs infrared light, so less is reflected and the sensor reads it as further away. Shiny surfaces do the opposite. Explanation: Surface affects reflection.
✅ Check your understanding
Quick recall check, generated from this lesson’s key terms. It is practice only and is not graded.