#include <Arduino.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
// Define the DHT pin and type
#define DHT_PIN 21 // The ESP8266 pin D7 connected to DHT11 sensor
#define DHT_TYPE DHT11
// Initialize the LCD with I2C address 0x27, 16 columns and 2 rows
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Initialize the DHT sensor
DHT dht11(DHT_PIN, DHT_TYPE);
void setup() {
// Initialize I2C with custom SDA and SCL pins
Wire.begin(18, 19); // (SDA, SCL)
lcd.init(); // Initialize the LCD I2C display
lcd.backlight(); // Turn on the backlight
dht11.begin(); // Initialize the DHT sensor
}
void loop() {
float humi = dht11.readHumidity(); // Read humidity
float temperature_C = dht11.readTemperature(); // Read temperature
lcd.clear();
// Check if the readings are valid
if (isnan(temperature_C) || isnan(humi)) {
lcd.setCursor(0, 0);
lcd.print("Failed");
} else {
lcd.setCursor(0, 0); // Display position
lcd.print("Temp: ");
lcd.print(temperature_C); // Display the temperature
lcd.print("°C");
lcd.setCursor(0, 1); // Display position
lcd.print("Humi: ");
lcd.print(humi); // Display the humidity
lcd.print("%");
}
// Wait 2 seconds between readings
delay(2000);
}