#include <DHT.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define DHTPIN 2 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302)
#define ldrPin A0 // Analog pin connected to the photoresistor
#define ledPin 13 // Digital pin connected to the LED
#define buzzerPin 11 // Digital pin connected to the buzzer
#define I2C_ADDR 0x27 // I2C address for the LCD1602
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
LiquidCrystal_I2C lcd(I2C_ADDR, 16, 2); // Initialize the LCD1602 with 16 columns and 2 rows
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor
void setup() {
Serial.begin(9600);
lcd.begin (16,2); // Initialize the LCD with 16 columns and 2 rows
lcd.setBacklight(HIGH);
pinMode(ledPin, OUTPUT); // Set LED pin as output
pinMode(buzzerPin, OUTPUT); // Set buzzer pin as output
dht.begin(); // Initialize DHT sensor
// Print initial messages to the LCD
lcd.clear();
lcd.print("Fire Detection");
lcd.setCursor(0, 1);
lcd.print("System");
delay(2000);
lcd.clear();
}
void loop() {
// Read temperature, humidity, and LDR value
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
int ldrValue = analogRead(ldrPin);
// Print temperature, humidity, and LDR value to serial monitor
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
Serial.print("LDR Value: ");
Serial.println(ldrValue);
// Print temperature, humidity, and LDR value to LCD
lcd.clear();
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print(" C");
lcd.setCursor(0, 1);
lcd.print("Humidity: ");
lcd.print(humidity);
lcd.print(" %");
// Fire detection logic
if (temperature > 50 && ldrValue < 50 && humidity > 10) { // Adjust threshold values as needed
Serial.println("Fire detected!");
lcd.clear();
lcd.print("Fire Detected!");
digitalWrite(ledPin, HIGH); // Turn on LED
tone(buzzerPin, 1000); // Turn on buzzer
delay(1000); // Delay for 1 second
digitalWrite(ledPin, LOW); // Turn off LED
noTone(buzzerPin); // Turn off buzzer
delay(1000); // Delay for 1 second
}
delay(1000); // Delay for readability
}