#include <DHT.h>
#include <LiquidCrystal.h> // Include this if using the LCD
#define DHTPIN 2 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // Change to DHT22 for the DHT22 sensor
DHT dht(DHTPIN, DHTTYPE);
// Initialize the LCD with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
Serial.begin(9600);
dht.begin();
lcd.begin(16, 2); // Initialize the LCD for 16x2 characters
lcd.clear(); // Clear the LCD display
delay(500); // Allow time for the LCD to initialize
}
void loop() {
// Wait a few seconds between measurements.
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
float h = dht.readHumidity();
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Print temperature and humidity to Serial Monitor
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C ");
// If using the LCD, display temperature and humidity
lcd.setCursor(0, 0); // Set the cursor to the first row, first column
lcd.print("Temp: ");
lcd.print(t);
lcd.print(" C");
lcd.setCursor(0, 1); // Set the cursor to the second row, first column
lcd.print("Hum: ");
lcd.print(h);
lcd.print(" %");
}