ESP32

ESP32 with PIR Motion Sensor using Interrupts and Timers (Arduino IDE)

Learn how to use a PIR (Passive Infrared) Motion Sensor with the ESP32 programmed with Arduino IDE to detect motion. We’ll create a simple example to introduce you to the concepts of timers and interrupts.

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.

ESP32 with PIR Motion Sensor using Interrupts and Timers

Updated October 27, 2025.

Using MicroPython? Check out this tutorial instead: MicroPython: Interrupts with ESP32 and ESP8266.

Table of Contents

In this tutorial, we’ll cover the following subjects:

Prerequisites

Before proceeding with this tutorial, you should have the ESP32 boards installed in your Arduino IDE. Follow this next tutorial to install the ESP32 on the Arduino IDE, if you haven’t already.

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!

header-200.png?w=828&quality=100&strip=all&ssl=1

Introducing the PIR Motion Sensor

A PIR motion sensor detects changes in infrared light in its field of view caused by movement. This makes it ideal for detecting humans or animals because it will pick up living things (or heat-emitting objects) that move within their range but not inanimate objects.

You can program the ESP32 to react to changes in infrared light by triggering an event such as turning on a light, sounding an alarm, sending a notification, or any other task. In this tutorial, we’ll print a message on the Serial Monitor, and turn on an LED for a predefined number of seconds.

AM312 PIR Motion Sensor Pinout labeledMini AM312 PIR Motion Sensor

There are different PIR motion sensor modules, but all act in a similar way. Usually, they have a power pin, GND, and data.

The PIR motion sensor outputs a HIGH signal on the Data pin when it detects movement, or a LOW signal if it doesn’t. Usually, they only have three pins: VCCGND, and Data.

PIR motion sensor how it works

An important concept about PIR motion sensors is the dwell time (reset time or sensor delay)—it is the duration during which a PIR motion sensor’s output remains HIGH after detecting motion before returning to a LOW state.

Some PIR sensor models, like the HC-SR501, might have two potentiometers (those two orange potentiometers in the picture below) to adjust the sensitivity and the sensor delay:

HC-SR501 Motion SensorHC-SR501 PIR Motion Sensor
  • Sensitivity potentiometer: this adjusts the sensor’s detection range. Clockwise increases sensitivity, counterclockwise decreases it.
  • Time delay potentiometer: this controls how long the sensor remains triggered after detecting motion. Clockwise increases the delay, and counterclockwise decreases it.

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).

Using Interrupts with the ESP32

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 the mode:

attachInterrupt(GPIO), callback_function, mode);

This instruction should be added to the setup() of your Arduino code.

Let’s take a look at the arguments you should pass to that function.

GPIO Interrupt

The first argument of the attachInterrupt() function is the GPIO number where we’ll detect the change. For example, if you want to use GPIO 27 as an interrupt, you can use:

digitalPinToInterrupt(27)

With an ESP32 board, all the pins that can act as inputs can be set as interrupts.

For example, in the case of the ESP32 DOIT V1 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.

Using an ESP32S3 instead? Check this pinout guide: ESP32-S3 DevKitC Pinout Reference Guide: GPIOs Explained.

Callback Function (ISR)

The second argument of the attachInterrupt() function is the name of the function that will be called when the interrupt is triggered. This function is also called interrupt service routine (ISR).

Now, there are a few important rules you should be aware of when defining your ISR (callback function).

  1. The ISR should not return anything.
  2. ISRs should be as short and fast as possible because they halt the normal execution of the code.
  3. They should have the ARDUINO_ISR_ATTR attribute, so that they run in the ESP32 Internal RAM and not in Flash. IRAM access is much faster, which is critical for ISRs to run reliably without timing issues or crashes during interrupts.
  4. Variables that are used inside ISRs and throughout the code should preferably be volatile. This prevents the compiler from caching values in registers (and skipping memory access), so reads/writes always access the actual memory location and reflect unexpected changes caused by the interrupt.

Here’s an example of an ISR so that you can check its syntax:

void ARDUINO_ISR_ATTR my_callback() {
    // Any code you want to run
}

Another important thing about ISRs is that you should keep their code as fast and simple as possible and avoid things like complex operations, writing to the Serial Monitor, or using delay(). Instead, you should use a flag or counter to indicate that the interrupt happened, and then handle whatever you need to do in the main code or loop() section.

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.

The following picture will help you better understand the different trigger modes.

Interrupt modes

For this example we’ll 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 call 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() is preferred over using delay(). The millis() function returns the number of milliseconds that have passed since the program first started.

millis()

Why is that function useful? By using some math, you can easily check how much time has passed since a certain event 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.

ESP32 with PIR Motion Sensor: Detecting Motion

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

We’ll create a simple example that will light up an LED when motion is detected. After learning how it works, the same way of thinking can be applied to useful applications, such as sending an email or triggering an alarm.

ESP32 with a PIR Motion Sensor Example Overview

