Almost every robot and assistive device needs to know when something is in the way. The three sensors students use most are the HC-SR04 ultrasonic sensor, IR obstacle modules and time-of-flight (ToF) laser sensors like the VL53L0X. Here is how they compare and when to use each.
The short answer
For most student robots, start with an HC-SR04 ultrasonic sensor: it is cheap, measures real distance and is easy to code. Use IR modules for line following and edge detection, and move to a ToF sensor when you need accurate readings in tight spaces or against angled surfaces.
| Ultrasonic (HC-SR04) | IR module | ToF (VL53L0X) | |
|---|---|---|---|
| Range | 2 cm – 4 m | 2 – 30 cm | Up to about 2 m |
| Output | Distance (echo pulse time) | Digital yes/no | Distance in mm over I²C |
| Accuracy | About ±3 mm in ideal conditions | Threshold only | Millimetre-level |
| Weak spots | Soft or angled surfaces, narrow objects | Sunlight, black surfaces | Very shiny or transparent surfaces |
| Price | Lowest | Lowest | Higher |
Ultrasonic sensors (HC-SR04)
The HC-SR04 sends a 40 kHz sound pulse and times the echo. Distance in centimetres is the echo time in microseconds × 0.0343 ÷ 2. Its beam is roughly 15° wide, so it can miss thin objects like chair legs, and soft materials such as curtains absorb the sound. It runs on 5 V — if you use an ESP32, put a voltage divider on the echo pin.
Infrared obstacle modules
IR modules shine infrared light and detect the reflection. They are ideal for line following and table-edge detection because they react fast, but they only tell you "something is close", and bright sunlight or matte black surfaces confuse them. Use the on-board potentiometer to set the trigger distance.
Time-of-flight sensors (VL53L0X)
A ToF sensor times a pulse of invisible laser light, giving millimetre-accurate distance over I²C and working well against angled surfaces where ultrasonic struggles. It costs more and needs a library, but it is the right choice for precise robots and for assistive devices where a missed obstacle matters.
Our recommendation
- First robot or obstacle avoider: HC-SR04.
- Line follower or edge detection: IR modules (two to five of them).
- Precise distance, tight spaces or assistive devices: VL53L0X — or combine it with an ultrasonic sensor for wider coverage.
Questions or improvements? Join the discussion →