#include <Wire.h>
#include <RTClib.h>
RTC_DS1307 rtc;
void setup() {
Serial.begin(9600);
// Initialize Wire library with ESP32 I2C pins
Wire.begin(21, 22); // SDA = GPIO 21, SCL = GPIO 22
rtc.begin();
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (!rtc.isrunning()) {
Serial.println("RTC is NOT running!");
// Set initial time
rtc.adjust(DateTime(2024, 3, 23, 8, 0, 0)); // Set to March 23, 2024, 08:00:00
} else {
Serial.println("RTC is running!");
}
}
void loop() {
DateTime now = rtc.now(); // Get the current time from RTC
// Display date and time on Serial Monitor
Serial.print(now.day(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.year(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(':');
if (now.minute() < 10) Serial.print('0'); // Add leading zero to minutes if needed
Serial.print(now.minute(), DEC);
Serial.print(':');
if (now.second() < 10) Serial.print('0'); // Add leading zero to seconds if needed
Serial.print(now.second(), DEC);
Serial.println();
delay(1000); // Wait 1 second before repeating the loop
}