pir motion sensor arduino interrupt example

PIR Motion Sensor with Arduino: Interrupt Example

By Anthony Harris
AD

In the vast realm of DIY electronics and automation, Arduino has become a cornerstone for hobbyists and professionals alike. One of its many accolades is the ability to interface with various sensors, significantly extending the Microcontroller's capabilities. Among these, the PIR (Passive Infrared) motion sensor is a popular choice for detecting movement, triggering events in home automation setups, or gaming projects. Today, we'll explore an engaging, step-by-step guide on implementing a PIR motion sensor with Arduino using interrupts, a feature that enhances the board's responsiveness and efficiency.

The PIR motion sensor, also known as a "passive infrared sensor," doesn't emit any radiation. Instead, it detects infrared radiation emitted by objects within its field of view. When an object moves, the sensor's output changes, signaling the Arduino to react accordingly. In this tutorial, we'll set up a simple PIR motion sensor using an HC-SR501 module, one of the most popular and budget-friendly options, and demonstrate how to wire it with Arduino, configure it for motion detection, and implement two crucial aspects: threshold tuning and interrupt-driven processing.

Wiring the HC-SR501 PIR Motion Sensor with Arduino

To start, let's connect the HC-SR501 PIR motion sensor to the Arduino board. The sensor has four pins: VCC, GND, OUT, and LED. Connect VCC to 5V and GND to the ground pin on the Arduino. The OUT pin connects to one of the Arduino's digital pins, say D2. We'll use D3 for the built-in LED to indicate motion detection.

AD
ESP32 PIR Motion Sensor – Detect Motion with Arduino IDE
ESP32 PIR Motion Sensor – Detect Motion with Arduino IDE
the instructions for how to use an arduino pir and motion sensor with arduino
the instructions for how to use an arduino pir and motion sensor with arduino
Arduino - Czujnik ruchu PIR - Tranzystor.pl - Vortal Elektroniczny
Arduino - Czujnik ruchu PIR - Tranzystor.pl - Vortal Elektroniczny

For this project, ensure you're using an Arduino board with sufficient pins or use an extended board like the Arduino Mega to accommodate more sensors and peripherals in future projects. Now, let's configure the Arduino IDE and begin coding.

Configuring the Arduino IDE and Setting up Libraries

To simplify the process, we'll use theJud Kaiser's PIR library, which provides functions to set the sensitivity and trigger duration of the HC-SR501 sensor, among other features. First, install the library via Sketch > Include Library > Manage Libraries, then search for "Kno тебя&Simon" and install the "PIR_Sensor" library.

Interface HC-SR501 PIR Sensor with Arduino
Interface HC-SR501 PIR Sensor with Arduino
How to Use a PIR Sensor for Motion sensing using Arduino
How to Use a PIR Sensor for Motion sensing using Arduino
Trigger Sound Effects with Arduino & PIR Motion Sensor
Trigger Sound Effects with Arduino & PIR Motion Sensor

Next, include the library in your sketch: `#include `. Define a PIR sensor object with the connected pin (D2) and initialize it in the setup function. Set the motion detection mode to low/active (INTRUSIVE) and enable the internal pull-up resistor for better noise reduction.

Setting Thresholds and Trigger Duration

One of the HC-SR501's outstanding features is itsensitivity adjustment. By default, the sensor is set to a medium sensitivity, but you can fine-tune it according to your needs. To do so, use the library's `setSensitivity()` function, with values ranging from 0 to 128 (0 being the most sensitive and 128 the least). Keep in mind that increasing the sensitivity may lead to false triggering from subtle environmental changes or small animals.

AD

The trigger duration can also be adjusted using the `setTriggerDuration()` function, with values ranging from 0 to 128 (0 being the quickest trigger and 128 the slowest). Balancing sensitivity and trigger duration ensures the sensor doesn't miss rapid movements or trigger excessively due to minor environmental changes. Fine-tune these settings based on your specific use case for optimal performance.

