Since I need to compare datasets between a BMP280 and the pressure transducer, I have to add a time stamp to the collected data. In response, I purchased three RTC PCF8523 breakouts (PID 3295) along with the required button cells from Adafruit for use with this and future projects (order ID 2848732-7118033168)
I installed the battery and wired the RTC breakout via I2C to the Adalogger-- no problem. Upon running the example sketch PF8523 sketch, the program works the first time, but it fails intermittently when I simulate a power failure. Specifically, after the I cut power and restart, I get a consistent Year Month Day Hour Minute Second that's nonsense, and does not increment.
To help explain what's going on, I simplified the PF8523 example sketch to realize some debugging. Here is a simplified version of the sketch that has some helpful flags as well as the output from the first run:
Code: Select all
#include <Wire.h>
#include "RTClib.h"
RTC_PCF8523 rtc;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
delay(3000);
Serial.print("RTC Begin: "); Serial.println(rtc.begin());
Serial.print("RTC Initialized: "); Serial.println(rtc.initialized());
Serial.print("RTC lost power: "); Serial.println(rtc.lostPower());
Serial.println();
delay(3000);
// Uncomment only if starting chip for the first time
rtc.adjust(DateTime(__DATE__, __TIME__));
// After chip has right time, comment out line above and re-upload this sketch!
// Do this so it doesn't reset back to original compile time if power is lost.
// Don't forget to include a battery to keep time in memory
rtc.start();
// The PCF8523 can be calibrated for:
// - Aging adjustment
// - Temperature compensation
// - Accuracy tuning
// The offset mode to use, once every two hours or once every minute.
// The offset Offset value from -64 to +63. See the Application Note for calculation of offset values.
// https://www.nxp.com/docs/en/application-note/AN11247.pdf
// The deviation in parts per million can be calculated over a period of observation. Both the drift (which can be negative)
// and the observation period must be in seconds. For accuracy the variation should be observed over about 1 week.
// Note: any previous calibration should cancelled prior to any new observation period.
// Example - RTC gaining 43 seconds in 1 week
float drift = 43; // seconds plus or minus over oservation period - set to 0 to cancel previous calibration.
float period_sec = (7 * 86400); // total obsevation period in seconds (86400 = seconds in 1 day: 7 days = (7 * 86400) seconds )
float deviation_ppm = (drift / period_sec * 1000000); // deviation in parts per million (μs)
float drift_unit = 4.34; // use with offset mode PCF8523_TwoHours
// float drift_unit = 4.069; //For corrections every min the drift_unit is 4.069 ppm (use with offset mode PCF8523_OneMinute)
int offset = round(deviation_ppm / drift_unit);
// rtc.calibrate(PCF8523_TwoHours, offset); // Un-comment to perform calibration once drift (seconds) and observation period (seconds) are correct
// rtc.calibrate(PCF8523_TwoHours, 0); // Un-comment to cancel previous calibration
Serial.print("Offset is "); Serial.println(offset); // Print to control offset
}
void loop() {
Serial.print("PC Start __DATE__: "); Serial.println((__DATE__)); // confirming computer time
Serial.print("PC Start __TIME__: "); Serial.println((__TIME__)); // confirming computer time
Serial.println();
DateTime now = rtc.now();
Serial.print("Year :"); Serial.print(now.year(), DEC);
Serial.print(':');
Serial.print(" Month :"); Serial.print(now.month(), DEC);
Serial.print(':');
Serial.print(" Day :"); Serial.print(now.day(), DEC);
Serial.println();
Serial.print("Hour :"); Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(" Min :"); Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(" Sec :"); Serial.print(now.second(), DEC);
Serial.println();
Serial.println();
delay(3000);
}
I then comment line 21 and upload to the board so that subsequent power failures don't reset my RTC time. I upload and check my serial terminal - all good! I can see the battery is working since the "RTC lost power" flag is false, and the reported time is close.
I then simulate a power failure with the Adalogger running code that has the rtc.adjust function commented out. I unplug the board and plug it back in. Looking good!
I let it run and do the test a couple more times. The second power failure works okay, but the third fails as per this screenshot:
Note that the year is 2000, the month is 31, the day is 0, the time is similarly nonsense, and nothing increments.
I have done this test using two different Adaloggers and two different RTC chips, and event tried this on two different machines (one a Mac, one a PC), and the results are always the same. In fact, sometimes, the failure starts on the first power failure simulation, so I'm guessing there is a timing issue where things get out of sync.
Either I'm missing something in code to help with the sync, or this RTC chip is not compatible with the microcontroller architecture on the Feather MO Adalogger (?).
This has set me back quite a bit of time against a fast approaching deadline-- any help would be greatly appreciated (order ID 2848732-7118033168).