#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define MOTION_SENSOR_PIN 2 // Pin connected to the PIR sensor output
#define LED_PIN 13 // Red LED pin
int motion_state = LOW; // Current state of the sensor
int prev_motion_state = LOW; // Previous state of the sensor
// Initialize the LCD
LiquidCrystal_I2C lcd(0x27, 20, 4); // Adjust I2C address if needed
void setup() {
Serial.begin(9600); // Initialize serial communication for debugging
pinMode(MOTION_SENSOR_PIN, INPUT); // Set PIR sensor pin as input
pinMode(LED_PIN, OUTPUT); // Set LED pin as output
// Initialize LCD
lcd.begin(20, 4);
lcd.backlight();
// Initial message on the LCD
lcd.setCursor(0, 0);
lcd.print("System Armed");
}
void loop() {
prev_motion_state = motion_state; // Store previous state
motion_state = digitalRead(MOTION_SENSOR_PIN); // Read current state of PIR sensor
// Check if motion is detected (LOW -> HIGH)
if (prev_motion_state == LOW && motion_state == HIGH) {
Serial.println("Motion detected!");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Intruder Detected!");
// Blink the red LED
for (int i = 0; i < 6; i++) { // Blink 3 times
digitalWrite(LED_PIN, HIGH);
delay(500);
digitalWrite(LED_PIN, LOW);
delay(500);
}
}
// Check if motion stopped (HIGH -> LOW)
else if (prev_motion_state == HIGH && motion_state == LOW) {
Serial.println("Motion stopped!");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("System Armed");
digitalWrite(LED_PIN, LOW); // Ensure LED is off
}
}