/*
* This ESP32 code is created by esp32io.com
*
* This ESP32 code is released in the public domain
*
* For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-dht22-lcd
*/
#include <DHT.h>
#include <LiquidCrystal_I2C.h>
#define DHT22_PIN 23 // ESP32 pin GPIO23 connected to DHT22 sensor
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27 (from DIYables LCD), 16 column and 2 rows
DHT dht22(DHT22_PIN, DHT22);
void setup() {
dht22.begin(); // initialize the DHT22 sensor
lcd.init(); // initialize the lcd
lcd.backlight(); // open the backlight
}
void loop() {
float humi = dht22.readHumidity(); // read humidity
float tempC = dht22.readTemperature(); // read temperature
lcd.clear();
// check whether the reading is successful or not
if (isnan(tempC) || isnan(humi)) {
lcd.setCursor(0, 0);
lcd.print("Failed");
} else {
lcd.setCursor(0, 0); // display position
lcd.print("Temp: ");
lcd.print(tempC); // display the temperature
lcd.print("°C");
lcd.setCursor(0, 1); // display position
lcd.print("Humi: ");
lcd.print(humi); // display the humidity
lcd.print("%");
}
// wait a 2 seconds between readings
delay(2000);
}