#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "DHT.h"
// Initialize the LCD with the I2C address (usually 0x27)
LiquidCrystal_I2C lcd(0x27, 16, 2);
// DHT11 sensor
#define DHTPIN 2 // Pin which is connected to the DHT sensor
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
// Initialize the LCD with 16 columns and 2 rows
lcd.begin(16, 2);
lcd.backlight();
// Initialize Serial communication
Serial.begin(9600);
// Initialize the DHT sensor
dht.begin();
// Print a message to the LCD.
lcd.setCursor(0, 0);
lcd.print("DHT11 Sensor");
lcd.setCursor(0, 1);
lcd.print("Initializing...");
delay(2000); // Wait for 2 seconds
}
void loop() {
// Read humidity
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, 0);
lcd.print("Failed to read");
lcd.setCursor(0, 1);
lcd.print("from DHT sensor!");
return;
}
// Print the readings on the LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(t);
lcd.print(" C");
lcd.setCursor(0, 1);
lcd.print(" Hum: ");
lcd.print(h);
lcd.print(" %");
// Print the readings to Serial Monitor
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" °C, Humidity: ");
Serial.print(h);
Serial.println(" %");
delay(2000); // Wait a few seconds between updates
}