#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
// Initialize the LCD with the I2C address found using the scanner
LiquidCrystal_I2C lcd(0x27, 16, 2); // Replace 0x27 with the correct address if different
// Define the DHT pin and type
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
void setup() {
// Start the Serial Monitor
Serial.begin(9600);
// Start the LCD with the number of columns and rows
lcd.begin(16, 2);
lcd.backlight();
// Start the DHT sensor
dht.begin();
lcd.setCursor(0, 0);
lcd.print(" WELCOME TO IOT");
lcd.setCursor(0, 1);
lcd.print(" PROJECT ");
delay(2000);
lcd.setCursor(0, 0);
lcd.print(" ");
// Print initial labels to the LCD
lcd.setCursor(0, 0);
lcd.print("Temp: C");
lcd.setCursor(0, 1);
lcd.print("Humidity: %");
Serial.println("DHT11 Sensor Initialized");
delay(2000); // Wait 2 seconds for the DHT sensor to stabilize
}
void loop() {
// Read temperature and humidity
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(humidity) || isnan(temperature)) {
lcd.setCursor(0, 0);
lcd.print("Failed to read ");
lcd.setCursor(0, 1);
lcd.print("from DHT sensor");
Serial.println("Failed to read from DHT sensor");
return;
}
// Print the temperature to the LCD on the first line
lcd.setCursor(6, 0);
lcd.print(temperature);
lcd.print(" "); // Add a space to overwrite any extra digits
// Print the humidity to the LCD on the second line
lcd.setCursor(10, 1);
lcd.print(humidity);
lcd.print(" "); // Add a space to overwrite any extra digits
// Print the temperature and humidity to the Serial Monitor
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" C, ");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
// Wait a few seconds between measurements
delay(2000);
}