Calibration value out of range

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