What the HC-SR501 detects
The HC-SR501 is a PIR module that detects changes in infrared radiation produced by the movement of warm bodies. It’s cheap and simple, but it doesn’t detect static presence: if a person stays still they may stop triggering the output.
It’s good enough for a hallway light or a movement alert. To know whether someone is still sitting in a room, an mmWave radar such as the LD2410 may fit better.
Pins and controls
Most modules expose three pins:
| Pin | Function |
|---|---|
| VCC | Power for the module |
| OUT | Digital motion output |
| GND | Common ground |
They usually also include two adjustments: one for sensitivity and another for how long the output stays active. Some modules include a retrigger mode selector. The silkscreen can vary, so check the module before wiring it.
Wiring to the ESP32
For a simple test, connect VCC to a compatible voltage, GND to the ESP32 ground and OUT to a GPIO configured as an input. The output level must be compatible with the ESP32: don’t assume that any module powered at 5 V can drive a 5 V signal straight into a 3.3 V GPIO.
An example connection could use GPIO27 for OUT, but pick a free pin on your board and avoid the ones used during booting. Test first with a multimeter or a low-current LED, and don’t connect relays or dangerous loads directly to the output.
Configuration with ESPHome
binary_sensor:
- platform: gpio
pin: GPIO27
name: "Hallway motion"
device_class: motion
filters:
- delayed_off: 10s
The filter keeps the state active for a few seconds after the last trigger and stops a light from switching off immediately. The module’s physical time and the software filter add up, so tune both during testing.
Sensitivity and false detections
Mount the sensor at a height and orientation that cover the real zone, not pointing at a window with temperature changes or at a heat source. Lateral movement is usually easier to detect than walking straight towards the sensor.
Start with moderate sensitivity and time. If you turn them up to the maximum, a door, a draught or thermal changes can keep the output active. If you turn them down too far, a person can walk through the zone without triggering the module.
Frequent problems
- OUT always active: wait for the warm-up time after powering it on and reduce the sensitivity or the time.
- OUT never changes: check the power, the ground, the pin and the mode selector.
- The ESP32 restarts: separate the module’s power supply from that of any power actuator.
- The light switches off late: reduce the module time and the
delayed_off. - It doesn’t detect a still person: that’s a PIR limitation, not necessarily a sensor fault.
PIR or mmWave
The PIR is a good option for cheap, simple movement detection. mmWave is more suitable if you need static presence, although it requires more careful tuning and can be more sensitive to the environment. Don’t choose on the datasheet alone: test the sensor in the actual room.
Security
Use the sensor for reversible automations. A movement detection shouldn’t be the only condition for opening a lock, disabling an alarm or switching on a dangerous load. Keep a manual control and check what happens if the ESP32 loses the network.
Sources and next step
Read the ESPHome GPIO sensor documentation and the datasheet of the specific module. For temperature and humidity projects, see DHT11 vs DHT22.