#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#define DHTpin 4 // GPIO pin connected to the DHT22 data pin
#define DHTtype DHT22 // DHT22 sensor type (change to DHT11 for DHT11 sensor)
float tempC = 0;
float tempF = 0;
float humid = 0;
// initialize the library with the numbers of the interface pins
LiquidCrystal_I2C lcd(0x27, 16, 2);
DHT dht(DHTpin, DHTtype);
void setup() {
dht.begin();
lcd.init();
lcd.clear();
lcd.backlight();
}
void loop() {
tempC = dht.readTemperature();
tempF = (tempC * 9.0)/5.0 + 32.0; // convert to F
lcd.setCursor(0,0);
lcd.print("Temp in C: ");
lcd.print(tempC);
lcd.setCursor(0,1);
lcd.print("Temp in F: ");
lcd.print(tempF);
delay(1000);
lcd.clear();
humid = dht.readHumidity();
lcd.setCursor(0,0);
lcd.print("Humidity: ");
lcd.print(humid);
lcd.print("%");
delay(1000);
lcd.clear();
if(isnan(tempC) || isnan(tempF) || isnan(humid)) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Error reading");
lcd.setCursor(0, 1);
lcd.print("sensor data");
delay(2000);
return;
}
}