Back to Tutorial

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

ComponentQuantity
ESP32 board1
HC-SR501 PIR sensor1
Jumper wiresfew
Breadboard (optional)1
LED + 220Ω resistor (optional)1

📟 PIR Sensor Pin Description (HC-SR501)

PinDescription
VCCPower supply (3.3V or 5V)
OUTDigital output (HIGH when motion detected)
GNDGround

There are two knobs on the sensor:

  1. Time Delay – Adjust how long output stays HIGH after motion.
  2. Sensitivity – Adjust how far motion can be detected (typically up to 6 meters).

🔌 Circuit Diagram – Connect PIR Sensor to ESP32

PIR Sensor PinESP32 Pin
VCC3.3V or 5V
GNDGND
OUTGPIO 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?
    1. Upload the code to ESP32.
    2. Open Serial Monitor at 115200 baud rate.
    3. Move your hand in front of the PIR sensor — you’ll see Motion Detected!
    4. Stay still — you’ll see No Motion.
    5. LED should turn ON/OFF according to motion.
    💡 Real-World Project IdeasProject IdeaDescription🔐 Security AlarmTrigger buzzer or send message when motion detected💡 Smart LightingTurn on lights only when someone is in the room🧠 IoT NotificationUse WiFi to send alerts to mobile or Telegram bot🛌 Sleep MonitorTurn off fan/light if no motion detected for 1 hour🐶 Pet DetectorKnow when your pet enters a room❓ FAQsQ1: Can PIR detect behind walls?
    ➡️ 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.

Motion Detected: ESP32 with PIR Sensor (HC-SR501)


Share this post

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to Tutorial