// Define the pin numbers for the PIR sensor and the LED
const int pirSensorPin = 8;
const int ledPin = 13;
int pirState = LOW;
int val = 0;
void setup() {
pinMode(pirSensorPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
val = digitalRead(pirSensorPin);
if (val == HIGH) {
digitalWrite(ledPin, HIGH); // Turn on the LED when PIR sensor is triggered
if (pirState == LOW) {
Serial.println("Motion detected!");
pirState = HIGH;
}
} else {
digitalWrite(ledPin, LOW); // Turn off the LED when no motion is detected
if (pirState == HIGH){
Serial.println("Motion ended!");
pirState = LOW;
}
}
delay(100);
}