Recently, I purchased an Adafruit Assembled Data Logging shield and METRO 328 for my project. And the programming for its own RTC (PCF8523) is following the guidance:
https://learn.adafruit.com/adafruit-dat ... time-clock
https://learn.adafruit.com/adafruit-pcf ... th-arduino
It works well when I upload my codes on the device, and takes the Date and Time according to the computer I am using.
However, when I disconnect it with my computer and run it using a battery, the start time is consistent with the time when I upload my code. For example, the time I upload my code to the device is 08:34:23, next time I use the device, the start time is still 08:34:23, even the exact time is 12:22:00.
Technically, the RTC should run with a coin cell even Arduino battery dies or is reprogrammed.
Could you please help me with this program?
Code: Select all
#include "RTClib.h"
#include <Wire.h>
RTC_PCF8523 RTC;
void setup(void)
{
RTC.begin();
RTC.adjust(DateTime(F(__DATE__), F(__TIME__)));
Wire.begin();
if (!RTC.begin()) {
logfile.println("RTC failed");
}
}
void loop(void)
{
DateTime now;
now = RTC.now();
logfile.print('"');
logfile.print(now.year(), DEC);
logfile.print("/");
logfile.print(now.month(), DEC);
logfile.print("/");
logfile.print(now.day(), DEC);
logfile.print(" ");
logfile.print(now.hour(), DEC);
logfile.print(":");
logfile.print(now.minute(), DEC);
logfile.print(":");
logfile.print(now.second(), DEC);
logfile.print('"');
}