RV3028 RTC with PICO, set time to local time?

I just got my PICO Breakout Garden and plugged an RV3028 RTC Breakout in.
I ran the demo for it and it worked just fine. Seems to have set it to GMT time though?
I’d like to set it to local time instead and could use some help.
The demo file is as follows.
from pimoroni_i2c import PimoroniI2C
from breakout_rtc import BreakoutRTC
import time

PINS_BREAKOUT_GARDEN = {"sda": 4, "scl": 5}  # i2c pins 4, 5 for Breakout Garden
PINS_PICO_EXPLORER = {"sda": 20, "scl": 21}  # Default i2c pins for Pico Explorer

i2c = PimoroniI2C(**PINS_BREAKOUT_GARDEN)
rtc = BreakoutRTC(i2c)

if rtc.is_12_hour():
    rtc.set_24_hour()

rtc.enable_periodic_update_interrupt(True)

while True:
    if rtc.read_periodic_update_interrupt_flag():
        rtc.clear_periodic_update_interrupt_flag()

        if rtc.update_time():
            rtc_date = rtc.string_date()
            rtc_time = rtc.string_time()
            print("Date: ", rtc_date, ", Time: ", rtc_time, sep="")

    time.sleep(0.1)

Reading it without updating first would be an option too. I can easily set it on a Pi.

If I do this

#if rtc.is_12_hour():
rtc.set_12_hour()

#rtc.enable_periodic_update_interrupt(True)

I get 12 hour time and it doesn’t auto update. Just have to figure out how to set it to a specific time?
@gadgetoid

Anybody? =( Am I the only one using this breakout with a PICO?
@hel

I’ve not had chance to plug one of these into a Pico yet, but it looks like you should be able to set the RTC time with rtc.set_time(sec, min, hour, weekday, date, month, year) (there are also micropython bindings for set_seconds, set_minutes etc if you just want to set one of the parameters?)

Thanks, I’ll give that a try and post back how it goes.

What do you enter for year? Or have I messed up one of the other entries?
rtc.set_time(00, 00, 09, 02, 06, 07, 00)
gets me
Date: 06/07/2048, Time: 09:00:20AM
and
rtc.set_time(00, 00, 09, 02, 06, 07, 21)
gets me
Date: 06/07/2069, Time: 09:00:00AM

Using 2021 for year gets me
ValueError: year out of range. Expected 0 to 99

EDIT: Is the following correct?
rtc.set_time(sec, min, hour, weekday, date, month, year)
sec = 0-59
min = 0-59
hour = 0-23
weekday = 1-7
date = 1-31
month = 1-12
year = 0-99

Once I run rtc.set_time its all down hill from there. I get the wrong year 2069 and running the stock demo file won’t reset it / update it to UTC? Even taking the battery out didn’t help? Running the demo just got me
Date: 01/01/2000, Time: 00:00:01 and it wouldn’t update? I’ve tried two different RV3028’s that worked as advertised on my Raspberry Pi Breakout Garden. I’ll have to plug this one into my raspberry Pi and run set_time.py to get it back to the correct time.

It looks like a library bug. rtc.set_time constrains the year to the range [00-99], but then calls the corresponding RV3028 driver routine, which treats it as a 16-bit unsigned int and subtracts 2000 from it.

When you take all that into account, the results you got are actually inevitable:

00 (rtc) → -2000 (RV3028) → 0xf830 (16-bit uint) → 72 (BCD) → 48 (decimal)

21 (rtc) → -1979 (RV3028) → 0xf845 (16-bit uint) → 105 (BCD) → 69 (decimal)

2021 (rtc) → 21 (RV3028) → 0x0015 (16-bit uint) → 33 (BCD) → 21 (decimal)

Methinks rtc.set_time should actually take the full year instead of artificially constraining the input value.

I think I’m going to have to report this as an issue on the github page.
Thanks for posting that info.

EDIT: RV3028 RTC with PICO, manual set time doesn’t work correctly. · Issue #176 · pimoroni/pimoroni-pico (github.com)

1 Like

@lexfiend Problem solved / fixed by Phil Howard @gadgetoid. There is a link to the fixed file here.
RV3028 RTC with PICO, manual set time doesn’t work correctly. · Issue #176 · pimoroni/pimoroni-pico (github.com)

What you posted was the issue and that info made fixing it easier. Thanks.

1 Like