#include <DHT.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <RTClib.h>
#define DHTPIN 2 // DHT22 sensor pin
#define DHTTYPE DHT22 // DHT22 sensor type
DHT dht(DHTPIN, DHTTYPE);
RTC_DS1307 rtc;
LiquidCrystal_I2C lcd(0x27, 16, 2); // Initialize with 16 columns and 2 rows
float t = -999;
void setup() {
Serial.begin(9600);
dht.begin();
rtc.begin();
lcd.begin(20, 4); // Specify the number of columns and rows
lcd.backlight();
}
void loop() {
// Read humidity and temperature from DHT22 sensor
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
// Convert float temperature to integer
int intTemperature = static_cast<int>(temperature); // Casting float to int to get the integer part
// Read current time and date from DS1307 RTC sensor
DateTime now = rtc.now();
// Update the display only if the temperature changes
if(intTemperature != static_cast<int>(t)){ // Compare the integer part only
t = temperature; // Store the last temperature (float for comparison on next loop)
lcd.clear();
lcd.setCursor(0, 0); // Set cursor to the first line
lcd.print("Tem: ");
lcd.print(intTemperature); // Display the integer part only
lcd.print("C");
// Display the date on the second line of the LCD
lcd.setCursor(0, 1); // Set cursor to the second line
lcd.print("Date: ");
lcd.print(now.day(), DEC);
lcd.print('/');
lcd.print(now.month(), DEC);
lcd.print('/');
lcd.print(now.year(), DEC);
// Set cursor to the third line for time
lcd.setCursor(0, 2);
lcd.print("Time: ");
lcd.print(now.hour(), DEC);
lcd.print(':');
lcd.print(now.minute(), DEC);
lcd.print(':');
lcd.print(now.second(), DEC);
}
delay(500); // Delay a bit before the next read
}