#include <DHT.h>
#include <LiquidCrystal_I2C.h> // Library to Run I2C LCD LiquidCrystal I2C Library by Frank de Barbander 1.1.2
// We are using I2C 16x2 alphanumeric LCD with HD44780 controller
LiquidCrystal_I2C lcd(0x27, 16, 2); // Format -> (Address,Columns,Rows )
#define DHTPIN 2 // Define the digital pin where the DHT sensor is connected
#define DHTTYPE DHT22 // DHT11 or DHT22, change this according to your sensor type
DHT dht(DHTPIN, DHTTYPE);
void setup() {
dht.begin(); // Initialize the DHT sensor
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// write column, row
lcd.setCursor(0, 0);
// Print a message to the LCD.
lcd.print("Temp. = ");
// write column, row
lcd.setCursor(0, 1);
lcd.print("Humid.= ");
}
void loop() {
// Read temperature and humidity from the DHT sensor
// In the parameter pass true if temperature is required in fahrenheit, pass false if tempearture is required in celcius
float temperature = dht.readTemperature(false);
float humidity = dht.readHumidity();
// Check if the sensor reading is valid (non-NaN)
if (!isnan(temperature) && !isnan(humidity)) {
// write column, row
lcd.setCursor(8, 0);
// Print a Tempearture to the LCD.
lcd.print(temperature);
lcd.print(" C ");
// write column, row
lcd.setCursor(8, 1);
// Print a Humidity to the LCD.
lcd.print(humidity);
lcd.print(" % ");
}
delay(2000); // Delay for 2 seconds before the next reading
}