Every project that we invent or create is built with an intent to solve an existing issue. We take various inputs from the environment and after sensing them we perform a given action. Now let’s take an example.
Simplified
Security System : A person trying to enter from your home window should set an alarm to alert the house owner (that’s you!). In this case we will be using a sensor to detect the person entering from the window (condition) and later set the alarm ON that is the part of the given action.
In this project we are going to use the PIR (Passive Infrared) sensor that will be used to detect the intruder that will trigger an LED that will be set to ON for a particular interval and then the LED will be turned OFF.
- Installation of Arduino for ESP32
- Basic Microcontroller Knowledge Recommended
What is an Interrupt?
- It is a signal that is detected by the sensor and is transmitted to the microcontroller.
- With interrupts we can trigger or set the output device
What is a Timer?
- A timer is an existing clock that does the work of counting time.
- With Timers we can set how long the alarm should ring after the alarm is triggered.
Working Principle
A PIR sensor is very complex compared to other sensors like photocells, FSR’s (Force Sensitive Resistors) etc. The PIR sensor will be triggered when there is differential change between two halves of its area of detection. A Human being, animal that emits heat/Passive Infrared will trigger the sensor. This sensor then will send the trigger to the ESP32 which will then detect that the signal is received and set the buzzer to perform a particular function.
PIR is the infrared that is emitted by the human body, which is also emitted by the sunlight. This sensor when exposed to the sunlight can malfunction and trigger that motion is detected.
Components Required
- ESP32
- PIR sensor (HC-SR501)
- LED
- 330 ohm resistor
- Jumper wires
- Breadboard
How does the PIR (Passive Infrared) Sensor Work?
PIR sensor has a pair of pyroelectric sensors to sense heat energy from the surrounding environment. These sensors are placed closed to each other and when there is a difference in the signal, these sensors will trigger a signal as motion is detected. There is a white covering or the Fresnel’s lens which widens the sensing area.
PIR – Pin Diagram
- Vcc pin supplies power for the sensor. Connected to 3.3V on the ESP32.
- SIG pin is used to communicate between the ESP32 and the sensor
- GND is the ground pin. Common grounded with the ESP32.
How does the PIR work with the ESP32?
The PIR sends a digital signal to the ESP32 and then the ESP32 on sensing the trigger performs the required action. In our case the PIR trigger the LED ON and prints “Motion detected” on the serial monitor.
Circuit Diagram
Working Principle
PIR Sensor with Interrupts
The PIR sensor connected to the pin 19 works in the rising mode. A few of the modes are listed below
- 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 state of the pin changes. LOW→ HIGH or HIGH→ LOW
- FALLING : when the pin goes from HIGH to LOW
- RISING : when the pin goes from LOW to HIGH
Code Simplified
To set an interrupt, we use the attachInterrupt() function which accepts arguments like the GPIO pin, function and type of mode. We are using GPIO14 as an interrupt connected to the PIR Motion Sensor.
GPIO Interrupt
The first argument is a GPIO number. Normally you should use digitalPinToInterrupt(GPIO) to set the actual GPIO as an interrupt pin.
#define time_in_seconds 10
// GPIOs for LED and PIR Motion Sensor
const int buzzer= 21;
const int motionSensor = 19;
// Auxiliary variables of the Timer
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 detectmotion() {
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), detectmotion, RISING);
// Set LED to LOW
pinMode(buzzer, OUTPUT);
digitalWrite(buzzer, LOW);
}
void loop() {
// Current time
now = millis();
// Turn off the Buzzer after the number of seconds defined in the time_in_seconds variable
if(startTimer && (now - lastTrigger > (time_in_seconds*1000))) {
Serial.println("No Motion...");
digitalWrite(buzzer, LOW);
startTimer = false;
}
}
Conclusion
We need to continuously read the GPIO pins to find if there is an interrupt. The Interrupts are used to detect the change in the GPIO pins and execute or trigger a function defined in the code. The timer and interrupts are very basic and the most simple logics are used for normal cases. But in a few advanced tutorials we will be using different types of triggers.