#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <RTClib.h>
RTC_DS3231 rtc;
LiquidCrystal_I2C lcd(0x27, 16, 2); // Change the I2C address if needed
void setup() {
// put your setup code here, to run once:
lcd.begin(16,2);
lcd.backlight();
if(!rtc.begin()){
lcd.print("Couldn`t find RTC");
while(1);
}
if(rtc.lostPower()){
lcd.print("RTC lost power, let's set the time!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void loop() {
// put your main code here, to run repeatedly:
DateTime now = rtc.now();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Time: ");
lcd.print(formatDigits(now.hour()));
lcd.print(":");
lcd.print(formatDigits(now.minute()));
lcd.print(":");
lcd.print(formatDigits(now.second()));
lcd.setCursor(0, 1);
lcd.print("Date: ");
lcd.print(formatDigits(now.day()));
lcd.print("/");
lcd.print(formatDigits(now.month()));
lcd.print("/");
lcd.print(now.year());
delay(1000); // Update every second
}
String formatDigits(int digits){
if(digits < 10){
return "0" + String(digits);
}else{
return String(digits);
}
}