Distance and Motion Sensors: the short answer
The HC-SR04 ultrasonic sensor sends a 40 kHz chirp and times the echo. You pulse TRIG high for 10 µs, then measure how long ECHO stays high.
- Level
- Intermediate
- Time
- About 6 min
- Topic
- Sensors and actuators
Pick between ultrasonic, time-of-flight, infrared and PIR sensors, and handle their timing, range and false triggers.
Before this: Sensor Interfaces, Logic Levels: 5 V and 3.3 V Together
The HC-SR04 ultrasonic sensor sends a 40 kHz chirp and times the echo. You pulse TRIG high for 10 µs, then measure how long ECHO stays high. Sound travels about 343 m/s at 20 °C, so distance in cm = echo time in µs ÷ 58. It works from about 2 cm to 4 m on hard, flat surfaces, but soft materials absorb the sound and angled surfaces reflect it away. Its echo output is 5 V, so use a divider on a 3.3 V board, and wait at least 60 ms between readings so old echoes die out.
Time-of-flight (ToF) sensors such as the VL53L0X and VL53L1X time an invisible laser pulse instead. They are small, use I2C, have a narrow beam and work on soft surfaces, with a range of about 2 m for the VL53L0X and up to about 4 m for the VL53L1X indoors. Bright sunlight and very dark or shiny targets reduce their range. Simple IR proximity modules only tell you whether something is near, and their range depends on the target's colour.
A PIR sensor such as the HC-SR501 does not measure distance. It detects changes in infrared heat across its lens, so it senses people and animals moving, not still objects. It needs up to a minute to settle after power-up. Two potentiometers set sensitivity and how long the output stays high, and a jumper selects single or repeat trigger. PIRs can be triggered by heaters, sunlight through windows and air from fans, so mount them away from these and combine with a second check, such as a ToF sensor, where false alarms matter.
Know how each sensor measures
An ultrasonic sensor such as the HC-SR04 sends a sound pulse and times the echo, and distance is the time multiplied by the speed of sound, about 343 m/s, divided by two. Soft surfaces absorb sound and angled surfaces reflect it away, so a cushion or a sloped wall can be missed. Infrared distance sensors read the angle of reflected light and depend on the surface colour.
Time-of-flight sensors such as the VL53L0X time a light pulse and are more precise at short range, but strong sunlight and some surfaces reduce their range.
Motion is different from distance
A PIR sensor detects changes in infrared heat, so it sees a warm body moving across its view and not one standing still. It has a delay and a sensitivity setting, and after triggering it holds the output for a time. It suits lights and alarms but not counting or measuring.
A radar module can detect movement through thin plastic, and a sensor with an IMU measures the motion of the board itself.
Filtering the readings
Single readings jump. Take several and use the median, which ignores a stray value, and ignore results outside the sensor's stated range. A short delay between pings stops one echo being mistaken for the next.
- Expecting an ultrasonic sensor to detect soft or angled objects.
- Using a PIR sensor to detect someone standing still.
- Trusting a single reading instead of filtering several.
Practise this lesson with Maker
The explanation above is free for everyone. The practice for Intermediate lessons in every domain are part of the Maker plan at ₹499 a month, and Pro includes them too.
With Maker you also get: a worked example, a hands-on task, a 3-question check.
