Calibration value out of range

Like I said, the get_time() code is very clear you can read it yourself, the internal RTC time has nothing to do with it, even if set_rtc=True (which is false by default) it only sets the internal RTC from the external one (not the other way around). I assume your RTC module has a battery ? Also if you have any PYB board you should try the module with it and the above script see if it works, if it still doesn’t work you should report an issue to that repo.

Thanks for the response.
Yeah, you are right, I guess the library will need to be adapted to work well in my situation since it was tested on PYB boards which I suppose should work well with OpenMV Cam board, but seems not to be the case directly. Will look into it. Thanks once again for the comments and the insight.

I got a DS3231 module finally and I tested it and there’s nothing wrong with the library, you’re either using it wrong or the module you have is broken or doesn’t have a battery (does it have a battery ?)

Initially you have to set the external RTC time once, using the internal RTC:

from ds3231_port import DS3231
from machine import Pin, I2C, RTC

i2c = I2C(sda = Pin('P5'), scl=Pin('P4'))
ds3231 = DS3231(i2c)

# Set Internal RTC time.
# (year, month, day, weekday, hours, minutes, seconds, subseconds)
RTC().datetime((2021, 9, 12, 1, 5, 20, 0, 0))

# Update External RTC time form the internal RTC.
ds3231.save_time()

print("DS3231 RTC Time:", ds3231.get_time())

Output:

>>> Warning: I2C(-1, ...) is deprecated, use SoftI2C(...) instead
DS3231 RTC Time: (2021, 9, 12, 5, 20, 0, 0, 0)

Then disconnect the cam, leave it off for say 5 minutes and connect and run this:

from ds3231_port import DS3231
from machine import Pin, I2C, RTC

i2c = I2C(sda = Pin('P5'), scl=Pin('P4'))
ds3231 = DS3231(i2c)

print("DS3231 RTC Time:", ds3231.get_time())

Output:

>>> Warning: I2C(-1, ...) is deprecated, use SoftI2C(...) instead
DS3231 RTC Time: (2021, 9, 12, 5, 25, 12, 0, 0)

You should something like this which is as expected.

You can also set the internal RTC from the external one, in boot.py/main.py and read it back with time.localtime()

NOTE : This time will only be kept while the cam is connected to a power source.

from ds3231_port import DS3231
from machine import Pin, I2C, RTC

i2c = I2C(sda = Pin('P5'), scl=Pin('P4'))
ds3231 = DS3231(i2c)

# Set internal RTC from external RTC.
ds3231.get_time(set_rtc=True)

import time
print("Local time", time.localtime())

Note: the STM32 can use the hardware I2C (there’s no need to use the software I2C) in this case it’s I2C 2, to remove those warnings, for example:

ds3231 = DS3231(I2C(2))
print(ds3231.get_time())

The only thing that I had issues with is the calibration, for some reason it returns a value way outside the range:

Pyboard 1.x. Waiting 5 minutes for calibration factor.
Error -37253823.3ppm -70894012.5mins/year. Cal factor 39050128
Calibration factor is 39050128

Thanks for checking this up.
It was the same thing I did before except for the calibration. I will check the battery that is in the RTC module, because I guess it may not be working which maybe the reason why it was not working in my case.
Will check it and give feedback.

Yes it seems the calibration routine is broken, and when called it will mess up the RTC time, it’s probably best to avoid calibration.

Yeah, it was the battery indeed.
Thanks for following this up, everything is working fine.

1 Like

FYI I tested the RTC with Pyboard LITE V1.0 and the calibration routine for this series is broken. I reported an issue upstream.

MicroPython v1.17-20-g0a5107372-dirty on 2021-09-13; PYBLITEv1.0 with STM32F411RE
Type "help()" for more information.
>>> 
paste mode; Ctrl-C to cancel, Ctrl-D to finish
=== from ds3231_pb import DS3231
=== from machine import I2C
=== 
=== ds3231 = DS3231(I2C(3))
=== print("DS3231 RTC Time:", ds3231.get_time(set_rtc=True))
=== 
=== import time
=== print(time.localtime())
=== 
=== ds3231.calibrate()
=== 
DS3231 RTC Time: (2021, 9, 13, 5, 36, 39, 1, 0)
(2021, 9, 13, 5, 36, 39, 1, 256)
Pyboard 1.x. Waiting 5 minutes for calibration factor.
Error -52822.7ppm -100521.5mins/year. Cal factor 55369
Traceback (most recent call last):
  File "<stdin>", line 10, in <module>
  File "ds3231_pb.py", line 183, in calibrate
ValueError: calibration value out of range

EDIT so apparently the reason it can’t be used to calibrate the internal RTC is because it needs the slow (32KHz) crystal to do that, which we don’t have on any of our boards.

Oh I see, now it makes sense.
But anyway, the RTC module can still work normally without the calibration at least for my application, so I think it is fine.