#include <Wire.h>
#include <LiquidCrystal_PCF8574.h>
#define MOTION_SENSOR_PIN 2 // Pin connected to the motion sensor
#define LED_PIN 13 // Pin connected to the red LED
// LCD object (adjust I2C address if necessary)
LiquidCrystal_PCF8574 lcd(0x27);
int motion_state = LOW; // Current state of the motion sensor
int prev_motion_state = LOW; // Previous state of the motion sensor
void setup() {
Serial.begin(9600); // Initialize Serial communication
pinMode(MOTION_SENSOR_PIN, INPUT); // Set motion sensor pin to input mode
pinMode(LED_PIN, OUTPUT); // Set LED pin to output mode
// Initialize LCD
lcd.begin(16, 2);
lcd.setBacklight(255);
// Display initial message
lcd.setCursor(0, 0);
lcd.print("House Status:");
lcd.setCursor(0, 1);
lcd.print("Secure");
}
void loop() {
// Update the state of the motion sensor
prev_motion_state = motion_state;
motion_state = digitalRead(MOTION_SENSOR_PIN);
// Check if motion is detected
if (prev_motion_state == LOW && motion_state == HIGH) {
// Motion detected, intruder detected
Serial.println("Intruder Detected!");
blinkLED();
// Update LCD with intruder status
lcd.setCursor(0, 1);
lcd.print("Intruder Detected");
}
delay(100); // Small delay to avoid bouncing issues
}
// Function to blink the red LED
void blinkLED() {
for (int i = 0; i < 5; i++) {
digitalWrite(LED_PIN, HIGH); // Turn on the LED
delay(500); // Wait for 500 milliseconds
digitalWrite(LED_PIN, LOW); // Turn off the LED
delay(500); // Wait for 500 milliseconds
}
}