#include
#include
#include
// --- Configuration ---
#define DHTPIN 2 // Pin where DHT11 Data is connected
#define DHTTYPE DHT11 // Defining the sensor type
#define LCD_ADDR 0x27 // Common address is 0x27 (try 0x3F if it fails)
// --- Initialization ---
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(LCD_ADDR, 16, 2);
void setup() {
// Start the sensor and LCD
dht.begin();
lcd.init();
lcd.backlight();
// Quick startup message
lcd.setCursor(0, 0);
lcd.print("Initializing...");
delay(2000);
lcd.clear();
}
void loop() {
// Read Humidity and Temperature
float h = dht.readHumidity();
float t = dht.readTemperature(); // Celsius
// Check if sensor is responding
if (isnan(h) || isnan(t)) {
lcd.setCursor(0, 0);
lcd.print("Sensor Error! ");
return;
}
// Display Temperature on Line 1
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(t, 1); // 1 decimal place
lcd.print((char)223); // Degree symbol
lcd.print("C");
// Display Humidity on Line 2
lcd.setCursor(0, 1);
lcd.print("Humi: ");
lcd.print(h, 1); // 1 decimal place
lcd.print("%");
// DHT11 is slow, so we wait 2 seconds between updates
delay(2000);
}