Adalogger Issues with RTC PFC8523 Breakout

Please tell us which board you are using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
biod101
 
Posts: 183
Joined: Sun Apr 19, 2015 4:21 pm

Adalogger Issues with RTC PFC8523 Breakout

Post by biod101 »

I'm using an Adalogger MO to log barometric pressure in a field site to help me normalize data from a pressure transducer.

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);
}
The first time I load this into the Adalogger, line 21 (rtc.adjust) is uncommented so that the RTC time can be set. No problems - the echoed time registered by the RTC mirrors the compile time- great!

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!
Slide3.jpeg
Slide3.jpeg (178.51 KiB) Viewed 179 times
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:
Slide6.jpeg
Slide6.jpeg (184.78 KiB) Viewed 179 times
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).

User avatar
adafruit_support_carter
 
Posts: 29152
Joined: Tue Nov 29, 2016 2:45 pm

Re: Adalogger Issues with RTC PFC8523 Breakout

Post by adafruit_support_carter »

Can you provide more details about how you are simulating a power failure? Is it just unplugging/plugging board from USB? Or something else?

User avatar
biod101
 
Posts: 183
Joined: Sun Apr 19, 2015 4:21 pm

Re: Adalogger Issues with RTC PFC8523 Breakout

Post by biod101 »

That is correct - just unplugging and plugging in a USB since I need a serial terminal to verify the time.

User avatar
adafruit_support_carter
 
Posts: 29152
Joined: Tue Nov 29, 2016 2:45 pm

Re: Adalogger Issues with RTC PFC8523 Breakout

Post by adafruit_support_carter »

Do you have some way of measuring the voltage on the coin cell batteries? Should double check those and make sure they're reading OK.

User avatar
biod101
 
Posts: 183
Joined: Sun Apr 19, 2015 4:21 pm

Re: Adalogger Issues with RTC PFC8523 Breakout

Post by biod101 »

Measured it with my Fluke - 3.25 - 3.31V.

I would have included a picture, but hard to hold my phone, two leads, and the battery all at the same time ;)

I really appreciate your prompt replies and attempt at helping, but I'm outta time on this so I've ordered a DS3231 to see if it does any better with the Adalogger. (I ordered on Amazon since Adafruit is out.) I will post my results here. If it works, perhaps I can get a refund on those three PFC8523s associated with my Adafruit order?

Thanks!

User avatar
biod101
 
Posts: 183
Joined: Sun Apr 19, 2015 4:21 pm

Re: Adalogger Issues with RTC PFC8523 Breakout

Post by biod101 »

So, the noted issue only appears to happen when I'm trying to test/debug by turning the circuit on and off via a USB cable.

I've since migrated this to a protoboard with an on-off switch, and the time appears to be holding between simulated power failures verified by checking the logged data on an SD card. Here's the completed box including a parts list in the description of the video sourced mainly to Adafruit: https://youtu.be/dC-deo3MdSE

I did manage to get this to work as planned, but I did have trouble keeping the OLED in the circuit as a result of half the screen posting garbage. I'll post that challenge in a separate thread, but it's okay since we don't need an OLED for the field deployment.

I'll follow up shortly with code summary sourced mostly to Adafruit's wonderful libraries. Thanks again for all you do in helping the scientific community and citizen scientists with open source alternatives that don't cost a fortune-- I'm looking at you HACH, ISCO ;)

User avatar
adafruit_support_carter
 
Posts: 29152
Joined: Tue Nov 29, 2016 2:45 pm

Re: Adalogger Issues with RTC PFC8523 Breakout

Post by adafruit_support_carter »

Thanks for the additional info. Any idea what may have caused the original behavior of losing the time setting? Seems like the issue sort of resolved itself somehow?

User avatar
biod101
 
Posts: 183
Joined: Sun Apr 19, 2015 4:21 pm

Re: Adalogger Issues with RTC PFC8523 Breakout

Post by biod101 »

The issue doesn’t appear to be with the Adalogger or RTC, rather something is getting corrupted via the Serial Terminal output between simulated power failures. I know this since the RTC may report garbage (after a simulated power failure from removing USB cable) when plugged into a first Adalogger, but then the real time recovers when I query the same RTC chip using a second Adalogger. As such, I think something is getting corrupted via serial communications since the actual time is being stored between failures-- I just need to plug a second Adalogger in to recover the actual time from the RTC.

Not being an EE or programmer, I can’t explain what’s going on behind the scenes, but I can confirm that not simulating the power failure by disconnecting the USB cable (while it's communicating to a Serial Terminal) is the best way to avoid this odd behavior. If I cut power via the VIN pin and monitor the results via a logged SD card upon restart, I encounter no issues upon restarting the circuit multiple times.

Locked
Please be positive and constructive with your questions and comments.

Return to “Feather - Adafruit's lightweight platform”