pir sensor arduino example

Arduino Pir Sensor Tutorial: Example Projects & Setup

By Laura Brown
AD

Looking to unravel the intricacies of environmental monitoring with Arduino? The Pir (Passive Infrared) sensor is an excellent choice for detecting motion, offering a wide range of applications from security systems to automated lighting. In this comprehensive guide, we'll explore how to use the Pir sensor with Arduino, delving into the mechanics, setup, and code examples to help you get started.

The Pir sensor is an essential component in many Arduino projects, leveraging its ability to sense temperature changes caused by motion. It is a passive sensor, meaning it doesn't emit any energy; instead, it detects the energy emitted by objects in its field of view. By learning how to use the Pir sensor with Arduino, you'll gain the capability to create sophisticated motion-detection systems.

Pir Sensor Arduino Examples

Before diving into Arduino examples, let's first understand the Pir sensor's basic setup and components. The Pir sensor consists of a pyroelectric element, which changes its resistance when exposed to changes in temperature. These resistance variations are converted into electrical signals, which are then read by the Arduino microcontroller.

AD
Arduino - Czujnik ruchu PIR - Tranzystor.pl - Vortal Elektroniczny
Arduino - Czujnik ruchu PIR - Tranzystor.pl - Vortal Elektroniczny
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
How to Use a PIR Sensor for Motion sensing using Arduino
How to Use a PIR Sensor for Motion sensing using Arduino

The Pir sensor typically includes Fresnel lenses to focus the infrared radiation towards the detector, increasing its sensitivity. Additionally, it features an integrated signal processing module that filters out noise and sets a sensitivity threshold for motion detection.

Pir Sensor Pinout

To connect the Pir sensor to your Arduino board, you'll need to identify its pinout. The Pir sensor usually has three pins: VCC (power supply), GND (ground), and OUT (signal). The VCC and GND pins connect to the power supply and ground pins on the Arduino board, respectively. The OUT pin is an active HIGH open-collector pin that goes HIGH when motion is detected.

Interface PIR Sensor to Arduino - Motion Sensor/Detector
Interface PIR Sensor to Arduino - Motion Sensor/Detector
PIR Sensor Arduino Alarm | Make:
PIR Sensor Arduino Alarm | Make:
PIR Sensor Datasheet, Pinout Specifications, Working - Homemade Circuit Projects
PIR Sensor Datasheet, Pinout Specifications, Working - Homemade Circuit Projects

The Pir sensor's sensitivity can be adjusted using a potentiometer located on the sensor board. Turning the potentiometer clockwise increases sensitivity, while counter-clockwise reduces it. Adjusting the sensor's sensitivity is crucial for optimal performance, ensuring it detects motion accurately while minimizing false positives.

Using the Pir Sensor with Arduino

Now that we've explored the Pir sensor's anatomy and setup, let's discuss how to use it with Arduino. We'll provide a step-by-step guide along with a basic Arduino example demonstrating how to read and interpret signals from the Pir sensor.

AD

To start, connect the Pir sensor to your Arduino board as follows:

Arduino with PIR Motion Sensor | Random Nerd Tutorials
Arduino with PIR Motion Sensor | Random Nerd Tutorials
Trigger Sound Effects with Arduino & PIR Motion Sensor
Trigger Sound Effects with Arduino & PIR Motion Sensor
ESP32 PIR Motion Sensor – Detect Motion with Arduino IDE
ESP32 PIR Motion Sensor – Detect Motion with Arduino IDE
Arduino Projects: PIR Motion Sensor - Tutorial45
Arduino Projects: PIR Motion Sensor - Tutorial45
the instructions for how to make an automatic room light using arduino and pir sensor
the instructions for how to make an automatic room light using arduino and pir sensor
an electronic circuit board with parts labeled
an electronic circuit board with parts labeled
  • Pir sensor VCC to Arduino 5V
  • Pir sensor GND to Arduino GND
  • Pir sensor OUT to Arduino digital pin (e.g., 2)

Once connected, upload the following Arduino sketch to your board. This example will continually monitor the Pir sensor's output and print "Motion detected" to the Serial Monitor whenever motion is detected.

```cpp const int pirPin = 2; bool motionDetected = false; void setup() { Serial.begin(9600); pinMode(pirPin, INPUT); } void loop() { motionDetected = digitalRead(pirPin); if (motionDetected) { Serial.println("Motion detected!"); } } ```

After uploading the code, open the Serial Monitor (Ctrl+Shift+M) and set the baud rate to 9600. You should now see "Motion detected" printed whenever the Pir sensor detects motion in its field of view.

AD

Practical Applications and More Arduino Examples

Now that you've got a grasp on using the Pir sensor with Arduino let's explore some practical applications and additional examples to expand your project possibilities.

