// Define the pin to which the OUT pin of PIR sensor is connected
int pirPin = 2;
// Define the pin to which the relay module is connected
int relayPin = 3;
// Variable to store the PIR sensor output
int pirState = 0;
void setup() {
// Set the PIR pin as input
pinMode(pirPin, INPUT);
// Set the relay pin as output
pinMode(relayPin, OUTPUT);
// Initialize relay module to OFF state
digitalWrite(relayPin, LOW);
// Start serial communication for debugging
Serial.begin(9600);
}
void loop() {
// Read the state of the PIR sensor
pirState = digitalRead(pirPin);
// Check if motion is detected
if (pirState == HIGH) {
Serial.println("Motion detected!");
// Turn on the relay (or perform any action you want)
digitalWrite(relayPin, HIGH);
} else {
Serial.println("No motion");
// Turn off the relay (or perform any action you want)
digitalWrite(relayPin, LOW);
}
// Delay for stability
delay(500);
}