ESP32 with PIR Motion Sensor using Interrupts and Timers

This tutorial shows how to detect motion with the ESP32 using a PIR motion sensor. In this example, when motion is detected (an interrupt is triggered), the ESP32 starts a timer and turns an LED on for a predefined number of seconds. When the timer finishes counting down, the LED is automatically turned off.

With this example we’ll also explore two important concepts: interrupts and timers.

Before proceeding with this tutorial you should have the ESP32 add-on installed in your Arduino IDE. Follow one of the following tutorials to install the ESP32 on the Arduino IDE, if you haven’t already.

Watch the Video Tutorial and Project Demo

This tutorial is available in video format (watch below) and in written format (continue reading).

Parts Required

To follow this tutorial you need the following parts

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Introducing Interrupts

To trigger an event with a PIR motion sensor, you use interrupts. Interrupts are useful for making things happen automatically in microcontroller programs, and can help solve timing problems.

With interrupts you don’t need to constantly check the current value of a pin. With interrupts, when a change is detected, an event is triggered (a function is called).

To set an interrupt in the Arduino IDE, you use the attachInterrupt() function, that accepts as arguments: the GPIO pin, the name of the function to be executed, and mode:

attachInterrupt(digitalPinToInterrupt(GPIO), function, mode);

GPIO Interrupt

The first argument is a GPIO number. Normally, you should use digitalPinToInterrupt(GPIO) to set the actual GPIO as an interrupt pin. For example, if you want to use GPIO 27 as an interrupt, use:

digitalPinToInterrupt(27)

With an ESP32 board, all the pins highlighted with a red rectangle in the following figure can be configured as interrupt pins. In this example we’ll use GPIO 27 as an interrupt connected to the PIR Motion sensor.

Function to be triggered

The second argument of the attachInterrupt() function is the name of the function that will be called every time the interrupt is triggered.

Mode

The third argument is the mode. There are 5 different modes:

  • LOW: to trigger the interrupt whenever the pin is LOW;
  • HIGH: to trigger the interrupt whenever the pin is HIGH;
  • CHANGE: to trigger the interrupt whenever the pin changes value – for example from HIGH to LOW or LOW to HIGH;
  • FALLING: for when the pin goes from HIGH to LOW;
  • RISING: to trigger when the pin goes from LOW to HIGH.

For this example will be using the RISING mode, because when the PIR motion sensor detects motion, the GPIO it is connected to goes from LOW to HIGH.

Introducing Timers

In this example we’ll also introduce timers. We want the LED to stay on for a predetermined number of seconds after motion is detected. Instead of using a delay() function that blocks your code and doesn’t allow you to do anything else for a determined number of seconds, we should use a timer.

The delay() function

You should be familiar with the delay() function as it is widely used. This function is pretty straightforward to use. It accepts a single int number as an argument. This number represents the time in milliseconds the program has to wait until moving on to the next line of code.

delay(time in milliseconds)

When you do delay(1000) your program stops on that line for 1 second.

delay() is a blocking function. Blocking functions prevent a program from doing anything else until that particular task is completed. If you need multiple tasks to occur at the same time, you cannot use delay().

For most projects you should avoid using delays and use timers instead.

The millis() function

Using a function called millis() you can return the number of milliseconds that have passed since the program first started.

millis()

Why is that function useful? Because by using some math, you can easily verify how much time has passed without blocking your code.

Blinking an LED with millis()

The following snippet of code shows how you can use the millis() function to create a blink LED project. It turns an LED on for 1000 milliseconds, and then turns it off.

/*********
  Rui Santos
  Complete project details at https://randomnerdtutorials.com  
*********/

// constants won't change. Used here to set a pin number :
const int ledPin =  26;      // the number of the LED pin

// Variables will change :
int ledState = LOW;             // ledState used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change :
const long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the
  // difference between the current time and last time you blinked
  // the LED is bigger than the interval at which you want to
  // blink the LED.
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

View raw code

How the code works

Let’s take a closer look at this blink sketch that works without a delay() function (it uses the millis() function instead).

Basically, this code subtracts the previous recorded time (previousMillis) from the current time (currentMillis). If the remainder is greater than the interval (in this case, 1000 milliseconds), the program updates the previousMillis variable to the current time, and either turns the LED on or off.