Here’s how the example works:

  • The sensor detects motion.
  • The ESP32 detects this event.
  • It prints in the Serial Monitor that motion was detected.
  • It turns on an LED for 20 seconds.
  • During those 20 seconds, we don’t print anything else to the Serial Monitor.
  • After those 20 seconds and if motion was not detected, we turn off the LED, print a message to the Serial Monitor indicating that motion has stopped.

Circuit Diagram

For this example, you need to connect the PIR motion sensor and an LED to your board.

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. You can 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.

Code – ESP32 with a PIR Motion Sensor: Detect Motion

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 & Sara Santos - Random Nerd Tutorials
  Complete project details at https://RandomNerdTutorials.com/esp32-pir-motion-sensor-interrupts-timers/
  ESP32 GPIO Interrupts with Arduino IDE: https://RandomNerdTutorials.com/esp32-gpio-interrupts-arduino/
*********/
#include <Arduino.h>

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

// Timer: Auxiliary variables
unsigned long now;
volatile unsigned long lastTrigger = 0;
volatile bool startTimer = false;

bool printMotion = false;

const unsigned long timeSeconds = 20 * 1000UL;  //20 seconds in milliseconds

void ARDUINO_ISR_ATTR motionISR() {
  lastTrigger = millis();
  startTimer = true;
}

void setup() {
  Serial.begin(115200);
  pinMode(motionSensor, INPUT_PULLUP);
  attachInterrupt(motionSensor, motionISR, RISING);

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

void loop() {
  now = millis();

// Turn LED on immediately on new trigger
  if (startTimer && !printMotion) {
    digitalWrite(led, HIGH);
    Serial.println("MOTION DETECTED!!!");
    printMotion = true;
  }

// Turn off the LED after timeout
  if (startTimer && (now - lastTrigger > timeSeconds)) {
    Serial.println("Motion stopped...");
    digitalWrite(led, LOW);
    startTimer = false;
    printMotion = 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 Does the Code Work?

Let’s take a quick look at the code to better understand how it works.

Defining Variables

We start by defining the pins for the LED and PIR Motion sensor. Adjust if you’re using different pins:

const uint8_t led = 26;
const uint8_t motionSensor = 27;

Create variables to track the duration of the LED in the on state. The now variable saves the current time (time elapsed since the program has started), the lastTrigger saves the last time motion was detected, and the startTimer is a boolean variable to indicate whether the timer to turn on the LED is currently running or not.

unsigned long now;
volatile unsigned long lastTrigger = 0;
volatile bool startTimer = false;

We also have another variable to keep track whether the Motion Detected text was already printed to the Serial Monitor.

bool printMotion = false;

The timeSeconds variable saves how long we want the LED on after motion is detected. You can adjust according to your preferences.

const unsigned long timeSeconds = 20 * 1000UL;  //20 seconds in milliseconds

motionISR()

The motionISR() will run when motion is detected. We save the current time on the lastTrigger variable to keep track when motion was detected, and we set the startTimer variable to true to indicate it’s time to start the timer to turn on the LED.

void ARDUINO_ISR_ATTR motionISR() {
  lastTrigger = millis();
  startTimer = true;
}

We’ll then handle these variables in the loop() to do the tasks we want.

setup()

In the setup(), set the motion sensor as an interrupt on RISING mode (when motion is detected, the sensor sets its output pin to HIGH).

pinMode(motionSensor, INPUT_PULLUP);
attachInterrupt(motionSensor, motionISR, RISING);

And set the LED as an OUTPUT and set it to LOW.

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

loop()

In the loop(), we’re constantly getting the current time and saving it in the now variable.

now = millis();

Then, we check whether the LED timer has started and if the motion message has not already been printed. If these conditions are met, we turn the LED on, print a message to the Serial Monitor, and set the printMotion variable to true, because we have now printed the Motion Detected message to the Serial Monitor.

if (startTimer && !printMotion) {
  digitalWrite(led, HIGH);
  Serial.println("MOTION DETECTED!!!");
  printMotion = true;
}

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. Press the ESP32 RST button so that it starts running the code.

Move your hand in front of the PIR sensor.

Moving my hand in front of the PIR motion sensor connected to the ESP32

The LED should turn on, and a message is printed in the Serial Monitor saying “MOTION DETECTED!!!”.

ESP32 PIR Motion Sensor with Interrupts - Messages on Serial Monitor

After 20 seconds, the LED should turn off (if motion was not detected meanwhile).

ESP32 on a breadboard connected to an LED and a PIR motion sensor

Now that you understand how to use interrupts to detect motion with a PIR motion sensor, you can easily adjust the code to do any useful tasks instead of controlling an LED.

You can, for example, send notifications to your email or smartphone to indicate that motion was detected. We have a tutorial with seven different ways to send notifications with the ESP32 that you can explore:

Watch the Video Tutorial and Project Demo

This tutorial is also available in video format (watch below). Note that the video format might be outdated.


Upcoming Course
Upcoming Course
Learn More
Instructor Tips
Instructor Tips
View Tips
Join Community
Join Community
Join Now