How does the onboard RTC work?

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
Abraxas
Posts: 16
Joined: Mon Jan 01, 2018 5:53 pm

How does the onboard RTC work?

Post by Abraxas » Wed Jan 03, 2018 7:47 pm

I feel like I should be able to use the RTC example provided (http://docs.micropython.org/en/v1.9.3/p ... b.RTC.html) to set up a clock and be able to unplug the pboard (v1.1) and still have it keep time (provided that a CR2032 is connected to VBACK & GND). Then, after some time passes, plug in the board and be able to recall the clock via REPL or my main program and have the clock give me the correct time within +/- 1-2 hours (Depending on the length of time the board is unplugged and unpowered).

Part of my problem is knowing where and how rtc is being stored since rtc is an object (if rtc = pyb.rtc()). If its stored in ram, then I feel like the rtc object would be wiped as soon as the board is unpowered and I would need to rebuild the rtc object in boot.py. Any thoughts on how to do this and what is going on with the rtc object?

Thanks!

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: How does the onboard RTC work?

Post by dhylands » Wed Jan 03, 2018 8:09 pm

The RTC object created by pyb.RTC() is in fact in RAM and is lost at power off. However, when you re-construct it you can just read the current time from the RTC (using rtc.datetime()) and it should be correct.

The CR2032 keeps the clock ticking on the underlying RTC hardware. Constructructing the RTC object doesn't affect the underlying hardware. Calling rtc.datetime with arguments to set the date and time will, of course, wipe out the current time stored in the RTC.

The example shows how to set the RTC. Remove the middle line to just read and print the time. i.e.

Code: Select all

rtc = pyb.RTC()
print(rtc.datetime())

Abraxas
Posts: 16
Joined: Mon Jan 01, 2018 5:53 pm

Re: How does the onboard RTC work?

Post by Abraxas » Wed Jan 03, 2018 8:27 pm

Thanks for the reply! So here is my next question. Is there a way for me to reconstruct the clock object without having the user reinitialize the clock. My thought is if I know when I first activated the RTC circuit then I would just have the board find out how long the RTC circuit has been in operation and then add that time to the initial activation time to give me current time. Is this possible? if so, how do I find out how long the RTC circuit has been in operation for?

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: How does the onboard RTC work?

Post by dhylands » Wed Jan 03, 2018 8:42 pm

You only need to set the RTC once. Once its set, it will maintain the correct time until your CR2032 battery runs out. Even when the pyboard is powered down, as long as the CR2032 is still powering the RTC, then it will continue to tick along and keep the current time.

Abraxas
Posts: 16
Joined: Mon Jan 01, 2018 5:53 pm

Re: How does the onboard RTC work?

Post by Abraxas » Wed Jan 03, 2018 8:51 pm

Sorry, I did understand what you were saying but when I tried your instructions I received this as a response from the controller.

>>> print(rtc.datetime())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'rtc' is not defined

If it is supposed to work could it be that I have a bad board?

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: How does the onboard RTC work?

Post by dhylands » Wed Jan 03, 2018 9:11 pm

You still need the line

Code: Select all

rtc = pyb.RTC()
to create the rtc object.

Abraxas
Posts: 16
Joined: Mon Jan 01, 2018 5:53 pm

Re: How does the onboard RTC work?

Post by Abraxas » Wed Jan 03, 2018 9:19 pm

Thank you so much! That was the problem! RTC is working beautifully now that I understand what I was doing wrong.

Post Reply