#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <RTClib.h>
// Initialize RTC and LCD
RTC_DS1307 rtc;
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27 for 16x2 LCD
void setup() {
// Start serial communication
Serial.begin(9600);
// Initialize the LCD
lcd.begin(16,2);
lcd.init();
lcd.backlight();
// Initialize the RTC
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
// Check if the RTC lost power
if (!rtc.isrunning()) {
Serial.println("RTC is NOT running!");
// Set the date and time if the RTC isn't running (set your own date & time here)
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void loop() {
DateTime now = rtc.now(); // Get current date and time from RTC
// Display the date and time on the serial monitor
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
// Display the date and time on the LCD
lcd.clear();
lcd.setCursor(0, 0); // First row
lcd.print("Time: ");
lcd.print(now.hour());
lcd.print(':');
lcd.print(now.minute());
lcd.print(':');
lcd.print(now.second());
lcd.setCursor(0, 1); // Second row
lcd.print("Date: ");
lcd.print(now.day());
lcd.print('/');
lcd.print(now.month());
lcd.print('/');
lcd.print(now.year());
delay(1000); // Update every second
}