ESP32 PIR Motion Sensor: Detect Movement with the HC-SR501

In this tutorial, you’ll learn how to interface the HC-SR501 PIR motion sensor with an ESP32 using the Arduino IDE. This is a great project for home automation, security systems, or any other DIY IoT project that needs motion detection.

We will look at wiring, how the ESP32 PIR motion sensor works, some example code, and how to make it more efficient using interrupts.

Table of Contents

    How Does the HC-SR501 PIR Motion Sensor Work?

    A PIR (passive infrared) sensor, like the HC-SR501, uses a pair of infrared sensors to detect movement of infrared-emitting objects, like humans or animals, across its field of view.

    Humans and animals emit infrared radiation due to their body heat. The warmer an object is, the more its particles move, causing it to emit electromagnetic waves of the infrared spectrum, also known as thermal or heat radiation. The HC-SR501 has two slots in its sensor element to detect these IR changes.

    When the sensor is idle, both slots detect the same level of infrared radiation. When a warm object moves across the sensor’s field of view, one slot sees a change before the other. This difference in IR levels is detected and amplified, causing the sensor to output a HIGH signal to indicate motion.

    HC-SR501 Adjustable Settings

    The PIR sensor module includes two potentiometers that allow us to tweak the sensitivity and the time delay between detections.

    HC-SR501 Adjustment Potentiometers

    The sensitivity potentiometer adjusts the sensor’s detection range. Turning the potentiometer clockwise increases the range, and turning it counterclockwise decreases it.

    The time delay potentiometer, on the other hand, controls how long the output stays HIGH after motion is detected. Turning the potentiometer clockwise increases the delay and time, while turning counterclockwise decreases it. This can be adjusted from about 3 seconds up to 300 seconds.

    Components Needed

    Here’s a list of the parts you need to follow along with this guide:

    Wiring the HC-SR501 to the ESP32

    ESP32 PIR Motion Sensor HC-SR501  wiring diagram
    Made with Wokwi
    HC-SR501 PinESP32 Pin
    GNDGND
    OUTAny GPIO pin (e.g., GPIO 4)
    VCC3V3 (3.3V)

    The HC-SR501 is designed for 5V operation, but usually works fine with the ESP32’s 3.3V pin. If your sensor is unreliable, consider connecting the sensor to the VIN (5V) pin and using a voltage divider.

    Development Environment Setup

    Luckily, we don’t need any additional libraries to use the HC-SR501 motion sensor.

    The only thing you need to do is set up the Arduino IDE or PlatformIO to work with the ESP32. Click the corresponding link if you need help doing that.

    ESP32 PIR Motion Sensor Example Sketch

    Use the following sketch to quickly get started with detecting motion using the HC-SR501 sensor.

    #define PIR_PIN 4   // GPIO pin connected to HC-SR501 OUT
    #define LED_PIN 2    // Built-in LED for indication
    
    void setup() {
      pinMode(PIR_PIN, INPUT);
      pinMode(LED_PIN, OUTPUT);
      Serial.begin(115200);
      Serial.println("PIR Sensor Ready");
    }
    
    void loop() {
      int motion = digitalRead(PIR_PIN);
    
      if (motion == HIGH) {
        digitalWrite(LED_PIN, HIGH);
        Serial.println("Motion detected!");
      } else {
        digitalWrite(LED_PIN, LOW);
      }
    
      delay(100);
    }

    This code checks every 100 milliseconds whether the HC-SR501 pulled the GPIO pin HIGH. For more complex projects, you should use a more efficient way of detecting movement.

    When movement is detected, the built-in LED of the ESP32 will blink, and there will be an output in the serial monitor.

    Use PIR Motion Sensor Interrupts – Sketch

    To detect movement using the ESP32 and HC-SR501 PIR sensor more efficiently, we will use interrupts instead of continuously checking for updates.

    Interrupts allow your ESP32 to sleep or perform other tasks and react only when motion is detected. Here’s how to use interrupts:

    #define PIR_PIN 4
    #define LED_PIN 2
    
    volatile bool motionDetected = false;
    
    void IRAM_ATTR handleInterrupt() {
      motionDetected = true;
    }
    
    void setup() {
      pinMode(PIR_PIN, INPUT);
      pinMode(LED_PIN, OUTPUT);
      attachInterrupt(digitalPinToInterrupt(PIR_PIN), handleInterrupt, RISING);
      Serial.begin(115200);
      Serial.println("PIR Sensor with Interrupt Ready");
    }
    
    void loop() {
      if (motionDetected) {
        motionDetected = false;
        digitalWrite(LED_PIN, HIGH);
        Serial.println("Motion detected (interrupt)!");
        delay(500);  // LED on for 0.5s
        digitalWrite(LED_PIN, LOW);
      }
    }

    Using interrupts offers several advantages. First, it significantly lowers power consumption since the ESP32 doesn’t need to constantly poll the sensor. Instead, it can remain idle or in sleep mode until motion is actually detected. This makes the system more efficient and frees up the CPU to perform other tasks or save power.

    ESP32 PIR Motion Sensor Project Ideas

    Together with the ESP32 microcontroller, the HC-SR501 PIR motion detection sensor can be used in many different and exciting IoT projects.

    In home automation, for example, it is commonly used to trigger smart lights when someone enters a room.

    Also, it is used a lot in security systems. The sensor can serve as a motion-activated alarm or trigger camera recordings when movement is detected.

    You can also integrate it with ESP32’s Wi-Fi capabilities to send alerts to your phone via email, Telegram, or a web dashboard.

    Wrapping Up

    You’ve successfully connected a PIR motion sensor to an ESP32 and created a basic motion detection system. This simple setup can serve as the foundation for more advanced home automation or security systems.


    Share your experience with PIR Motion Sensors in the comments below!

    Thanks for reading!

    Links marked with an asterisk (*) are affiliate links which means we may receive a commission for purchases made through these links at no extra cost to you. Read more on our Affiliate Disclosure Page.

    Share this article

    Leave a Reply

    Your email address will not be published. Required fields are marked *