Arduino - PIR Sensor
Arduino: Interfacing with a PIR Motion Sensor
Section titled “Arduino: Interfacing with a PIR Motion Sensor”Passive Infrared (PIR) sensors are essential for detecting motion, making them a cornerstone of security systems, automatic lighting, and interactive projects. They detect changes in infrared radiation emitted by warm bodies, such as humans.
Key Features and Advantages
Section titled “Key Features and Advantages”- Low Power Consumption: Ideal for battery-powered applications.
- Cost-Effective: Inexpensive and widely available.
- Simple Interface: Typically provide a simple HIGH/LOW digital output.
- Durable: Solid-state design means they don’t wear out.
A PIR sensor works by using two pyroelectric sub-sensors. It looks for a change where one half of the sensor detects a different IR level than the other, which indicates movement across its field of view. Note that PIRs detect change in IR, not static presence. If a person stands perfectly still, the sensor will eventually stop detecting them.
Calibration and Adjustment
Section titled “Calibration and Adjustment”Most PIR sensor modules have two potentiometers for adjustment:
- Sensitivity: Adjusts the detection range (e.g., from 3 to 7 meters).
- Time Delay: Adjusts how long the output signal remains HIGH after motion is detected (e.g., from a few seconds to several minutes).
Important: When first powered on, most PIR sensors require a warm-up and calibration period of 30-60 seconds. During this time, they may trigger unpredictably. Keep the area clear of motion while it stabilizes.
Components Required
Section titled “Components Required”- 1 × Arduino UNO R3 or compatible board
- 1 × PIR Motion Sensor (e.g., HC-SR501)
- 1 × LED (for visual feedback)
- 1 × 220Ω resistor (for the LED)
- Jumper wires
Wiring Diagram
Section titled “Wiring Diagram”// PIR Sensor Connections// (Pin labels may vary, check your sensor's datasheet)PIR VCC Pin --- Arduino 5VPIR OUT Pin --- Arduino Digital Pin 2PIR GND Pin --- Arduino GND
// LED Indicator ConnectionsArduino D13 --- [220Ω Resistor] --- [LED Anode (+)]Arduino GND --- [LED Cathode (-)]Modernized Arduino Code
Section titled “Modernized Arduino Code”This revised code uses a clean, state-based approach to monitor the PIR sensor. It is non-blocking and easy to extend.
/** * @file modern_pir_sensor.ino * @brief A clear, state-based approach to reading a PIR motion sensor. * @author Modernized Tutorial */
// Define pins for better readability and maintenance.constexpr int PIR_PIN = 2;constexpr int LED_PIN = 13;
// State variable to track motion.// Using an enum makes the code more self-documenting.enum MotionState { NO_MOTION, MOTION_DETECTED };MotionState currentMotionState = NO_MOTION;
void setup() { Serial.begin(9600); while (!Serial); // Wait for Serial to be ready (for modern boards like Leonardo/Micro)
pinMode(PIR_PIN, INPUT); pinMode(LED_PIN, OUTPUT);
Serial.println("PIR Sensor Warm-up and Calibration..."); Serial.println("Please keep the area clear of motion for 30 seconds."); delay(30000); // Blocking delay is acceptable in setup() for calibration. Serial.println("Sensor Ready.");}
void loop() { // Read the current state of the PIR sensor. int pirState = digitalRead(PIR_PIN);
if (pirState == HIGH) { // Motion is currently being detected. digitalWrite(LED_PIN, HIGH); // Turn on the indicator LED.
// If we were previously in a NO_MOTION state, this is a new event. if (currentMotionState == NO_MOTION) { Serial.println("Motion started!"); currentMotionState = MOTION_DETECTED; // Update state } } else { // No motion is currently being detected. digitalWrite(LED_PIN, LOW); // Turn off the indicator LED.
// If we were previously in a MOTION_DETECTED state, the event has ended. if (currentMotionState == MOTION_DETECTED) { Serial.println("Motion ended."); currentMotionState = NO_MOTION; // Update state } }
// loop() can continue to do other tasks here without being blocked.}Troubleshooting and Common Issues
Section titled “Troubleshooting and Common Issues”- False Triggers: If the sensor triggers without motion, try decreasing the sensitivity. Electrical noise from nearby appliances can also be a cause.
- Not Detecting: Ensure the sensor has had time to calibrate. Check wiring and increase sensitivity. Remember the sensor detects movement across its view, not directly towards or away from it.
- Stays HIGH: The time-delay potentiometer might be set to its maximum. Adjust it to a shorter duration for testing.
Practical Applications & Next Steps
Section titled “Practical Applications & Next Steps”Now that you can detect motion, you can build exciting projects:
- Automated Lighting: Control a relay to switch AC-powered lights.
- Security Alarm: Trigger a buzzer or send a notification via a WiFi module (like an ESP8266 or ESP32).
- Interactive Art: Wake up a project from a low-power sleep mode when a person approaches.