Distance Sensors
Introduction
Robots need to know how far away things are — avoid walls, follow lines, detect hands. Distance sensors convert physical gap into numbers your firmware reads over GPIO, UART, or I²C. Each technology trades range, accuracy, speed, and blind spots.
This article covers ultrasonic, infrared, and time-of-flight (ToF) sensors you see in student kits (HC-SR04, Sharp IR, VL53L0X). Pair with Reading datasheets and Firmware architecture.
What you are really measuring
| Technology | Measures | Good at |
|---|---|---|
| Ultrasonic | Echo time of sound pulse | Meters-scale, opaque objects |
| Infrared (triangulation) | Angle of reflected IR spot | Short range, fast, cheap |
| Time-of-flight (laser/IR) | Light round-trip time | mm precision, I²C |
In plain terms
Ultrasonic is shouting and listening for echo. IR Sharp is shining a dot and seeing where it lands. ToF is stopwatch for light.
Ultrasonic (e.g. HC-SR04)
Pins (typical): VCC, Trig, Echo, GND.
How it works:
- MCU sends 10 µs HIGH pulse on Trig.
- Module sends 8× 40 kHz sound bursts.
- Echo pin goes HIGH for a duration proportional to round-trip time.
- Distance ≈
(echo_time_us × speed_of_sound) / 2.
Use speed of sound ≈ 343 m/s (340 m/s is fine for lab math) → about 0.034 cm/µs round trip halved.
Example: echo 1160 µs → distance ≈ 1160 × 0.034 / 2 ≈ 20 cm (check your library's formula).
| Limit | Detail |
|---|---|
| Min range | ~2 cm — echo too fast |
| Max range | ~4 m (ideal); foam/absorbers shorten it |
| Beam width | Cone ~15° — not a laser point |
| Voltage | Often 5 V module → level-shift Echo to 3.3 V MCU |
Software: measure echo pulse width with timer input capture or pulseIn-style helper; timeout if no echo (out of range).
Infrared distance (Sharp GP2Y0A21 etc.)
Analog output — voltage maps to distance via non-linear curve in datasheet graph.
| Pros | Cons |
|---|---|
| Fast update | Sunlight and black objects confuse it |
| Simple ADC read | Narrower useful range (e.g. 10–80 cm) |
Read characteristic curve in datasheet — do not assume linear voltage × constant.
Time-of-flight (e.g. VL53L0X)
I²C sensor — returns distance in mm. Better for short range precision and small beams.
| Pros | Cons |
|---|---|
| mm-level | Glass, angles, and long range are tricky |
| No trigger echo timing | Needs stable I²C and address config |
See Serial communication protocols for I²C wiring.
Choosing a sensor
| Need | Start with |
|---|---|
| Cheap room mapping / obstacle | HC-SR04 |
| Fast proximity on desk | Sharp IR |
| Accurate short range / lid | VL53L0X |
| Outdoor long range | Ultrasonic or dedicated lidar module (advanced) |
Firmware patterns
- Filter readings — median of 3–5 samples beats single spikes.
- Rate limit — ultrasonic needs ~60 ms between pings; firing too fast collides with previous echo.
- Sensor module — expose
distance_mm()from a driver; app code stays clean (Firmware architecture).
Videos — other ways to learn
Ultrasonic sensor basics
How Ultrasonic Sensors Work (HC-SR04)
HC-SR04 with microcontroller
Arduino Tutorial: Ultrasonic Sensor HC-SR04
Further reading
- Ultrasonic distance sensing (overview article) — physics background
Relevant topics
- Reading datasheets
- Voltage level shifting
- Serial communication protocols
- Hardware architecture
- Lab tools for prototyping
Starting points
- Read HC-SR04 datasheet — note 5 V and timing limits.
- Print distance to serial every 200 ms; walk toward wall slowly.
- Plot raw vs filtered values in a spreadsheet — see spike rejection.
- Compare ultrasonic vs ToF on soft fabric — notice different failures.
Focus points
- Echo timeout — without it, code hangs waiting forever.
- Mounting angle — tilted sensor measures diagonal path, not horizontal gap.
- Temperature — speed of sound changes with heat; fine for lab, note for precision.
- 3.3 V compatibility — level-shift Echo from 5 V modules.
Key points
- Ultrasonic = pulse timing; IR Sharp = analog curve; ToF = I²C mm ranging.
- Pick sensor by range, material, and update rate — not price alone.
- Put timing and filtering in a driver module; document wiring in pin map.