Getting the internal RTC to work on Photon

I agree. This is what I observed as well.

I will look forward for the fix. For now I will just have to connect an external RTC :grin:

@BDub, has there been any progress to the RTC vbat power-cycle issue?

thanks

Hi @wesner0019 - the 0.4.3 release has a fix for the RTC problem.

Here's an excerpt from the github releases page:

Fix time being reset on wakeup. (removed WICED RTC init code that resets to default preset time in platform_mcu_powersave_init() within photon-wiced repo.) #440

2 Likes

That’s awesome! Will give that a try!

@mdma, sweet just verified it works. Originally I had some solder flux that prevented the battery to make a connection when I was testing this with version 0.4.3.

2 Likes

Great to hear! Thanks for confirming @wesner0019!

1 Like

The RTC works with battery backup! It is even automatically updated whenever P1 is connected to the cloud. Awesome work @mdma and the Particle team!

3 Likes

@bing1106 could you maybe share your code? I have a RTC running on an Arduino but I can’t find some examples for the photon to get me started. Thanks a lot!

@Maarten_CH, do you plan to work with an external RTC like the DS1307 or use Photon’s internal RTC? I am assuming the latter. If so, are you connecting Vbat to a coin cell (or 2 AA/AAA batteries)?

Before you do anything, please first update the firmware 0.4.3 to via DFU mode. This will fix the issue of the Broadcom resetting the internal RTC during power cycle. Once you have done that, try this:

  1. Connect Photon to the cloud, do an OTA with code below.
  2. check if the time is correct (use Time.zone to correct) via uart (I use Putty)
  3. comment out “if (1) Spark.connect();” and re-flash
  4. Now the Photon is truely offline, disconnect the USB/5V power. Make sure you already have Vbat connected to a <3.6 V supply
  5. After a few minutes (or hours), connect photon back to 5V (still in offline mode) and check if the time keeping is still correct via uart.

Test Code:

SYSTEM_MODE(SEMI_AUTOMATIC);

void setup() {
    Serial.begin(9600);
    Time.zone(-7);
    if (1) Spark.connect();    
}

void loop() {
    if (Spark.connected()) Spark.process();
    Serial.println(Time.timeStr());
    delay(1000);
}

The internal RTC updates every time the Photon connects to the cloud. This is a nice feature since you don’t need to manually set the RTC, (just the time zone and DST). If you are doing other things with the RTC, please check out the firmware documentation on Time. Unlike using external I2C-based RTCs such as DS1307, you can simply use the functions provided there to get time and other attributes. Works like magic!

4 Likes

@bing1106 Thanks, I got your code running exactly as you described! I received my first photon a few hours ago so I didn’t do the firmware upgrade because, as far as I understand, it does that automatically the first time you start it up.

I used the Arduino for my QlockTwo with a DCF-77 antenna to receive the time and the DS1307 to keep track of it. Thanks to the battery it could start directly without the need of a good time signal (which can take up to several minutes). Thanks to the cloud connectivity this is not a problem for the Photon so maybe I will skip the battery. But it’s nice to know that the battery works as expected.

I noticed that the time printed from the photon to the Particle Dev was several seconds behind the time of my computer and the time reference I found on the internet. Did you noticed something similar? I guess the time needed to retrieve the time from the cloud will not result in an offset that large.

It indeed does a firmware update, but not yet to the latest version. The system to provide for those updates are currently being worked on, and are hopefully ready for launch early next week. If you want 0.4.3 on in right now, you'll have to do so manually. You'll need the CLI to do so, but that's very much worthwhile installing anyhow. The update itself is really easy after you've got that installed, and can even be done over the air. Let me know if you need any assistance, should you encounter any difficulties.

2 Likes

@Maarten_CH, I am glad the code worked out. The reason for the manual firmware update was exactly what @Moors7 described. The automatic firmware update when you first start the Photon is not the latest 0.4.3 which included some bug fixes for the RTC. Before that release I was not able to get the RTC to work.

Your observation is correct that the cloud-updated RTC time is a few seconds behind the NIST. Might be an unfortunate side effect of how the time was updated in the Photon. I would just adjust for those few seconds of delay in code if you need it to be very accurate. Though, if you disconnect the Photon from cloud I image the time would be off a little bit anyway :smile:

@Moors7 @bing1106, thanks for the additional information! I don’t’ know how to use the CLI (or what it is) but I will check the documentation on the site. In case I get stuck I will ask you agin.

I think the easiest way is to add a few seconds manually when displaying the time. It does a last minute of the year countdown followed by a scrolling text message “Happy 20xx” so it would be nice if it’s not too far off :wink:

1 Like

Just as a follow up for people who noticed that the Photon RTC is running a few seconds behind.

When the RTC is set during the startup of the photon it's ~5 seconds behind. However, after calling the Spark.syncTime() function from the program loop the RTC is within ~1 second compared to the NIST. So don't add a few seconds to the RTC because it will make it less accurate after calling the Spark.syncTime() function within your loop.

(calling Spark.syncTime() already within setup() did not reduce the ~6 second lag)

2 Likes

Is there still a need to remove the 0 ohm resistor if trying to connect coin cell (CR2032) to vbat as mentioned in the docs…?

That only applies to the Electron, and there it’s still required AFAIK

@hagandh, on the Photon, Vbat is not connected to 3V3 via a zero-ohm resistor so you can just connect the coin cell to Vbat. This is great when you expect power to be removed from the Photon. However, if you expect power will be maintained to the Photon while it is in deep sleep (never totally off), then you should connect Vbat to the 3V3 pin so as to keep Vbat powered. It all depends on your design. :wink:

1 Like

if we don’t have internet connection and we are calling Time.now() .will it give data from RTC if i am maintaining 3.3v in V bat pin? @bing1106 @mdma ?

Yes, but the RTC will need setting from time to time due to drift.

2 Likes

it will automatically update whenever connected to cloud right?