#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <RTClib.h>
// Initialize the RTC and LCD
RTC_DS1307 rtc;
LiquidCrystal_I2C lcd(0x27, 20, 4); // I2C address 0x27 for 20x4 LCD
void setup() {
// Start the RTC and LCD
Wire.begin();
lcd.begin(20, 4); // Initialize the LCD with 20 columns and 4 rows
lcd.backlight();
if (!rtc.begin()) {
lcd.setCursor(0, 0);
lcd.print("Couldn't find RTC");
while (1);
}
if (!rtc.isrunning()) {
lcd.setCursor(0, 1);
lcd.print("Setting the time!");
// Set the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
lcd.clear();
}
void loop() {
// Get the current time from the RTC
DateTime now = rtc.now();
// Display the current date and time on the LCD
lcd.setCursor(0, 0);
lcd.print("Date: ");
lcd.print(now.year(),DEC);
lcd.print('/');
lcd.print(now.month(), DEC);
lcd.print('/');
lcd.print(now.day(), DEC);
lcd.setCursor(0, 1);
lcd.print("Time: ");
char timeNow[9];
sprintf(timeNow,"%02d:%02d:%02d",now.hour(),now.minute(),now.second());
lcd.print(timeNow);
// You can add more information on the remaining lines, for example:
lcd.setCursor(0, 2);
lcd.print("Day of Week: ");
lcd.print(now.dayOfTheWeek()); // This is a number from 0-6 where 0 is Sunday
// Update the display every second
delay(1000);
}