const int pirPin = 2; // PIR sensor output pin
const int relayPin = 6; // Relay module control pin
void setup() {
pinMode(pirPin, INPUT);
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW); // Make sure the relay is initially off
Serial.begin(9600);
}
void loop() {
int pirState = digitalRead(pirPin);
if (pirState == HIGH) {
Serial.println("Motion detected!");
digitalWrite(relayPin, HIGH); // Turn on the relay
delay(5000); // Wait for 5 seconds
} else {
digitalWrite(relayPin, LOW); // Turn off the relay
}
delay(1000); // Delay between readings
}