Choosing a Distance or Motion Sensor

Compare HC-SR04, VL53L0X, and PIR sensors for ESP32 — ultrasonic vs laser vs infrared, range, precision, and use cases

Distance and motion sensors are used in everything from parking assistants and robots to security systems and automatic doors. But "measuring distance" and "detecting motion" are different problems, and the sensors that solve them work in very different ways. This guide explains the three most common options and helps you pick the right one — or combine them.

🔗Quick Recommendations

  • Detect whether someone is in the room: PIR sensor — passive, low power, no code complexity.
  • Measure distance on a budget (parking sensor, water level): HC-SR04 — cheap, long range, well-documented.
  • Precise millimetre-level distance (robotics, gesture detection): VL53L0X — laser time-of-flight, I2C, 3.3V native.

🔗Comparison Table

FeaturePIR (HC-SR501)HC-SR04 (Ultrasonic)VL53L0X (Laser ToF)
MeasuresMotion (yes/no)Distance ($2\text{--}400\,\text{cm}$)Distance ($30\text{--}1200\,\text{mm}$)
OutputDigital (HIGH/LOW)Pulse width (timing)Digital (I2C)
AccuracyN/A$\pm 3\,\text{mm}$$\pm 3\,\%$ typical
Operating voltage$3.3\text{--}5\,\text{V}$$5\,\text{V}$$2.6\text{--}3.5\,\text{V}$
Interface1 digital GPIO2 GPIOs (TRIG + ECHO)I2C
ESP32-safeYes (3.3V output)No — needs voltage divider on ECHOYes (3.3V native)
Affected by temperatureNoYes (speed of sound changes)No
Affected by surface materialNoYes (soft/angled surfaces absorb sound)Somewhat (very dark/shiny surfaces)
Detection angle~$120°$ cone~$15°$ cone~$25°$ cone
Library neededNoOptional (NewPing)Yes (Adafruit VL53L0X)
Current draw$< 50\,\mu\text{A}$ (idle)$\sim 15\,\text{mA}$$\sim 20\,\text{mA}$
PriceVery cheapVery cheapModerate

🔗PIR Sensor — Motion Detection

The PIR sensor detects changes in infrared radiation caused by warm bodies (people, animals) moving through its field of view. It does not measure distance — it simply outputs HIGH when motion is detected and LOW when the scene is still.

Best for:

  • Room occupancy detection
  • Security alarms and intruder alerts
  • Automatic lighting (turn on when someone enters)
  • Wake-on-motion for battery-powered devices (very low idle current)

Limitations:

  • Cannot tell you how far away something is
  • Cannot detect stationary warm objects (only movement)
  • Needs a $\sim 60\,\text{s}$ warm-up period after power-on
  • Sensitive to sudden temperature changes (heating vents, sunlight through windows can cause false triggers)

The PIR is the simplest sensor here — one digital pin, no library, no math. Two adjustment potentiometers on the HC-SR501 module let you tune sensitivity and the duration of the HIGH output.

🔗HC-SR04 — Ultrasonic Distance

The HC-SR04 sends a $40\,\text{kHz}$ ultrasonic pulse and measures the time for the echo to return. Distance is calculated from the speed of sound.

Best for:

  • Parking assistants and proximity alerts
  • Water level measurement in tanks
  • Object avoidance on robots
  • Any distance measurement up to $\sim 4\,\text{m}$

Limitations:

  • Runs on 5V — the ECHO pin outputs 5V, so you need a voltage divider to protect the ESP32's 3.3V GPIO
  • Sound-based — soft materials (fabric, foam) absorb the pulse, angled surfaces deflect it away
  • Speed of sound changes with temperature ($\sim 0.6\,\text{mm/°C}$), affecting accuracy in extreme conditions
  • Wide-ish beam ($15°$ cone) means it cannot distinguish small objects next to each other
  • Minimum range of $\sim 2\,\text{cm}$ — objects closer than that produce unreliable readings

Despite these limitations, the HC-SR04 is extremely popular because it is cheap, available everywhere, and "good enough" for most hobby projects.

🔗VL53L0X — Laser Time-of-Flight

The VL53L0X uses a tiny eye-safe laser ($940\,\text{nm}$) and measures the time for the light to bounce back. Because light is much faster than sound, the measurement is nearly instantaneous ($\sim 30\,\text{ms}$) and unaffected by temperature.

Best for:

  • Precision distance measurement (millimetre resolution)
  • Robotics and gesture detection
  • Fill-level sensing in small containers
  • Projects already using I2C (shares the bus with other sensors)
  • 3.3V projects (no voltage divider needed)

Limitations:

  • Shorter range — maximum $\sim 1.2\,\text{m}$ (vs. $4\,\text{m}$ for HC-SR04)
  • Accuracy degrades on very dark or shiny surfaces (low reflectance)
  • Minimum range of $\sim 30\,\text{mm}$
  • Default I2C address 0x29 cannot be changed in hardware — multiple sensors require the XSHUT pin trick (power off all, enable one at a time, reassign addresses in code)

🔗Other Distance Sensors Worth Knowing

SensorTypeRangeInterfaceNotes
VL53L1XLaser ToFUp to $4\,\text{m}$I2C (0x29)Longer-range successor to VL53L0X; programmable field of view
Sharp GP2Y0A21Infrared analog$10\text{--}80\,\text{cm}$AnalogNo timing code needed; reads like an LDR but for distance
RCWL-1601Ultrasonic$2\text{--}400\,\text{cm}$Compatible with HC-SR043.3V version of HC-SR04 — no voltage divider needed
TF-LunaLiDARUp to $8\,\text{m}$UART / I2CLonger-range LiDAR for outdoor use

The VL53L1X is particularly worth noting — it keeps the VL53L0X's precision and I2C convenience but extends the range to $\sim 4\,\text{m}$, making it competitive with the HC-SR04 on range while being far easier to wire.

🔗Combining Sensors

These sensors complement each other well. Common combinations:

  • PIR + HC-SR04: Detect motion first (PIR), then measure how far away the person is (HC-SR04). The PIR acts as a low-power wake trigger.
  • PIR + VL53L0X: Same idea, but with more precise distance. Good for gesture-sensing projects.
  • HC-SR04 + servo: Mount the ultrasonic sensor on a servo for a scanning "radar" that maps the surroundings.

🔗I2C Address Quick Reference

SensorDefaultAlternateNotes
VL53L0X0x29Software-reassigned via XSHUTNo hardware alternate
VL53L1X0x29Software-reassigned via XSHUTSame as VL53L0X

The PIR and HC-SR04 do not use I2C, so they never conflict with bus devices.

🔗Which Should You Pick?

Your SituationRecommendedWhy
"Is someone in the room?"PIRSimplest, lowest power, built for exactly this
Parking distance indicatorHC-SR04Long range, cheap, widely available
Robot obstacle avoidanceHC-SR04 or VL53L0XHC-SR04 for budget, VL53L0X for precision
Precise mm-level measurementVL53L0XBest accuracy, I2C, no voltage divider
Long-range precision (>1.2m)VL53L1XExtends ToF range to ~4m
Water level in a tankHC-SR04Works well pointing straight down at a water surface
Battery-powered motion alertPIRDraws under $50\,\mu\text{A}$ when idle
Gesture / hand-wave detectionVL53L0XFast, precise, short-range — perfect fit

🔗Next Steps