#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal_I2C.h> // if you don´t have I2C version of the display, use LiquidCrystal.h library instead
LiquidCrystal_I2C lcd(0x27,16,2);
RTC_DS3231 rtc;
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.clear();
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, let's set the time!");
// Set waktu pada RTC ketika kehilangan daya
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void loop() {
// Baca waktu dari RTC
DateTime now = rtc.now();
// Tampilkan waktu pada Serial Monitor
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.print(now.second(), DEC);
Serial.println();
lcd.setCursor(0, 0);
lcd.print(String(now.year(), DEC) + "/" + String(now.month(), DEC) + "/" + String(now.day(), DEC));
lcd.setCursor(0, 1);
lcd.print(String(now.hour(), DEC) + ":" + String(now.minute(), DEC) + ":" + String(now.second(), DEC));
delay(1000); // Tunggu 1 detik
}