#include <DHT.h>
#include <LiquidCrystal_I2C.h>
// DHT Sensor setup
DHT dht(32, DHT22);
// LCD setup
byte dot[8] = {0x0E, 0x0A, 0x0E, 0x00, 0x00, 0x00, 0x00, 0x00};
LiquidCrystal_I2C lcd(0x27, 20, 4);
void setup() {
// Initialize serial
Serial.begin(115200);
Serial.println("Hello Lab 3");
// Initialize DHT sensor
dht.begin();
// Initialize LCD
lcd.init();
lcd.backlight();
lcd.createChar(0, dot); // store dot[] into slot 0
}
void loop() {
// Read sensor values
float h = dht.readHumidity(); // Relative humidity
float c = dht.readTemperature(); // Celsius
float f = dht.readTemperature(true); // Fahrenheit
// Check if any reads failed and try again
if (isnan(h) || isnan(c) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Sensor Error!");
delay(2000);
return;
}
// Print to serial monitor
Serial.printf("Humidity: %.1f%%, Temperature: %.2f°C, %.1f°F\n", h, c, f);
// Display on LCD
lcd.setCursor(0, 0);
lcd.print("Humidity: "); lcd.print(h, 1); lcd.print("%");
lcd.setCursor(0, 2);
lcd.print("Temperature: "); lcd.print(c, 2); lcd.write(0); lcd.print("C");
lcd.setCursor(0, 3);
lcd.print("Temperature: "); lcd.print(f, 1); lcd.write(0); lcd.print("F");
delay(4000); // Wait 4 seconds between readings
}