// include the library code:
#include <LiquidCrystal.h>
#include "DHT.h"
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 9, en = 8, d4 = 7, d5 = 6, d6 = 5, d7 = 4; // rs= Register Select, en= Enable, D4-D7= Data Bus, rw= Read/Write
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
// initialize the library by associating any needed DHT interface pin
const int DHTpin = 2, DHTtype = DHT22; // DHT11
DHT dht(DHTpin, DHTtype);
void setup() {
// set up the LCD's number of columns and rows:-
Serial.begin(9600);
lcd.begin(16, 2);
dht.begin();
}
void loop() {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Check if any reads failed and exit early (to try again).
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
lcd.clear(); // clear display
lcd.setCursor(0, 0); // move cursor to (0, 0)
lcd.print("Humidity:" + String(humidity) + "%"); // print message at (0, 0)
lcd.setCursor(0, 1); // move cursor to (0, 1)
lcd.print("Temp:" + String(temperature) + "°C"); // print message at (0, 1)
delay(2000); // display the above for two seconds
}