
The HC-SR501 PIR (Passive Infrared) Motion Sensor is one of the most widely used sensors for detecting human and animal movement. It works by detecting changes in infrared radiation (heat) emitted by living beings. When motion is detected, the sensor outputs a HIGH signal, which can be read by a microcontroller like the ESP8266.
By connecting the PIR sensor to the ESP8266, you can create smart motion detection systems such as security alarms, intruder alerts, and IoT-based motion monitoring projects.
In this tutorial, you will learn how to interface the HC-SR501 PIR motion sensor with ESP8266 and monitor motion detection using the Serial Monitor.

// HC-SR501 PIR Motion Sensor with ESP8266 (Serial Output Only)
int pirPin = D2; // PIR sensor output pin
int pirState = LOW; // Current PIR state
void setup() {
pinMode(pirPin, INPUT); // Set PIR as input
Serial.begin(115200); // Start Serial Monitor
}
void loop() {
int motion = digitalRead(pirPin); // Read PIR sensor state
if (motion == HIGH) {
if (pirState == LOW) {
Serial.println("Motion detected!");
pirState = HIGH;
}
} else {
if (pirState == HIGH) {
Serial.println("Motion ended!");
pirState = LOW;
}
}
}
PIR Pin Setup
int pirPin = D2;
Reading Motion
int motion = digitalRead(pirPin);
Reads whether motion is detected → HIGH or LOW.
State Change Detection
| Issue | Cause | Solution |
|---|---|---|
| PIR not detecting motion | Wrong wiring or insufficient power | Check connections; use 5V if unstable |
| Always HIGH output | Sensor delay set too long | Adjust delay knob |
| Always LOW output | Sensor not aligned properly | Point sensor toward motion area |
| False triggers | Heat sources (sunlight, heater) | Lower sensitivity or reposition sensor |
Yes, it works with 3.3V, but using 5V ensures more stable operation.
It can detect motion within 3 to 7 meters, depending on sensitivity adjustment.
Yes, you can integrate ESP8266 with Blynk, MQTT, or IFTTT to send smartphone notifications.
It may detect heat from sunlight, heaters, or moving curtains. Adjust sensitivity or shield the sensor.
No. PIR detects motion only, not static presence.