PIR Motion Sensor on Raspberry Pi Pico Using MicroPython
PIR Motion Sensor on Raspberry Pi Pico Using MicroPython
Arduino Interrupts with PIR Motion Sensor — Maker Portal
Arduino Interrupts with PIR Motion Sensor — Maker Portal
How to Use Pir Sensor Without Arduino
How to Use Pir Sensor Without Arduino
Arduino Basics: PIR Sensors
Arduino Basics: PIR Sensors
an electronic device with wires attached to it and the words easy arduino pir sensor
an electronic device with wires attached to it and the words easy arduino pir sensor
PIR Sensor Datasheet, Pinout Specifications, Working - Homemade Circuit Projects
PIR Sensor Datasheet, Pinout Specifications, Working - Homemade Circuit Projects
Interface HC-SR501 PIR Sensor with Arduino
Interface HC-SR501 PIR Sensor with Arduino
How PIR Sensor Works and How To Use It with Arduino
How PIR Sensor Works and How To Use It with Arduino
Arduino - Mini alarm PIR HC-SR501 - Tranzystor.pl - Vortal Elektroniczny
Arduino - Mini alarm PIR HC-SR501 - Tranzystor.pl - Vortal Elektroniczny
HC-SR505 Mini PIR Motion Sensor with Arduino
HC-SR505 Mini PIR Motion Sensor with Arduino
Arduino + Sensor PIR
Arduino + Sensor PIR
Motion detection alarm using a PIR sensor module with a PIC Microcontroller
Motion detection alarm using a PIR sensor module with a PIC Microcontroller
Sensore PIR HC-SR501
Sensore PIR HC-SR501
Arduino with PIR Motion Sensor | Random Nerd Tutorials
Arduino with PIR Motion Sensor | Random Nerd Tutorials

One popular application is creating an automated lighting system. When integrated with a relay module, the Pir sensor can turn on lights when it detects motion in a room, saving energy and providing convenience. To achieve this, you can modify the previous example to control a relay instead of printing to the Serial Monitor.

Another compelling application is a motion-activated camera. By combining the Pir sensor with a camera module, you can automatically capture photos or videos whenever motion is detected, making it an ideal solution for surveillance or wildlife photography.

AD

Adjusting Sensitivity and Debouncing

As mentioned earlier, adjusting the Pir sensor's sensitivity is essential for optimal performance. However, even with the sensitivity set appropriately, false positives may still occur due to the sensor's inherent delay in stabilizing after detecting motion. To mitigate this, you can implement debouncing by introducing a delay after detecting motion before printing or controlling output.

Here's an updated example incorporating a delay to debounce the sensor's output:

```cpp const int pirPin = 2; const int delayTime = 5000; // Delay time in milliseconds (5 seconds) unsigned long prevMillis = 0; void setup() { Serial.begin(9600); pinMode(pirPin, INPUT); } void loop() { if (digitalRead(pirPin) == HIGH) { if (millis() - prevMillis > delayTime) { Serial.println("Motion detected!"); prevMillis = millis(); } } } ```

This example features a 5-second delay (adjustable) after detecting motion before printing "Motion detected". This reduction in false positives enhances the sensor's overall reliability and accuracy.

Calculating Motion Duration

For more advanced applications, you might want to track the duration of detected motion. To achieve this, you can measure the time between the start and end of motion detection events. Here's an example demonstrating how to calculate and print the motion duration:

```cpp const int pirPin = 2; const int motionDurationThreshold = 10000; // Minimum duration to consider motion (10 seconds) void setup() { Serial.begin(9600); pinMode(pirPin, INPUT); } void loop() { static bool motionDetected = false; static unsigned long motionStartTime = 0; if (digitalRead(pirPin) == HIGH && !motionDetected) { motionDetected = true; motionStartTime = millis(); } else if (digitalRead(pirPin) == LOW) { motionDetected = false; } if (motionDetected && millis() - motionStartTime > motionDurationThreshold) { Serial.print("Motion detected for "); Serial.print((millis() - motionStartTime) / 1000); Serial.println(" seconds."); } } ```

This example tracks the duration of motion detection events and only prints the duration if it exceeds the specified threshold (10 seconds). This functionality is useful for distinguishing between short, irrelevant motion events and longer, meaningful ones.

As your skills and projects grow, you'll find endless possibilities for incorporating the Pir sensor with Arduino. From home automation to security systems and art installations, the Pir sensor's versatility makes it an invaluable component in countless Arduino projects.

Now that you've mastered using the Pir sensor with Arduino, it's time to let your creativity flourish. Embrace the endless possibilities that Arduino offers and build something extraordinary. Happy crafting!

AD