#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal_I2C.h>
// Create RTC and LCD objects
RTC_DS1307 rtc;
LiquidCrystal_I2C lcd(0x27, 16, 2); // Adjust I2C address if needed (0x27 or 0x3F)
void setup() {
// Start serial
Serial.begin(115200);
// Init LCD
lcd.init();
lcd.backlight();
// Init RTC
if (!rtc.begin()) {
lcd.setCursor(0, 0);
lcd.print("RTC not found");
while (1);
}
if (!rtc.isrunning()) {
lcd.setCursor(0, 0);
lcd.print("RTC not running");
// Uncomment below line to set RTC to current compile time
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void loop() {
DateTime now = rtc.now();
// Print to serial
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.println(now.second(), DEC);
// Show on LCD
lcd.setCursor(0, 0);
lcd.print("Time: ");
if (now.hour() < 10) lcd.print("0");
lcd.print(now.hour(), DEC);
lcd.print(":");
if (now.minute() < 10) lcd.print("0");
lcd.print(now.minute(), DEC);
lcd.print(":");
if (now.second() < 10) lcd.print("0");
lcd.print(now.second(), DEC);
lcd.setCursor(0, 1);
lcd.print("Date: ");
if (now.day() < 10) lcd.print("0");
lcd.print(now.day(), DEC);
lcd.print("/");
if (now.month() < 10) lcd.print("0");
lcd.print(now.month(), DEC);
lcd.print("/");
lcd.print(now.year(), DEC);
delay(1000);
}