#include <LiquidCrystal_I2C.h>
#include <DHT.h>
// LCD settings
LiquidCrystal_I2C lcd(0x27, 16, 2); // LCD address and dimensions
// DHT sensor settings
#define DHTPIN 11 // Pin where the DHT is connected
#define DHTTYPE DHT22 // Use DHT22 for real-life weather station, supports negative values
DHT dht(DHTPIN, DHTTYPE);
// Custom degree symbol
byte degree_symbol[8] = {
0b00111,
0b00101,
0b00111,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000
};
void setup()
{
lcd.init(); // Initialize the LCD
lcd.backlight();
// Print name/message
lcd.print("Bharath"); // Replace "Your Name" with the desired name/message
delay(5000); // Delay for 5 seconds
// Clear the display
lcd.clear(); // Clear the LCD for new data
lcd.print("Temp = ");
lcd.setCursor(0, 1);
lcd.print("Humidity = ");
lcd.createChar(1, degree_symbol); // Create degree symbol
lcd.setCursor(9, 0);
lcd.write(1); // Display degree symbol
lcd.print("C");
lcd.setCursor(13, 1);
lcd.print("%");
dht.begin(); // Initialize the DHT sensor
}
void loop()
{
delay(1000); // Delay between readings
const int numReadings = 5;
float tempSum = 0;
float humSum = 0;
int validReadings = 0;
for (int i = 0; i < numReadings; i++) {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
if (!isnan(temperature) && !isnan(humidity)) {
tempSum += temperature;
humSum += humidity;
validReadings++;
}
delay(100); // Delay between individual readings
}
if (validReadings > 0) {
// Calculate average temperature and humidity
float avgTemp = tempSum / validReadings; // Already in Celsius
float avgHum = humSum / validReadings;
// Clear previous temperature and display
lcd.setCursor(7, 0);
lcd.print(" "); // Clear space
lcd.setCursor(7, 0);
lcd.print(avgTemp, 1); // Display temperature in Celsius with 1 decimal
lcd.print(" C"); // Add " C" after the temperature
// Clear previous humidity and display
lcd.setCursor(11, 1);
lcd.print(" "); // Clear space
lcd.setCursor(11, 1);
lcd.print(avgHum, 1); // Display humidity
lcd.print("%"); // Add "%" after humidity
} else {
// Display error if readings are invalid
lcd.setCursor(7, 0);
lcd.print("Err ");
lcd.setCursor(11, 1);
lcd.print("Err ");
}
}