In this esp32 tutorial we will check how to use a PIR sensor to detect motion, using an ESP32 and the Arduino core.
Since a PIR sensor allows to detect motion, it means that we can use it to know when someone enters and leaves a certain area. That information can be useful, for example, to trigger an alarm or to turn on the lights, amongst many other possible application scenarios.
PIR stands for Pyroelectric Infrared (many times, they are also referred as Passive Infrared sensors). This is because their principle of operation is based on the detection of infrared energy emitted by a moving body [1]. This is achieved using a pyroelectric sensor, which produces energy when it is exposed to heat [1].
In this tutorial we will use a DFRobot’s PIR sensor module, which already contains all the electronics we need to connect the sensor to a microcontroller and start using it.
The tests were performed using a DFRobot’s ESP32 module integrated in a ESP32 development board.
As mentioned, we will use a sensor module ready to use, so we don’t need to worry about all the conditioning electronics needed to make it work. Thus, we can directly connect the device to the ESP32.
The device’s principle of operation is very simple. It has a signal pin than will output a voltage of 3.0 V when motion is detected [2]. If we read this value as a digital voltage in the ESP32, it will be considered a digital HIGH voltage level, so it will be really simple to interact with the sensor, as we will see in the code section.
One important thing to mention is that when motion is detected, the output will stay high for 2.3 to 3 seconds [2] after the motion stops.
Regarding the power supply, it can work with voltages of both 3.3 V and 5 V [2]. The device has a detection range of 7 meters and a detection angle of 100º [2].
The size of the module is 30mm × 22mm and it has an indicator LED that lights when motion is detected [2], which is useful to make sure everything is working even if we are not yet reading the output from a microcontroller.
This module is based on the AM412 PIR sensor [2].
The connection diagram of this device to the ESP32 is really simple, since we only need to connect the data pin of the sensor to a digital pin of the microcontroller, as indicated in figure 1.
Figure 1 – Connection diagram between ESP32 and PIR module.
Depending on your ESP32 board, you may be able to directly power the sensor module using a power pin. If you are not sure, my recommendation is to use an external power supply such as this.
The signal, GND and VCC pins are marked as D, – and + respectively, in the PCB, near the connector.
The code for this tutorial will be really simple since we basically only need to read from a digital pin. So, we start by declaring the number of the pin we are going to use as a global variable, so we can easily change it if we need.
int pin = 22;
Moving on to the Arduino setup, we will declare our pin as input, since we are going to read digital values from it. We do this by using the pinMode function, which receives as first input the number of the pin and as second the pin mode (in this case, the pin mode will be INPUT).
pinMode(pin,INPUT);
We will also open a serial connection, so we can output a message whenever motion is detected.
Serial.begin(115200);
Now we will periodically poll the sensor on the main loop, to check if motion is being detected. Note that there are approaches better than polling, such as using interrupts. Nonetheless, to keep this introduction simple and focus on the sensor mode of operation, we will use the polling approach.
So, we will simply read the sensor value using the digitalRead function, which receives the number of the pin as input and returns the state of the pin, either HIGH or LOW.
Since HIGH and LOW are constants that map to 1 and 0 respectively, we can interpret the output of the digitalRead function as a Boolean.
bool isDetected = digitalRead(pin);
Now, if we have detected a HIGH value (which means our Boolean will be true), then we print a message to the serial port indicating a presence was detected.
if(isDetected){ Serial.println("Presence detected"); }
We end the loop by doing a small delay, so we are not constantly polling the sensor.
delay(500);
The final source code can be seen below.
int pin = 22; void setup() { pinMode(pin,INPUT); Serial.begin(115200); } void loop() { bool isDetected = digitalRead(pin); if(isDetected){ Serial.println("Presence detected"); } delay(500); }
To test the code, simply compile it and upload it to your ESP32 device using the Arduino IDE, after connecting the sensor module to the ESP32.
Once the procedure finishes, simply open the Arduino IDE serial monitor. Then, try to move around in front of the sensor and it should start printing the “Presence detected” string to the console, as shown in figure 2.
Figure 2 – ESP32 PIR motion detector program output.