// include necessary libraries
#include <DHT.h>
#include <LiquidCrystal.h>
// define pin for DHT sensor and sensor type
#define DHTPIN 7
#define DHTTYPE DHT22
// create DHT and LCD objects
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal lcd(12,11,5,4,3,2);
void setup()
{
// start serial communication and LCD
Serial.begin(9600);
lcd.begin(16,2);
// initialize DHT sensor
dht.begin();
}
void loop()
{
// read temperature and humidity from DHT sensor
float temp = dht.readTemperature();
float humidity = dht.readHumidity();
// check if readings are valid
if (isnan(temp) || isnan(humidity))
{
// print error message and exit loop
Serial.println("failed to read from dht sensor");
return;
}
// clear LCD screen and print temperature and humidity readings
lcd.clear();
lcd.print("temperature ");
lcd.print(temp);
lcd.print("°");
lcd.setCursor(0,1);
lcd.print("Humidity ");
lcd.print(humidity);
lcd.print("%");
// wait for 2 seconds before repeating loop
delay(2000);
}