The HC-SR501 is a low-cost PIR (Passive Infrared) motion sensor module that detects changes in infrared energy (movement of warm bodies). The module includes on-board potentiometers to adjust sensitivity and delay time, and a jumper to switch retrigger mode (repeatable / non-repeatable). Ideal for motion sensing, lighting control, intrusion detection, and presence automation.
Typical supply: 5V (many modules work at 3.3V but check your module's silkscreen). Output is a single digital pulse (HIGH when motion detected). This listing includes example code for an ESP32 (Nano/Dev board) and ESPHome integration.
HC-SR501 MODULE PINS -------------------- VCC ---> 5V (or 3.3V on tolerant modules) GND ---> GND OUT ---> Digital input pin on MCU (pull-up not required; module drives output) Typical wiring to ESP32 (Nano / Dev board): HC-SR501 ESP32 ------- ------ VCC --> 5V (or 3V3 if your module supports it) GND --> GND OUT --> GPIO 15 (example - change to any available input) Optional: LED indicator on GPIO (for local visual feedback) LED+ --> GPIO 2 (with resistor) LED- --> GND
Note: Always confirm the VCC range printed on your board. If your MCU is 3.3V-only, prefer a 3.3V-compatible PIR module or use a regulator/level shifter.
| HC-SR501 Pin | ESP32 (example) | Notes |
|---|---|---|
| VCC | 5V (or 3.3V on compatible modules) | Power supply — check board label |
| GND | GND | Common ground |
| OUT | GPIO 15 (example) | Digital input — HIGH when motion detected |
// ESP32 Motion Detection Example — HC-SR501
// Shows basic debounced motion detection and toggles an LED.
// Connect HC-SR501 OUT to PIR_PIN (example GPIO 15).
// Optional: connect an LED (with resistor) to LED_PIN for visual feedback.
const int PIR_PIN = 15; // PIR output
const int LED_PIN = 2; // On-board LED or external (change if needed)
unsigned long lastTrigger = 0;
const unsigned long debounceMs = 500; // ignore triggers shorter than this
volatile bool motionFlag = false;
void IRAM_ATTR handlePIR() {
// ISR sets a flag; keep ISR short
motionFlag = true;
}
void setup() {
Serial.begin(115200);
pinMode(PIR_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
// Attach interrupt for rising edge detection
attachInterrupt(digitalPinToInterrupt(PIR_PIN), handlePIR, RISING);
Serial.println("HC-SR501 PIR Motion Sensor demo");
}
void loop() {
if (motionFlag) {
motionFlag = false; // reset flag
unsigned long now = millis();
// Debounce / rate limit
if (now - lastTrigger > debounceMs) {
lastTrigger = now;
Serial.println("Motion detected!");
// Visual pulse on LED
digitalWrite(LED_PIN, HIGH);
delay(200); // short flash
digitalWrite(LED_PIN, LOW);
// Here you can add actions: MQTT publish, GPIO toggle, web request, etc.
}
}
// low CPU usage; add sleep or other tasks here
delay(10);
}
Tip: You can increase the PIR's built-in delay potentiometer to keep OUT HIGH longer, or decrease sensitivity to avoid false positives.
esphome:
name: pir_room
platform: ESP32
board: esp32dev
wifi:
ssid: "YOUR_SSID"
password: "YOUR_PASS"
binary_sensor:
- platform: gpio
pin:
number: GPIO15
mode: INPUT
inverted: false
name: "PIR Motion"
device_class: motion
filters:
- delayed_on: 50ms # short debounce
- delayed_off: 10s # treat as motion for 10s after last detection
on_press:
then:
- logger.log: "PIR triggered"
logger:
level: INFO
Note: Adjust `delayed_off` to control how long the sensor reports motion after trigger (or use the onboard delay pot).
HC-SR501 PIR Motion Sensor Module — Infrared PIR Detector for ESP32 / Arduino — Motion Presence Detector
HC-SR501, PIR motion sensor, infrared detector, PIR module, motion detector, ESP32 sensor, Arduino PIR, occupancy sensor, presence detector, home automation.
✨ Fast Shipping • 🔒 Secure Checkout