// Importing required libraries
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define pitch 300
int red=2;
int green=3;
int PIR=13;
int alarm=10;
// Initialize the LCD display (16x2) with I2C address 0x27
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
// put your setup code here, to run once:
// Initialize the serial communication for debugging purposes
Serial.begin(115200);
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(PIR, INPUT);
pinMode(alarm, OUTPUT);
// Initialize the LCD display
lcd.init();
lcd.backlight();
lcd.clear();
// Display a welcome message
lcd.setCursor(0, 0);
lcd.print("Welcome!");
delay(2000);
lcd.clear();
}
void loop() {
// put your main code here, to run repeatedly:
int pirState = digitalRead(PIR);
if (pirState == HIGH) {
digitalWrite(red, HIGH);
digitalWrite(green, LOW);
tone(alarm,pitch);
delay(600);//delays the frequency at which the alarm is heard
digitalWrite(red, LOW);
digitalWrite(green, LOW);
noTone(alarm);
delay(600);
// Display the message on the LCD display
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Motion Detected!");
// Optional: print a message to the serial monitor
Serial.println("Motion Detected!");
}
else {
digitalWrite(red, LOW);
digitalWrite(green, HIGH);
noTone(alarm);
// Display "Motion Not Detected" message on the LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Motion Not Detected");
Serial.println("Motion Not Detected!");
}
}