#include <Wire.h>
#include <DHTesp.h>
#include <LiquidCrystal_I2C.h>
// DHT22 Setup
DHTesp dht;
// Fire Alarm System
const int smokePin = 16;
const int buzzerPin = 11; // Connect to Buzzer
const int bluePin = 12; // Connect to Blue LED
const int greenPin = 13; // Connect to Green LED
const int yellowPin = 14; // Connect to Yellow LED
const int redPin = 15; // Connect to Red LED
// LCD Control
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address, columns, rows
void setup()
{
pinMode(smokePin, INPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(yellowPin, OUTPUT);
pinMode(redPin, OUTPUT);
// Initialize LCD
Wire.begin(8, 9);
lcd.begin(16, 2);
// Start DHT sensor
dht.setup(16, DHTesp::DHT22);
// Display initial message on LCD
lcd.print("Temp: Humidity: ");
}
void loop()
{
// Read temperature and humidity from DHT22
float temperature = dht.getTemperature();
float humidity = dht.getHumidity();
// Display temperature and humidity on LCD
lcd.setCursor(6, 0);
lcd.print(temperature, 1);
lcd.print(" C ");
lcd.setCursor(0, 1);
lcd.print("Humidity: ");
lcd.print(humidity, 1);
lcd.print("%");
// Fire Alarm System
int smokeValue = digitalRead(smokePin);
if (smokeValue == HIGH)
{
digitalWrite(buzzerPin, HIGH); // Turn on the buzzer
digitalWrite(bluePin, LOW); // Turn on the blue LED
delay(1000); // Buzz for 1 second
if (temperature < 0)
{
digitalWrite(buzzerPin, LOW); // Turn off the buzzer
digitalWrite(bluePin, HIGH); // Turn on the blue LED
delay(1000); // Wait for 1 second
digitalWrite(bluePin, HIGH); // Turn off the blue LED
}
else if (temperature < 40 && temperature >= 0)
{
digitalWrite(buzzerPin, LOW); // Turn off the buzzer
digitalWrite(greenPin, HIGH); // Turn on the green LED
delay(1000); // Wait for 1 second
digitalWrite(greenPin, LOW); // Turn off the green LED
}
else if (temperature >= 40 && temperature < 70)
{
digitalWrite(buzzerPin, LOW); // Turn off the buzzer
digitalWrite(yellowPin, HIGH); // Turn on the yellow LED
delay(500); // Buzz for 0.5 seconds
digitalWrite(yellowPin, LOW); // Turn off the yellow LED
}
else if (temperature >= 70)
{
digitalWrite(buzzerPin, LOW); // Turn off the buzzer
digitalWrite(redPin, HIGH); // Turn on the red LED
delay(250); // Buzz for 0.25 seconds
digitalWrite(redPin, LOW); // Turn off the red LED
}
}
else
{
digitalWrite(buzzerPin, LOW); // Turn off the buzzer
digitalWrite(bluePin, HIGH); // Turn on the blue LED
delay(1000); // Wait for 1 second
digitalWrite(bluePin, LOW); // Turn off the blue LED
}
// Delay for a while before the next loop
delay(2000);
}