// Define the PIR sensor pin and LED pin
int pirPin = 2; // PIR sensor output pin connected to digital pin 2
int ledPin = 5; // LED connected to digital pin 13
void setup() {
pinMode(pirPin, INPUT); // Set PIR sensor pin as input
pinMode(ledPin, OUTPUT); // Set LED pin as output
Serial.begin(9600); // Initialize serial communication for debugging
}
void loop() {
int pirState = digitalRead(pirPin); // Read the state of the PIR sensor
if (pirState == HIGH) { // If motion is detected
digitalWrite(ledPin, HIGH); // Turn on the LED
Serial.println("Motion detected!"); // Print to Serial Monitor
} else {
digitalWrite(ledPin, LOW); // Turn off the LED if no motion
}
delay(100); // Short delay to avoid excessive checking
}