#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define DHTPIN 2 // Pin connected to the DHT sensor
#define DHTTYPE DHT22 // Type of DHT sensor (DHT22)
// Initialize the LCD with I2C address, width, and height
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Initialize the DHT sensor with the specified pin and type
DHT dht(DHTPIN, DHTTYPE);
// Define pins for LED indicators
int ledY = 3; // Yellow LED for "Freezing"
int ledO = 4; // Orange LED for "Very Cold"
int ledDG = 5; // Green LED for "Normal"
int ledP = 6; // Magenta LED for "Hot"
int ledR = 7; // Red LED for "Very Hot"
void setup() {
Wire.begin(); // Start I2C communication for LCD
dht.begin(); // Start the DHT sensor
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the LCD backlight
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Initializing..."); // Display startup message
// Initialize the LED pins as outputs
pinMode(ledY, OUTPUT);
pinMode(ledO, OUTPUT);
pinMode(ledDG, OUTPUT);
pinMode(ledP, OUTPUT);
pinMode(ledR, OUTPUT);
}
void loop() {
delay(5000); // Delay to prevent rapid sensor readings
float humidity = dht.readHumidity(); // Read humidity from DHT22
float temperature = dht.readTemperature(); // Read temperature in Celsius
// Check if sensor readings are valid
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read data from DHT sensor!");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Sensor Error");
return;
}
// Display temperature and humidity readings on the LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperature, 1); // Display temperature with one decimal
lcd.print(" ");
lcd.write(0xDF); // Degree symbol
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Humidity: ");
lcd.print(humidity, 1); // Display humidity with one decimal
lcd.print(" %");
delay(5000);
// Control LEDs based on temperature conditions
if (temperature <= 0) {
lcd.setCursor(0, 0);
lcd.print("Temp: Freezing ");
digitalWrite(ledY, HIGH); // Yellow LED for Freezing
digitalWrite(ledO, LOW);
digitalWrite(ledDG, LOW);
digitalWrite(ledP, LOW);
digitalWrite(ledR, LOW);
} else if (temperature <= 16) {
lcd.setCursor(0, 0);
lcd.print("Temp: Very Cold ");
digitalWrite(ledY, LOW);
digitalWrite(ledO, HIGH); // Orange LED for Very Cold
digitalWrite(ledDG, LOW);
digitalWrite(ledP, LOW);
digitalWrite(ledR, LOW);
} else if (temperature <= 25) {
lcd.setCursor(0, 0);
lcd.print("Temp: Normal ");
digitalWrite(ledY, LOW);
digitalWrite(ledO, LOW);
digitalWrite(ledDG, HIGH); // Green LED for Normal
digitalWrite(ledP, LOW);
digitalWrite(ledR, LOW);
} else if (temperature <= 37) {
lcd.setCursor(0, 0);
lcd.print("Temp: Hot ");
digitalWrite(ledY, LOW);
digitalWrite(ledO, LOW);
digitalWrite(ledDG, LOW);
digitalWrite(ledP, HIGH); // Magenta LED for Hot
digitalWrite(ledR, LOW);
} else {
lcd.setCursor(0, 0);
lcd.print("Temp: Very Hot ");
digitalWrite(ledY, LOW);
digitalWrite(ledO, LOW);
digitalWrite(ledDG, LOW);
digitalWrite(ledP, LOW);
digitalWrite(ledR, HIGH); // Red LED for Very Hot
}
// Control LEDs based on humidity conditions
if (humidity <= 20) {
lcd.setCursor(0, 1);
lcd.print("Humidity: Dry ");
} else if (humidity <= 40) {
lcd.setCursor(0, 1);
lcd.print("Humidity: Normal ");
} else if (humidity <= 60) {
lcd.setCursor(0, 1);
lcd.print("Humidity: Comfort");
} else {
lcd.setCursor(0, 1);
lcd.print("Humidity: Humid ");
}
delay(2000); // Delay to allow for stable LCD display updates
}