Motion Detected: ESP32 with PIR Sensor (HC-SR501)
š§ What is a PIR Sensor?
PIR = Passive Infrared Sensor
- A PIR sensorĀ detects motionĀ by measuring changes inĀ infrared radiation.
- Every warm object (like humans and animals) emitsĀ infrared (IR) radiation.
- PIR sensors donāt emit anything; they onlyĀ detectĀ IR changes. Thatās why they are calledĀ passive.
š§Ŗ How Does It Work?
- The PIR sensor has two slots made ofĀ pyroelectric sensors.
- When a human or animal moves in front of the sensor, one slot sees more IR radiation than the other.
- This suddenĀ changeĀ causes aĀ voltage differenceĀ and the sensorĀ outputs HIGH (3.3V or 5V)Ā for a short time.
So in short:
š No motion ā LOW output
š Motion detected ā HIGH output
š§° Components Required
| Component | Quantity |
|---|---|
| ESP32 board | 1 |
| HC-SR501 PIR sensor | 1 |
| Jumper wires | few |
| Breadboard (optional) | 1 |
| LED + 220Ī© resistor (optional) | 1 |
š PIR Sensor Pin Description (HC-SR501)
| Pin | Description |
|---|---|
| VCC | Power supply (3.3V or 5V) |
| OUT | Digital output (HIGH when motion detected) |
| GND | Ground |
There are two knobs on the sensor:
- Time DelayĀ ā Adjust how long output stays HIGH after motion.
- SensitivityĀ ā Adjust how far motion can be detected (typically up to 6 meters).
š Circuit Diagram ā Connect PIR Sensor to ESP32
| PIR Sensor Pin | ESP32 Pin |
|---|---|
| VCC | 3.3V or 5V |
| GND | GND |
| OUT | GPIO 14 (any digital pin) |
If youāre using an LED for indication, connect:
- LED anode (+) ā GPIO 2 (or any other GPIO)
- LED cathode (ā) ā 220Ī© resistor ā GNDš How to Test?
- Upload the code to ESP32.
- OpenĀ Serial MonitorĀ atĀ 115200 baud rate.
- Move your hand in front of the PIR sensor ā you’ll seeĀ
Motion Detected! - Stay still ā you’ll seeĀ
No Motion. - LED should turn ON/OFF according to motion.
ā”ļø No, PIR only detects line-of-sight IR radiation. It cannot see through walls or glass.Q2: Can PIR detect small objects?
ā”ļø It depends on heat and movement. Itās good at detecting humans and large animals.Q3: Is PIR sensor affected by sunlight or heat?
ā”ļø Yes, strong sunlight or high ambient temperature can affect sensitivity.
HOW TO OPERATE
#define pir 12
void setup() {
pinMode(pir,INPUT);
Serial.begin(9600);
Serial.println("Device Started");
Serial.println("Hello Bros");
}
void loop() {
int data = digitalRead(pir);
if(pir==0)
Serial.println("Device is off");
else
Serial.println("Device is on");
}š Upload this using Arduino IDE. Make sure to select the right Board: ESP32 Dev Module and COM port.


Leave a Reply
You must be logged in to post a comment.