#include <LiquidCrystal.h>
#include <DHT.h>
// Pin definitions for the ESP32
#define DHTPIN 15 // Pin connected to the DHT11 sensor
#define DHTTYPE DHT11 // DHT11 sensor type
// Pin definitions for the LCD (RS, E, D4, D5, D6, D7)
LiquidCrystal lcd(15, 5, 12, 13, 14, 16);
// Initialize DHT sensor
DHT dht(DHTPIN, DHTTYPE);
void setup() {
// Initialize serial communication
Serial.begin(115200);
// Initialize the LCD (16 columns, 2 rows)
lcd.begin(16, 2);
// Initialize the DHT11 sensor
dht.begin();
// Display initial message on LCD
lcd.print("Reading sensor");
}
void loop() {
// Read humidity and temperature from DHT11
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
// Check if any reading failed
if (isnan(humidity) || isnan(temperature)) {
lcd.clear();
lcd.print("Error reading");
return;
}
// Display temperature and humidity on the LCD
lcd.clear();
lcd.setCursor(0, 0); // Set cursor to first row
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print("C");
lcd.setCursor(0, 1); // Set cursor to second row
lcd.print("Humidity: ");
lcd.print(humidity);
lcd.print("%");
// Wait 2 seconds before the next update
delay(2000);
}