#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
// Initialize the LCD, adjust the I2C address (0x27 is common for LCD2004)
LiquidCrystal_I2C lcd(0x27, 20, 4);
// Define DHT22 sensor settings
#define DHTPIN 13 // DHT22 data pin connected to GPIO 13
#define DHTTYPE DHT22 // DHT22 (AM2302) sensor type
DHT dht(DHTPIN, DHTTYPE);
void setup() {
// Start the LCD and set up the DHT sensor
lcd.init(); // Initialize the LCD (using init instead of begin)
lcd.backlight(); // Turn on the backlight
dht.begin();
// Display initial message on the LCD
lcd.setCursor(0, 0);
lcd.print("Temp & Humidity");
delay(2000);
}
void loop() {
// Read temperature and humidity from the DHT22 sensor
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
// Check if any readings failed and exit early
if (isnan(humidity) || isnan(temperature)) {
lcd.setCursor(0, 0);
lcd.print("Error reading data");
return;
}
// Display temperature on the first line
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print(" C");
// Display humidity on the second line
lcd.setCursor(0, 1);
lcd.print("Humidity: ");
lcd.print(humidity);
lcd.print(" %");
// Wait before updating again
delay(2000);
}