arduino pir sensor example code

Arduino PIR Motion Sensor Example Code

By Melissa Lopez
AD

Have you ever wished to automate tasks or build intelligent systems that respond to their environment? The Arduino platform, with its simplicity and extensive community support, is a fantastic starting point for such projects. Today, we're going to explore how to use a Passive Infrared (PIR) sensor with an Arduino board, and we'll share a comprehensive example code to get you started.

PIR sensors are humanity's faithful companions in countless movies and games, often serving as the trigger mechanism for elaborate traps and defense systems. In the real world, they're equally versatile and find applications in home automation, security systems, and basic motion detection. Let's dive into how you can make the most of these sensors with Arduino.

Understanding Arduino and PIR Sensors

Arduino is an open-source electronic prototyping platform based on flexible, easy-to-use hardware and software. It's built around a family of microcontrollers that allow you to create standalone interactive and reactive objects or installations that can sense and react to various inputs.

AD
How to Use a PIR Sensor for Motion sensing using Arduino
How to Use a PIR Sensor for Motion sensing using Arduino
Arduino - Czujnik ruchu PIR - Tranzystor.pl - Vortal Elektroniczny
Arduino - Czujnik ruchu PIR - Tranzystor.pl - Vortal Elektroniczny
ESP32 PIR Motion Sensor – Detect Motion with Arduino IDE
ESP32 PIR Motion Sensor – Detect Motion with Arduino IDE

PIR sensors, on the other hand, consist of a pyroelectric detector that generates a small electric current when there's a change in temperature, i.e., when something that emits or absorbs heat moves in its field of view. They're cheap, widely available, and incredibly useful when you want to trigger an action based on the presence or absence of heat sources.

Connecting the PIR Sensor to Arduino

To connect a PIR sensor to your Arduino board, you'll need a 10kΩ resistor, a breadboard, jumper wires, and some patience. First, insert the PIR sensor and the resistor onto the breadboard. Then, connect the VCC and GND pins of the PIR sensor to the 5V and GND pins of your Arduino board, respectively.

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
PIR Sensor with Arduino: Wiring & Code for Interactive Props
PIR Sensor with Arduino: Wiring & Code for Interactive Props
Arduino PIR Sensor Circuit and Code Example
Arduino PIR Sensor Circuit and Code Example

The resistor plays a crucial role here. Connect one end of it to the OUTPUT pin of the PIR sensor and the other end to a 5V pin on the Arduino. This pull-up resistor ensures that the signal from the PIR sensor is readable by the Arduino.

Arduino PIR Sensor Example Code

Now that the hardware is set up, let's dive into the Arduino code. The following example code initializes the PIR sensor, monitors its output, and turns on an LED when motion is detected.

AD

// Define the PIR Sensor pin int PIR Sensor = 2; int LED = 13; void setup() { pinMode(PIR Sensor, INPUT); pinMode(LED, OUTPUT); } void loop() { int motionSensorValue = digitalRead(PIR Sensor); if (motionSensorValue == HIGH) { digitalWrite(LED, HIGH); delay(500); // Turn on for half a second digitalWrite(LED, LOW); }} }

Turn on a DC Motor with a PIR Motion Sensor and Arduino
Turn on a DC Motor with a PIR Motion Sensor and Arduino
Sensore PIR HC-SR501
Sensore PIR HC-SR501
PIR Sensor Datasheet, Pinout Specifications, Working - Homemade Circuit Projects
PIR Sensor Datasheet, Pinout Specifications, Working - Homemade Circuit Projects
Arduino + Sensor PIR
Arduino + Sensor PIR
50 Arduino Sensors Every Maker Should Know
50 Arduino Sensors Every Maker Should Know
Interface HC-SR501 PIR Sensor with Arduino
Interface HC-SR501 PIR Sensor with Arduino

The setup function initializes the PIR sensor and LED pins. In the loop function, the Arduino reads the PIR sensor's output. If motion is detected (i.e., the output is HIGH), it turns on the LED for half a second. Simple, yet efficient.

Exploring More PIR Sensor Applications

Modifying the above code, you can use the PIR sensor to trigger various actions like sounding an alarm, sending emails, or tweeting when motion is detected. You can also trigger multiple actions based on different durations of motion - short motion could trigger a light to turn on, while prolonged motion could trigger an alarm.

AD

PIR Sensor and Timing

The motion detection duration can be further customized using delay functions. For instance, you could add a delay after the initial detection to prevent the LED from turning on and off too frequently when someone is just passing by.

PIR Sensor Datasheet, Pinout Specifications, Working - Homemade Circuit Projects
PIR Sensor Datasheet, Pinout Specifications, Working - Homemade Circuit Projects
Arduino with PIR Motion Sensor | Random Nerd Tutorials
Arduino with PIR Motion Sensor | Random Nerd Tutorials
How HC-SR501 PIR Sensor Works & How To Interface It With Arduino
How HC-SR501 PIR Sensor Works & How To Interface It With Arduino
Trigger Sound Effects with Arduino & PIR Motion Sensor
Trigger Sound Effects with Arduino & PIR Motion Sensor
Arduino with PIR Motion Sensor | Random Nerd Tutorials
Arduino with PIR Motion Sensor | Random Nerd Tutorials
Interface PIR Sensor to Arduino - Motion Sensor/Detector
Interface PIR Sensor to Arduino - Motion Sensor/Detector
Interface PIR Sensor With NodeMCU
Interface PIR Sensor With NodeMCU
Detector de movimiento con Arduino y sensor PIR
Detector de movimiento con Arduino y sensor PIR
Arduino light sensor: A beginner's tutorial on DIY light sensing
Arduino light sensor: A beginner's tutorial on DIY light sensing
HC-SR505 Mini PIR Motion Sensor with Arduino
HC-SR505 Mini PIR Motion Sensor with Arduino
How to Use Pir Sensor Without Arduino
How to Use Pir Sensor Without Arduino
PIR Motion Sensor With Arduino & LED
PIR Motion Sensor With Arduino & LED
IR Sensor Interfacing with Arduino – Circuit & Code
IR Sensor Interfacing with Arduino – Circuit & Code

To account for noise and false positives, you could also introduce a delay between the end of one motion detection and the start of a new one. This helps to prevent the Arduino from reacting to every minor fluctuation in the sensor's output.

Remember, Arduino and PIR sensors are incredibly versatile. Your imagination is the only limit to what you can achieve with these. Start with the basics, experiment, and don't hesitate to seek help from the Arduino community when you're stuck.

AD

Happy tinkering!

AD