07-21-2023, 02:01 AM
#include <Wire.h>
#include <RTClib.h>
RTC_DS1307 rtc;
void setup() {
Serial.begin(9600);
Wire.begin(4,15); //SDA is GPIO4 SCL is GPIO15
rtc.begin();
// Set initial time
// rtc.adjust(DateTime(2023, 7, 21, 9, 53, 50));
// You only need to set the initial time once. After setting the initial time, you should comment out the
//above line when uploading the code again to avoid resetting the time every time you start.
}
void loop() {
DateTime now = rtc.now();
// Print the date and time
Serial.print("The time is: ");
Serial.print(now.year(), DEC);
Serial.print("-");
printDigits(now.month());
Serial.print("-");
printDigits(now.day());
Serial.print("-");
printDigits(now.hour());
Serial.print("-");
printDigits(now.minute());
Serial.print("-");
printDigits(now.second());
Serial.println();
delay(1000);
}
void printDigits(int digits) {
if (digits < 10) {
Serial.print("0");
}
Serial.print(digits);
}
#include <RTClib.h>
RTC_DS1307 rtc;
void setup() {
Serial.begin(9600);
Wire.begin(4,15); //SDA is GPIO4 SCL is GPIO15
rtc.begin();
// Set initial time
// rtc.adjust(DateTime(2023, 7, 21, 9, 53, 50));
// You only need to set the initial time once. After setting the initial time, you should comment out the
//above line when uploading the code again to avoid resetting the time every time you start.
}
void loop() {
DateTime now = rtc.now();
// Print the date and time
Serial.print("The time is: ");
Serial.print(now.year(), DEC);
Serial.print("-");
printDigits(now.month());
Serial.print("-");
printDigits(now.day());
Serial.print("-");
printDigits(now.hour());
Serial.print("-");
printDigits(now.minute());
Serial.print("-");
printDigits(now.second());
Serial.println();
delay(1000);
}
void printDigits(int digits) {
if (digits < 10) {
Serial.print("0");
}
Serial.print(digits);
}