How to Use PIR Sensor with Arduino: Beginner’s Guide
Introduction
A PIR (Passive Infrared) sensor is a popular tool for detecting motion, making it easy to trigger events like turning on a light or sounding an alarm. In this guide, we’ll show you how to use a PIR sensor with Arduino, focusing on wiring, code, and practical applications for beginners.
What is a PIR Sensor?
A PIR (Passive Infrared) sensor is an electronic sensor that detects changes in infrared light, typically caused by moving objects that emit heat (like people or animals). Unlike active sensors, which emit signals and measure their return, a PIR sensor is “passive” because it only receives infrared radiation without emitting any itself. This makes PIR sensors energy-efficient and simple to use for detecting motion in a wide range of projects.
Types of PIR Sensors
Focus on HC-SR501
The most commonly used PIR sensor with Arduino is the HC-SR501. It’s highly popular due to its affordability, ease of use, and adjustable settings for sensitivity and delay. The sensor typically includes three pins: VCC, GND, and OUT (for the signal). The HC-SR501 is often used in home security systems, automatic lighting, and basic motion detection projects.
Other Models and Their Differences
Other PIR sensor models, such as the D203S or AM312, work similarly but may have different features like sensitivity range or size. The AM312 is a mini-PIR sensor that is smaller and has lower power consumption compared to the HC-SR501. Meanwhile, the D203S is a more advanced sensor used in industrial applications for greater accuracy and sensitivity.
Use cases of PIR sensor
PIR sensor is widely implemented in security systems for detecting intruders, automatically triggering alarms or lights when motion is detected. PIR sensors are also used in home automation, such as turning on lights or appliances when someone enters a room, and in energy-saving systems to control lighting based on occupancy. Additionally, they are utilized in automatic doors and hands-free faucets to detect the presence, offering both convenience and efficiency. These sensors are popular due to their low cost, reliability, and ease of integration into systems.
How a PIR Sensor Works
Principle of Detection
PIR sensors detect changes in infrared radiation from objects like humans or animals. Every object emits some level of infrared radiation, and the sensor monitors the environment for any shifts in this radiation. When a warm object (e.g., a person) moves within the sensor’s field of view, the infrared radiation changes, triggering the sensor to output a HIGH signal, indicating motion.
Range and Time Delay
Most PIR sensors, including the HC-SR501, allow you to adjust two settings:
- Range: The detection distance of the sensor can usually be adjusted between 3 to 7 meters. For instance, in a home security project, you might set the range to 5 meters to detect intruders in a hallway.
- Time Delay: This is the amount of time the output signal stays HIGH after detecting motion. You can set it from a few seconds up to several minutes. For example, in an automatic light system, you can set the delay to 30 seconds to keep the lights on after detecting movement.
Two-Pin Logic (HIGH/LOW)
PIR sensors operate using a simple two-pin logic. When the sensor detects motion, it sends a HIGH signal (3.3V or 5V, depending on the sensor) to the output pin. When there is no motion, the sensor outputs a LOW signal (0V). This makes it easy to integrate with Arduino to trigger actions like turning on LEDs or activating alarms.
Wiring PIR sensor with Arduino
Wiring Diagram
To use the HC-SR501 with an Arduino, follow this simple wiring guide:
- VCC: Connect to the 5V pin on the Arduino.
- GND: Connect to a GND pin on the Arduino.
- OUT (Signal Pin): Connect to digital pin 2 on the Arduino.
Arduino Code
Now that the PIR sensor is connected, let’s write the Arduino code to detect motion and trigger an LED or send a message to the serial monitor.
#define pirPin 2 // PIR sensor connected to digital pin 2
#define ledPin 13 // Built-in LED pin on the Arduino
void setup() {
pinMode(pirPin, INPUT); // Set the PIR sensor as input
pinMode(ledPin, OUTPUT); // Set the LED pin as output
Serial.begin(9600); // Start serial communication for debugging
}
void loop() {
int pirState = digitalRead(pirPin); // Read the state of the PIR sensor
if (pirState == HIGH) { // If motion is detected
Serial.println("Motion detected!"); // Print to serial monitor
digitalWrite(ledPin, HIGH); // Turn on the LED
} else {
digitalWrite(ledPin, LOW); // Turn off the LED
}
delay(500); // Wait for 500 milliseconds before repeating
}
Explanation of the Code:
- pinMode(): Configures the PIR sensor as an input and the LED as an output.
- digitalRead(): Reads the sensor’s output (HIGH or LOW). If the PIR sensor detects motion (HIGH), the LED will light up, and a message is printed on the serial monitor.
- digitalWrite(): Controls the LED based on the sensor reading.
- Serial.begin() and Serial.println(): These functions help you monitor sensor activity by printing information to the Arduino IDE’s Serial Monitor.
Conclusion
Using a PIR sensor with Arduino opens the door to countless exciting projects. Once you’re comfortable with the basics, you can expand your projects in several ways. By understanding how PIR sensors work and how to connect them to an Arduino, you can bring automation and intelligence to your projects with ease.