#include <LiquidCrystal_I2C.h>// Include LiquidCrystal_I2C library
#define PIR_SENSOR_PIN 2
#define ULTRASONIC_TRIGGER_PIN 3
#define ULTRASONIC_ECHO_PIN 4
#define BUZZER_PIN 5
#define LED_PIN 6
const int threshold = 200; // Distance threshold in cm for proximity detection
const int lcd_address = 0x27; // I2C address of your LCD (may vary)
const int rows = 2;
const int cols = 16;
LiquidCrystal_I2C lcd(lcd_address, rows, cols);
void setup() {
pinMode(PIR_SENSOR_PIN, INPUT);
pinMode(ULTRASONIC_TRIGGER_PIN, OUTPUT);
pinMode(ULTRASONIC_ECHO_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
lcd.init();
lcd.backlight(); // Optional: turn on LCD backlight
lcd.print("Welcome Benneth ");
delay(1000);
}
void loop() {
if (digitalRead(PIR_SENSOR_PIN) == HIGH) {
int distance = measureDistance();
if (distance <= threshold) {
digitalWrite(BUZZER_PIN, HIGH);
digitalWrite(LED_PIN, HIGH);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Intruder!");
lcd.setCursor(0, 1);
lcd.print("Distance: ");
lcd.print(distance);
lcd.print("cm");
delay(1000); // Alarm duration
} else {
// Object detected but not close enough, optional indication
digitalWrite(LED_PIN, LOW); // Replace with desired indication logic
delay(500);
}
} else {
lcd.clear(); // Clear LCD when no motion detected
digitalWrite(BUZZER_PIN, LOW);
digitalWrite(LED_PIN, LOW);
lcd.print("No Danger,Keep Calm ");
}
delay(500); // Adjust delay based on desired checking frequency
}
int measureDistance() {
// ... same as previous implementation ...
// Trigger ultrasonic sensor
digitalWrite(ULTRASONIC_TRIGGER_PIN, HIGH);
delayMicroseconds(2);
digitalWrite(ULTRASONIC_TRIGGER_PIN, LOW);
// Measure echo pulse duration
long duration = pulseIn(ULTRASONIC_ECHO_PIN, HIGH);
// Calculate distance (speed of sound * echo duration / 2)
return duration * 0.034 / 2;
}