#include <Wire.h> // Include Wire library for I2C communication
#include <LiquidCrystal_I2C.h> // Include LCD library
#include <RTClib.h> // Include RTC library
RTC_DS3231 rtc; // Create an instance of the RTC
const int SDA_PIN = A4;
const int SCL_PIN = A5;
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address to 0x27 for a 16 chars and 2 line display
void setup() {
Wire.begin(); // Initialize I2C communication
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight
if (!rtc.begin()) { // Initialize RTC
lcd.print("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) { // If RTC lost power, set the time to the compile time
lcd.print("RTC lost power, setting time...");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void loop() {
DateTime now = rtc.now(); // Get current time from RTC
lcd.clear(); // Clear the LCD screen
lcd.setCursor(0, 0); // Set cursor to the first column of the first row
lcd.print("Date: "); // Print "Date: " on LCD
lcd.print(now.year(), DEC); // Print year
lcd.print('/');
lcd.print(now.month(), DEC); // Print month
lcd.print('/');
lcd.print(now.day(), DEC); // Print day
lcd.setCursor(0, 1); // Set cursor to the first column of the second row
lcd.print("Time: "); // Print "Time: " on LCD
lcd.print(now.hour(), DEC); // Print hour
lcd.print(':');
lcd.print(now.minute(), DEC); // Print minute
lcd.print(':');
lcd.print(now.second(), DEC); // Print second
delay(1000); // Delay for 1 second
}