if (currentMillis - previousMillis >= interval) {
  // save the last time you blinked the LED
  previousMillis = currentMillis;
  (...)

Because this snippet is non-blocking, any code that’s located outside of that first if statement should work normally.

You should now be able to understand that you can add other tasks to your loop() function and your code will still be blinking the LED every one second.

You can upload this code to your ESP32 and assemble the following schematic diagram to test it and modify the number of milliseconds to see how it works.

Note: If you’ve experienced any issues uploading code to your ESP32, take a look at the ESP32 Troubleshooting Guide.

ESP32 with PIR Motion Sensor

After understanding these concepts: interrupts and timers, let’s continue with the project.

Schematic

The circuit we’ll build is easy to assemble, we’ll be using an LED with a resistor. The LED is connected to GPIO 26. We’ll be using the Mini AM312 PIR Motion Sensor that operates at 3.3V.  It will be connected to GPIO 27. Simply follow the next schematic diagram.

Important: the Mini AM312 PIR Motion Sensor used in this project operates at 3.3V. However, if you’re using another PIR motion sensor like the HC-SR501, it operates at 5V. You can either modify it to operate at 3.3V or simply power it using the Vin pin.

The following figure shows the AM312 PIR motion sensor pinout.

AM312 mini pir pinout

Uploading the Code

After wiring the circuit as shown in the schematic diagram, copy the code provided to your Arduino IDE.

You can upload the code as it is, or you can modify the number of seconds the LED is lit after detecting motion. Simply change the timeSeconds variable with the number of seconds you want.

/*********
  Rui Santos
  Complete project details at https://randomnerdtutorials.com  
*********/

#define timeSeconds 10

// Set GPIOs for LED and PIR Motion Sensor
const int led = 26;
const int motionSensor = 27;

// Timer: Auxiliary variables
unsigned long now = millis();
unsigned long lastTrigger = 0;
boolean startTimer = false;

// Checks if motion was detected, sets LED HIGH and starts a timer
void IRAM_ATTR detectsMovement() {
  Serial.println("MOTION DETECTED!!!");
  digitalWrite(led, HIGH);
  startTimer = true;
  lastTrigger = millis();
}

void setup() {
  // Serial port for debugging purposes
  Serial.begin(115200);
  
  // PIR Motion Sensor mode INPUT_PULLUP
  pinMode(motionSensor, INPUT_PULLUP);
  // Set motionSensor pin as interrupt, assign interrupt function and set RISING mode
  attachInterrupt(digitalPinToInterrupt(motionSensor), detectsMovement, RISING);

  // Set LED to LOW
  pinMode(led, OUTPUT);
  digitalWrite(led, LOW);
}

void loop() {
  // Current time
  now = millis();
  // Turn off the LED after the number of seconds defined in the timeSeconds variable
  if(startTimer && (now - lastTrigger > (timeSeconds*1000))) {
    Serial.println("Motion stopped...");
    digitalWrite(led, LOW);
    startTimer = false;
  }
}

View raw code

Note: if you’ve experienced any issues uploading code to your ESP32, take a look at the ESP32 Troubleshooting Guide.

How the Code Works

Let’s take a look at the code. Start by assigning two GPIO pins to the led and motionSensor variables.

// Set GPIOs for LED and PIR Motion Sensor
const int led = 26;
const int motionSensor = 27;

Then, create variables that will allow you set a timer to turn the LED off after motion is detected.

// Timer: Auxiliar variables
long now = millis();
long lastTrigger = 0;
boolean startTimer = false;

The now variable holds the current time. The lastTrigger variable holds the time when the PIR sensor detects motion. The startTimer is a boolean variable that starts the timer when motion is detected.

setup()

In the setup(), start by initializing the Serial port at 115200 baud rate.

Serial.begin(115200);

Set the PIR Motion sensor as an INPUT PULLUP.

pinMode(motionSensor, INPUT_PULLUP);

To set the PIR sensor pin as an interrupt, use the attachInterrupt() function as described earlier.

attachInterrupt(digitalPinToInterrupt(motionSensor), detectsMovement, RISING);

The pin that will detect motion is GPIO 27 and it will call the function detectsMovement() on RISING mode.

The LED is an OUTPUT whose state starts at LOW.

pinMode(led, OUTPUT);
digitalWrite(led, LOW);

loop()

The loop() function is constantly running over and over again. In every loop, the now variable is updated with the current time.

now = millis();

Nothing else is done in the loop().

But, when motion is detected, the detectsMovement() function is called because we’ve set an interrupt previously on the setup().

The detectsMovement() function prints a message in the Serial Monitor, turns the LED on, sets the startTimer boolean variable to true and updates the lastTrigger variable with the current time.

void IRAM_ATTR detectsMovement() {
  Serial.println("MOTION DETECTED!!!");
  digitalWrite(led, HIGH);
  startTimer = true;
  lastTrigger = millis();
}

Note: IRAM_ATTR is used to run the interrupt code in RAM, otherwise code is stored in flash and it’s slower.

After this step, the code goes back to the loop().

This time, the startTimer variable is true. So, when the time defined in seconds has passed (since motion was detected), the following if statement will be true.

if(startTimer && (now - lastTrigger > (timeSeconds*1000))) {
  Serial.println("Motion stopped...");
  digitalWrite(led, LOW);
  startTimer = false;
}

The “Motion stopped…” message will be printed in the Serial Monitor, the LED is turned off, and the startTimer variable is set to false.

Demonstration

Upload the code to your ESP32 board. Make sure you have the right board and COM port selected.

Open the Serial Monitor at a baud rate of 115200.

Move your hand in front of the PIR sensor. The LED should turn on, and a message is printed in the Serial Monitor saying “MOTION DETECTED!!!”. After 10 seconds the LED should turn off.

Wrapping Up

To wrap up, interrupts are used to detect a change in the GPIO state without the need to constantly read the current GPIO value. With interrupts, when a change is detected, a function is triggered. You’ve also learned how to set a simple timer that allows you to check if a predefined number of seconds have passed without having to block your code.

We have other tutorials related with ESP32 that you may also like:

This is an excerpt from our course: Learn ESP32 with Arduino IDE. If you like ESP32 and you want to learn more, we recommend enrolling in Learn ESP32 with Arduino IDE course.

Thanks for reading.


Learn how to program and build projects with the ESP32 and ESP8266 using MicroPython firmware DOWNLOAD »

Learn how to program and build projects with the ESP32 and ESP8266 using MicroPython firmware DOWNLOAD »


Enjoyed this project? Stay updated by subscribing our weekly newsletter!

22 thoughts on “ESP32 with PIR Motion Sensor using Interrupts and Timers”

    • Hi Ivan.
      You can’t do that! The ESP32 pins operate at 3.3V. You can’t provide 5V to those pins – that will damage your ESP32.
      Regards,
      Sara

      Reply
  1. Hello great tutorial i got it working but now i am working on a button to activate the system od not, as a alarm system do you have any ideas?

    thnx reinier

    Reply
    • Hi.
      You can add a button and declare it as an interrupt. This button will simply change a variable state to “active” or “not active”.
      Then, instead of just looking if motion was detected or not, you also need to check if it is active or not.
      So you’ll need a condition that checks the following:
      if (motion detected and motion sensor is activated) then do something…
      I hope this helps.
      Regards,
      Sara

      Reply
  2. Hi, good tutorial. Surprised no one has commented on the little timing mistake for “Blinking an LED with millis()”. You refer to using 1000 milliseconds, but your code has “const long interval = 5000; // interval at which to blink (milliseconds)”. That’s a 4 second difference. Not a big deal. Just an FYI.

    Reply
  3. Hello Thank you for the Tutorial,

    i have a question how i can blink the Motion led after motion detected? t i need 10 sec. continuous blinking and after 10 sec. stop the blinking.
    Thank you

    Reply
    • Hi Ferdi.
      I think the best way to do that is by creating two timers, one to keep track of the 10 seconds and the other to blink the LED without delay.
      You can find an example to blink the LED without delay in the Arduino examples. In your Arduino IDE, go to File > Examples > 02.Digital > BlinkWithoutDelay
      Regards,
      Sara

      Reply
  4. I tried the same code just changed the pins a little bit. I attached an interrupt to gpio27, led on gpio2 and also changed the ON time of led to 1 second in place of 10. But I get error “Guru Meditation Error: Core 1 panic’ed (Interrupt wdt timeout on CPU1) ” but as soon as I disable Serial.begin function things work fine. Any clue why is this happening?

    Reply
  5. I get an error trying to compile: “expected initializer before ‘detectsMovement'” I’m using a Sparkfun ESP32 Thing. Otherwise everything is as explained in the tutorial. I’ve googled and not been able to find anything for this particular error. Any ideas appreciated, please.

    Reply

Leave a Comment

Download our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.