Introduction:
A PIR (Passive Infrared) sensor like the HW-416 is typically used to detect motion by sensing changes in infrared radiation in its surroundings. These sensors are widely used in security systems, automatic lighting, and similar applications where detecting human presence or movement is required
Key Features of PIR Sensors:
- Motion Detection: Detects changes in infrared radiation caused by moving objects, particularly humans and animals.
- Digital Output: Provides a HIGH signal when motion is detected and a LOW signal otherwise.
- Adjustable Sensitivity and Time: Some sensors allow adjustments to the sensitivity (detection range) and the duration for which the output remains HIGH after detecting motion.
Pin Connections:
- VCC: Connect to 5V on the Arduino.
- GND: Connect to GND on the Arduino.
- OUT: Connect to a digital input pin on the Arduino (e.g., pin 2).
Code:
#define PIR_PIN 2 // Pin connected to PIR sensor OUT pin
#define LED_PIN 13 // Pin connected to an LED for visual indication
void setup() {
pinMode(PIR_PIN, INPUT); // Set PIR pin as input
pinMode(LED_PIN, OUTPUT); // Set LED pin as output
Serial.begin(9600); // Start serial communication for debugging
}
void loop() {
int pirState = digitalRead(PIR_PIN); // Read PIR sensor state
if (pirState == HIGH) {
// Motion detected
Serial.println(“Motion detected!”);
digitalWrite(LED_PIN, HIGH); // Turn on the LED
} else {
// No motion
Serial.println(“No motion.”);
digitalWrite(LED_PIN, LOW); // Turn off the LED
}
delay(1000); // Wait for 1 second before checking again
}
https://github.com/makertribe/IoT-Codes/blob/51eb3e61145189586acfb33bbf6545d8c6f15267/PIR%20sensor
Output: