Setting DS3231 programmatically #9876
-
I have a problem with setting a DS3231 RTC clock from within the program. I am using the ds3231_i2c library and everything is working fine within the program. Initially I set the clock to the correct time using the recommended code, which is used once then commented out:
Again, this works fine. However, there may be a situation when the RTC loses the correct time and the user needs to reset it within the program. My problem is trying to recreate the I can easily obtain the necessary settings in the form ['22', '11', '7', '17', '3', '0'] but how do I then convert this into the required form? I've been struggling over this for a day without success, so I'd be grateful for any advice. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
Maybe, look at ubinascii |
Beta Was this translation helpful? Give feedback.
-
Is it this library you're using? balance19/micropython_DS3231: A library to run the DS3231 RTC on Raspberry Pi Pico That's a really odd way to go about setting the clock. Looks like the library is expecting binary coded decimal. A couple of routines like this might help: def bcd(n):
n = n % 100
return 16 * (n // 10) + n % 10
def date_to_bytes(sec, minutes, hour, week, day, month, year):
# week is week of the year, seemingly
return bytes(
[bcd(sec), bcd(minutes), bcd(hour), bcd(week),
bcd(day), bcd(month), bcd(year)]
)
# example 2022-11-07 21:25:00 (in week 45)
print(date_to_bytes(0, 25, 21, 45, 7, 11, 22)) This returns Alternatively, you could use a different library. Peter Hinch's ds3231_port.py works well for me, with library docs here: The DS3231 real time clock chip |
Beta Was this translation helpful? Give feedback.
-
See also this module. |
Beta Was this translation helpful? Give feedback.
Is it this library you're using? balance19/micropython_DS3231: A library to run the DS3231 RTC on Raspberry Pi Pico
That's a really odd way to go about setting the clock. Looks like the library is expecting binary coded decimal.
A couple of routines like this might help:
This returns
b'\x00%!E\x07\x11"'
, which is a…