// Include necessary libraries
#include "DHT.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define DHTPIN 14 // Set the pin connected to the DHT11 data pin
#define DHTTYPE DHT22 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
// Initialize the LCD object with I2C address 0x27, 16 columns, and 2 rows
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
// Begin serial communication at 115200 baud
Serial.begin(115200);
// Initialize the dht11
dht.begin();
// Initialize the LCD
lcd.init();
lcd.backlight();
// Clear the LCD
lcd.clear();
}
void loop() {
// Wait a few seconds between measurements.
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (it's a very slow sensor)
float humidity = dht.readHumidity();
// Read temperature as Celsius (the default)
float temperature = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Display temperature and humidity on the LCD
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.write(223); // Degree symbol
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Humi: ");
lcd.print(humidity);
lcd.print("%");
}