#include <dht.h> // DHT library works for both DHT11 and DHT22
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
dht DHT; // Declaring the DHT
#define DHT22_PIN 7 // DHT data pin, change it as per your connection
#define I2C_ADDR 0x27 // I2C address of your LCD
#define LCD_COLS 16 // Number of columns in the LCD
#define LCD_ROWS 2 // Number of rows in the LCD
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLS, LCD_ROWS); // Initialize the LCD
void setup() {
Serial.begin(9600); // Serial baud rate
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight
}
void loop() {
int chk = DHT.read22(DHT22_PIN); // Reading data from the sensor
float t = DHT.temperature * (9.0 / 5.0) + 32; // Converting Celsius to Fahrenheit
// Display temperature and humidity on the LCD
lcd.clear(); // Clear the LCD
lcd.setCursor(0, 0); // Set cursor to the first column of the first row
lcd.print("Temp: ");
lcd.print(DHT.temperature);
lcd.print(" C");
lcd.setCursor(0, 1); // Set cursor to the first column of the second row
lcd.print("Humidity: ");
lcd.print(DHT.humidity);
lcd.print("%");
// Output temperature and humidity to serial monitor
Serial.print("Temperature = ");
Serial.print(DHT.temperature); // Showing the temperature in Celsius
Serial.print("°C ");
Serial.print("Humidity = ");
Serial.println(DHT.humidity); // Showing the humidity
delay(2000); // Repeat every 2 seconds (make sure the reading is done every 2 seconds or more)
}