Arduino with PIR Motion Sensor
Arduino with PIR Motion Sensor
ESP8266 Interrupts and Timers Arduino IDE - PIR Motion Sensor Example
ESP8266 Interrupts and Timers Arduino IDE - PIR Motion Sensor Example
PIR Motion Sensor with Arduino Board: A Step-by-Step Guide
PIR Motion Sensor with Arduino Board: A Step-by-Step Guide
Arduino Projects: PIR Motion Sensor - Tutorial45
Arduino Projects: PIR Motion Sensor - Tutorial45
Raspberry Pi: Detect Motion using a PIR Motion Sensor with Python | Random Nerd Tutorials
Raspberry Pi: Detect Motion using a PIR Motion Sensor with Python | Random Nerd Tutorials
ESP32 Interrupts and Timers with PIR Sensor using Arduino IDE
ESP32 Interrupts and Timers with PIR Sensor using Arduino IDE

Implementing Interrupt-driven Processing

Interrupts are a powerful feature of electronics programming that enable real-time handling of hardware events, like the PIR sensor triggering. To illustrate this, we'll implement both the rising edge (when motion is detected) and falling edge (when motion ends) interrupts, enabling instant reactions to sensor events.

The Arduino Uno contains two types of interrupts: external and event. The external interrupts are dedicated to processing hardware-generated events, while event interrupts are triggered internally by specific conditions like timers. To use interrupts, include the library to disable sleep modes and utilize the available interrupt functions: `attachInterrupt()` and `detachInterrupt()`.

AD

Rising Edge Interrupt (Motion Detection)

To capture the rising edge ( altaertweak), we'll use the `attachInterrupt()` function with the interrupt mode set to RISING. In the interrupt service routine (ISR), we'll toggle the built-in LED (D3) to display motion detection and print relevant information to the Serial Monitor. To prevent the ISR from executing repeatedly due to noise, add a delay of a few milliseconds before clearing the interrupt flag.

ESP32 with PIR Motion Sensor using Interrupts and Timers | Random Nerd Tutorials
ESP32 with PIR Motion Sensor using Interrupts and Timers | Random Nerd Tutorials
PIR Sensor
PIR Sensor
ESP32 with PIR Motion Sensor using Interrupts and Timers | Random Nerd Tutorials
ESP32 with PIR Motion Sensor using Interrupts and Timers | Random Nerd Tutorials
PIR Motion Sensor With Arduino & LED
PIR Motion Sensor With Arduino & LED
12v 100Ah Automatic Battery Charge Control Circuit
12v 100Ah Automatic Battery Charge Control Circuit
ESP32 GPIO Interrupts with Arduino IDE | Random Nerd Tutorials
ESP32 GPIO Interrupts with Arduino IDE | Random Nerd Tutorials
the circuit diagram for a motion sensor that is connected to a light switch with two wires
the circuit diagram for a motion sensor that is connected to a light switch with two wires
How to Use Pir Sensor Without Arduino
How to Use Pir Sensor Without Arduino
Arduino Projects: PIR Motion Sensor - Tutorial45
Arduino Projects: PIR Motion Sensor - Tutorial45
ESP32 External Interrupts using Arduino IDE
ESP32 External Interrupts using Arduino IDE
Adafruit Learning System
Adafruit Learning System
an electronic circuit with two batteries attached to the board and another device connected to it
an electronic circuit with two batteries attached to the board and another device connected to it
5 Simple Motion Detector Circuits using PIR - Homemade Circuit Projects
5 Simple Motion Detector Circuits using PIR - Homemade Circuit Projects

For better performance, minimize the ISR's workload by offloading heavy computations to the main loop. Be cautious when employing delays or blocking functions in the ISR, as they can cause mischief by preventing the ISR from executing or leading to unpredictable behavior.

Falling Edge Interrupt (Motion End Detection)

Similarly, configure a falling edge interrupt for detecting when motion ends. In the corresponding ISR, display the motion cessation by turning off the built-in LED (D3) and print relevant information to the Serial Monitor. Again, employ a short delay before clearing the interrupt flag and ensure minimal ISR workload.

AD

To make your project more robust, use appropriate debugging techniques like Serial.print statements and LEDs to understand and tackle potential issues promptly. Persistence and patience are invaluable allies in electronics projects.

After mastering these concepts, you can expand your setup by adding more PIR sensors, implementing complex triggers, or integrating it into a broader home automation or IoT project using platforms like Blynk or Home Assistant. The sky's the limit once you've got the fundamentals down! Happy hacking!

AD