#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#define DHTPIN 1
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
#define RED_LED_PIN 11
#define GREEN_LED_PIN 12
#define BLUE_LED_PIN 13
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
pinMode(RED_LED_PIN, OUTPUT);
pinMode(GREEN_LED_PIN, OUTPUT);
pinMode(BLUE_LED_PIN, OUTPUT);
Serial.begin(9600);
dht.begin();
// Initialize the LCD
lcd.init();
lcd.backlight();
// Initialize LED pins as outputs
// Print a message to the LCD.
lcd.print("Temp & Humidity");
}
void loop() {
// Wait a few seconds between measurements.
delay(2000);
lcd.print("Temp & Humidity");
// Reading temperature or humidity takes about 250 milliseconds!
float h = dht.readHumidity();
// Read temperature as Celsius
float t = dht.readTemperature();
// // Check if any reads failed and exit early (to try again).
// if (isnan(h) || isnan(t)) {
// lcd.setCursor(0, 1);
// lcd.print("Error reading");
// return;
// }
// Clear the previous readings
lcd.clear();
// Print temperature to LCD
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(t);
lcd.print(" C");
// Print humidity to LCD
lcd.setCursor(0, 1);
lcd.print("Humidity: ");
lcd.print(h);
lcd.print(" %");
Serial.println(t);
Serial.println(h);
// Control LEDs based on temperature range
if (t < 20) {
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(GREEN_LED_PIN, LOW);
digitalWrite(BLUE_LED_PIN, HIGH); // Blue LED for cold
} else if (t >= 20 && t <= 30) {
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(GREEN_LED_PIN, HIGH); // Green LED for normal
digitalWrite(BLUE_LED_PIN, LOW);
} else {
digitalWrite(RED_LED_PIN, HIGH); // Red LED for hot
digitalWrite(GREEN_LED_PIN, LOW);
digitalWrite(BLUE_LED_PIN, LOW);
}
}