#include <Wire.h>
#include <LiquidCrystal_I2C.h> // Include I2C LCD library
#include <DHT.h>
#define DHTPIN 14 // Pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT 22 sensor type
#define LEDPIN 5 // Pin connected to the LED
#define LCD_ADDRESS 0x27 // Replace with your LCD's I2C address
#define LCD_ROWS 2 // Number of LCD rows
#define LCD_COLUMNS 16 // Number of LCD columns
LiquidCrystal_I2C lcd(LCD_ADDRESS, LCD_ROWS, LCD_COLUMNS); // Initialize LCD object
DHT dht(DHTPIN, DHTTYPE);
void setup() {
pinMode(LEDPIN, OUTPUT); // Set LED pin as output
Serial.begin(9600); // Optional: Initialize serial communication for debugging
dht.begin(); // Initialize DHT sensor
lcd.init(); // Initialize LCD display
}
void loop() {
// Read temperature from the sensor
float temp = dht.readTemperature();
int humidity = dht.readHumidity();
// Check if reading was successful
if (isnan(temp) || isnan(humidity)){
Serial.println("Failed to read from DHT sensor!");
return;
}
// Control LED based on temperature
if (temp > 40) {
digitalWrite(LEDPIN, HIGH); // Turn on LED
} else {
digitalWrite(LEDPIN, LOW); // Turn off LED
}
// Display temperature on LCD
lcd.clear();
lcd.print("T:");
lcd.println(temp); // Use println to move to the next line
lcd.print("H: ");
lcd.print(humidity);
lcd.print("%");
delay(1000); // Delay for 1 second
}