// PIR sensor connected to pin 7
const int pirPin = 7;
// LED connected to pin 12
const int ledPin = 12;
// Variable to store PIR sensor status (motion detected or not)
int pirState = LOW;
int prevPirState = LOW;
void setup() {
// Begin communication with the computer at a baud rate of 9600
Serial.begin(9600);
// Set the PIR sensor pin as input
pinMode(pirPin, INPUT);
// Set the LED pin as output
pinMode(ledPin, OUTPUT);
// Welcome message for user interaction
Serial.println("PIR Sensor is warming up...");
delay(5000); // Wait for PIR sensor to stabilize (common with PIR sensors)
Serial.println("PIR Sensor ready! Waiting for motion...");
}
void loop() {
// Read the value from the PIR sensor (HIGH when motion is detected, LOW otherwise)
pirState = digitalRead(pirPin);
if (pirState == HIGH) {
// If motion is detected
if (prevPirState == LOW) {
// Change in state detected (motion started)
Serial.println("Motion detected! πΆββοΈ");
// Turn on the LED
digitalWrite(ledPin, HIGH);
// Update the previous state
prevPirState = HIGH;
}
} else {
// If no motion is detected
if (prevPirState == HIGH) {
// Change in state detected (motion stopped)
Serial.println("Motion ended! π");
// Turn off the LED
digitalWrite(ledPin, LOW);
// Update the previous state
prevPirState = LOW;
}
}
// Small delay to stabilize readings
delay(200);
}