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