Helpful Scripts for CircuitPython & Real Time Clocks (RTCs)

I have used two different RTCs in the Feather form factor. One has the PCF8523, and the other has the DS3231. The former has an SD card slot while the latter has higher precision including a temperature-compensated crystal oscillator.

One interesting wrinkle is that both devices have the same I2C address, 0x68, but incompatible register layouts! The following script,, will detect which one is attached, and set it as the CircuitPython time source. I tried several detection methods, but settled on trying to read from register 19. The DS3231 only has registers 0 through 18, so this fails. Other tests I tried were checking which register "counted up" in seconds,or when reading enough registers that the addresses "wrap around", what bytes matched. Ultiamtely, this seems the most likely to accurately distinguish these two parts.

(Note that the DS1307 also uses the 0x68 address, but this code is not designed to detect it. Doing a read at register 63 would be a good way to detect it, probably)

The next problem is setting the time. This program, run on a Linux host, will set the time. Note that it sets the RTC to UTC/GMT time, not to local time, in order to work with my time zone handling code. You could change the time.gmtime call to time.localtime if that's not what you want.

It can probably be adapted to other systems. Python-serial aka pyserial is required, and must be installed on the target device. Depending what your "" or "" does you may need to temporarily rename it before running this script.

Entry first conceived on 16 July 2020, 15:12 UTC, last modified on 18 July 2020, 20:56 UTC
Website Copyright © 2004-2024 Jeff Epler