#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <RTClib.h>
// Create objects for the RTC and LCD
RTC_DS1307 rtc;
LiquidCrystal_I2C lcd(0x27, 16, 2); // Adjust the address if needed
void setup() {
lcd.begin(16, 2); // Initialize the LCD with 16 columns and 2 rows
lcd.backlight();
// Initialize the RTC
if (!rtc.begin()) {
lcd.print("RTC not found");
while (1);
}
// If the RTC is not running, set the time
if (!rtc.isrunning()) {
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void loop() {
DateTime now = rtc.now();
// Display the time on the first row
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Time: ");
lcd.print(now.hour());
lcd.print(':');
if (now.minute() < 10) lcd.print('0'); // Leading zero for minutes
lcd.print(now.minute());
lcd.print(':');
if (now.second() < 10) lcd.print('0'); // Leading zero for seconds
lcd.print(now.second());
// Display the date on the second row
lcd.setCursor(0, 1);
lcd.print("Date: ");
lcd.print(now.day());
lcd.print('/');
lcd.print(now.month());
lcd.print('/');
lcd.print(now.year());
delay(1000); // Update every second
}