Index
Introduction
The PIR (Passive Infrared) sensor detects motion by sensing changes in infrared radiation.
It outputs a digital HIGH signal to the ESP32 when motion is detected, and LOW when idle.
Commonly used in security systems, motion-activated lights, and automation projects.
In this tutorial, we’ll connect a PIR sensor to the ESP32 and detect motion events.
Required Components
- ESP32 Board
- PIR Motion Sensor
- Jumper wires
- Breadboard (optional)
Pinout

Circuit Diagram / Wiring
- PIR VCC → 5V (ESP32)
- PIR GND → GND (ESP32)
- PIR OUT → 14 (ESP32)

Code / Programming
#define PIR_Pin 14 // Use GPIO14 or any other available digital pin
void setup() {
Serial.begin(115200); // Use a higher baud rate for ESP32
pinMode(PIR_Pin, INPUT); // Set PIR pin as input
Serial.println("PIR Sensor Initialized");
}
void loop() {
int val = digitalRead(PIR_Pin); // Read PIR sensor value
if (val == HIGH) {
Serial.println("Motion Detected");
} else {
Serial.println("No motion detected.");
}
delay(1000); // Delay for stability
}
Explanation
- The PIR sensor detects motion by sensing changes in infrared radiation from moving objects.
- It sends a HIGH signal to ESP32 when motion is detected and LOW when idle.
- ESP32 reads this digital signal to trigger actions like alerts, lights, or automation tasks.
Troubleshooting
- If motion is never detected, wait 30–60 seconds after powering up for the PIR sensor to stabilize.
- Ensure the OUT pin is connected to a proper digital GPIO, like GPIO14 (avoid boot-sensitive pins like GPIO0, 2, or 15).
- If false triggers occur, adjust the PIR sensor’s sensitivity and delay knobs to filter out noise.