#include <DHT.h>
#include <LiquidCrystal_I2C.h>
DHT dht(32, DHT22); // Initialize DHT sensor on pin 32, type DHT22
LiquidCrystal_I2C lcd(0x27, 20, 4); // 20x4 LCD with I2C address 0x27
byte dot[8] = {0x0E, 0x0A, 0x0E, 0x00, 0x00, 0x00, 0x00, 0x00}; // Custom degree symbol
void setup() {
Serial.begin(115200);
Serial.println("Hello Lab 3");
dht.begin();
lcd.init();
lcd.backlight();
lcd.createChar(0, dot); // store dot[] into slot 0
}
void loop() {
float h = dht.readHumidity(); // Relative humidity
float c = dht.readTemperature(); // Celsius
float f = dht.readTemperature(true); // Fahrenheit
// Check if any reads failed
if (isnan(h) || isnan(c) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
delay(2000);
return;
}
lcd.clear(); // Optional: clear the screen before printing new values
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);}
Serial.printf(
"Humidity: %.1f Temperature: %.1f deg C %.1f deg F\n", h, c, f);
delay(4000); // Delay for 4 seconds
}