What a distance sensor really tells you
A distance sensor does not see the world the way you do. It sends out sound or light, waits for what comes back, and turns the result into a number. Every approach has surfaces and situations where that number is wrong or missing, so choosing among distance sensors is mostly a question of which blind spots you can live with.
The three families covered here are ultrasonic, infrared and optical time-of-flight. Cameras are another route, but they involve vision software rather than a simple reading.
Ultrasonic sensors
An ultrasonic module sends a short burst of high-pitched sound and measures the time until the echo returns. Because sound travels at roughly 343 metres per second in air at about 20 degrees Celsius, the controller can convert the round-trip time into distance. Since the sound goes out and back, the distance is half of speed multiplied by time. For example, an echo that returns after 5.8 milliseconds corresponds to about 1 metre (343 × 0.0058 ÷ 2 ≈ 0.99 m). The speed of sound shifts with temperature, so careful designs allow for that.
Strengths
- Works on many surfaces regardless of colour, including glass and shiny objects that confuse light-based sensors.
- Not bothered by sunlight in the way infrared can be.
- Inexpensive and simple to read.
Blind spots
- The sound spreads out in a cone, so the sensor cannot tell where within a wide area the object is.
- Soft materials such as fabric or foam can absorb sound and return a weak echo.
- Surfaces at a steep angle can bounce the sound away from the sensor rather than back to it, so the robot may not see a wall it approaches at a slant.
- Very close objects fall inside a minimum range where the sensor cannot measure.
- Several ultrasonic sensors running at once can hear each other, so they are often triggered one at a time.
- Readings update relatively slowly compared with light-based methods.
Infrared sensors
Infrared sensors fall into two broad groups, and it is worth keeping them apart.
Simple reflective sensors shine an infrared LED and measure how much returns. They are good for detecting that something is near, such as a table edge or a wall within a few centimetres, but the amount reflected depends on surface colour, texture and angle. A dark object can look farther away than a pale one at the same distance. They are often used as presence detectors rather than rulers.
Triangulating infrared rangers send a beam and detect where the reflected spot lands on a small sensor. The position of the spot gives the distance more reliably than brightness. They usually give an analog voltage that is not linear with distance, so a conversion table or curve from the datasheet is needed. These work in a limited range, and many models give ambiguous readings if the object is closer than the minimum distance.
Strengths
- Narrow beam, so you know roughly where the object is.
- Fast response.
- Small and cheap.
Blind spots
- Strong sunlight or other infrared sources can disturb readings.
- Surface colour and finish can bias the result, particularly with simple reflective types.
- Transparent materials such as clear glass may let light pass through.
- Limited useful range compared with other methods.
Optical time-of-flight sensors
Optical time-of-flight modules measure how long a pulse of light, usually from a small infrared laser or LED, takes to return. Light is very fast, so these sensors rely on specialised electronics to time it. They typically report distance digitally over a bus, which removes the need to calibrate an analog curve and makes the readings cleaner than raw analog infrared.
Strengths
- Narrow beam with good directional information.
- Digital output, which is easy to read and less sensitive to wiring noise.
- Often more consistent across surface colours than simple infrared.
Blind spots
- Bright ambient light, glass, mirrors and very dark surfaces can reduce range or accuracy.
- Maximum range is usually stated for good conditions and is often shorter in practice.
- They can cost more than ultrasonic or simple infrared parts.
- A single beam sees only one thin line, so a chair leg or a gap in a fence can slip between readings.
Spinning scanners that sweep a beam around the robot give a fuller picture, but at higher cost and complexity, and they are beyond what most beginner projects need.
Comparing them at a glance
| Feature | Ultrasonic | Infrared | Optical time-of-flight |
|---|---|---|---|
| Beam | Wide cone | Narrow | Narrow |
| Hard cases | Soft or angled surfaces | Sunlight, dark or clear objects | Glass, mirrors, bright light |
| Output | Echo timing | Mostly analog | Mostly digital |
| Relative cost | Low | Low | Moderate |
Specific parts differ, so treat the table as tendencies, not guarantees.
Practical choices for obstacle avoidance
- Mix methods. An ultrasonic sensor for a wide forward warning plus a narrow optical or infrared sensor for edges is a common pairing, since their blind spots differ.
- Aim sensors carefully. Mount them so the beam does not hit the robot’s own body or the floor in front of it. Solid mounting hardware keeps the aim consistent, and rattling sensors give noisy data.
- Filter the readings. Noisy supplies make this worse, so a steady supply from voltage regulators helps. Take several samples and use the median, or ignore a single sudden jump, before acting.
- Plan for the unknown. If a reading fails or returns the maximum value, decide what the robot should do. A simple state machine with modes for cruising, slowing and backing away keeps this clear.
- Verify wiring. Multimeter readings of supply voltage at the sensor catch many faults before you blame the code.
- Test with awkward objects. Try fabric, glass, chair legs and angled walls. Systematic debugging with these cases shows the real limits quickly.
- Check the datasheet. Supply voltage, logic level and minimum distance all matter, and reading datasheets before wiring avoids damaged inputs.
Remember that a sensor is advisory, not a guarantee. A robot that could cause harm or damage if it fails to detect something needs a separate, non-software way to stop. If you later want the robot to recognise what an object is, rather than only how far away it is, that moves into the territory of object detection with a camera.