const int pirPin = 15; // Pin connected to PIR sensor
const int ledPin = 16; // Pin connected to LED
void setup() {
pinMode(pirPin, INPUT); // Set PIR pin as input
pinMode(ledPin, OUTPUT); // Set LED pin as output
Serial.begin(9600); // Start serial communication
digitalWrite(ledPin, LOW); // Ensure LED is off at the beginning
}
void loop() {
// Read the PIR sensor
int pirState = digitalRead(pirPin);
if (pirState == HIGH) {
// Motion detected, turn on LED
Serial.println("Motion Detected! Turning ON LED");
digitalWrite(ledPin, HIGH); // Turn ON LED
delay(2000); // Keep LED on for 2 seconds
digitalWrite(ledPin, LOW); // Turn OFF LED after 2 seconds
} else {
// No motion detected
Serial.println("No motion detected");
}
delay(500); // Delay to prevent serial flooding
}