DS3231 for MicroPython

MIT-licensed I²C driver for the DS3231 real-time clock. Provides date and time, both alarms, oscillator-stop status and square-wave output. The driver accepts an existing I²C bus and defaults to address 0x68.

This is the unchanged DS3231 driver by Willem Peterse from the original repository (opens in a new tab), packaged separately. Some DS3231 breakout boards also carry an EEPROM; use the AT24C32 driver (opens in a new tab) for that chip. The two packages have no dependency on each other.

Install

mpremote mip install github:mattytrentini/micropython-ds3231

On a network-connected board:

import mip
mip.install("github:mattytrentini/micropython-ds3231")

The package installs ds3231.py with no external dependencies. The driver handles years 2000–2099. Temperature and aging offset are not implemented.

Usage

Create instance

Create an instance of the DS3231 class:

from machine import Pin, I2C
from ds3231 import DS3231

i2c = I2C(sda=Pin(4), scl=Pin(5))

ds = DS3231(i2c)

For a KinCony CO16, use I2C0 with SDA GPIO8, SCL GPIO18, and the RTC at 0x68:

from machine import Pin, I2C
from ds3231 import DS3231

i2c = I2C(0, sda=Pin(8), scl=Pin(18), freq=100_000)
ds = DS3231(i2c, addr=0x68)
if ds.OSF():
    print("RTC time is not trustworthy; set it from a known-good source.")
else:
    print(ds.datetime())

This example only reads the RTC; it does not set the clock or clear its status.

A read-only CO16 smoke verified that the mip-installed driver reads advancing time fields. OSF was set on that board, so the smoke did not trust the calendar, set the clock, clear OSF, or change alarm/output configuration.

Time functions

Example of setting the date and time:

year = 2020 # Can be yyyy or yy format
month = 10
mday = 3
hour = 13 # 24 hour format only
minute = 55
second = 30 # Optional
weekday = 6 # Optional

datetime = (year, month, mday, hour, minute, second, weekday)
ds.datetime(datetime)

The getter ds.datetime() returns an eight-element tuple in ESP8266 RTC order: (2020, 10, 3, 6, 13, 55, 30, 0) # (year, month, mday, weekday, hour, minute, second, 0)

The setter and getter use different tuple orders. The setter accepts (year, month, mday, hour, minute[, second[, weekday]]), not the getter's tuple. Do not pass a getter result directly back to ds.datetime(...). Omitted seconds and weekday are written as zero; an explicitly supplied weekday follows the DS3231's 1–7 convention. Setting the clock clears OSF, so first check ds.OSF() and use a known-good time source when setting it.

Set the ESP8266 internal RTC using the DS3231

import machine
rtc = machine.RTC()
if not ds.OSF():
    rtc.datetime(ds.datetime())
else:
    print("RTC time is not trustworthy; internal RTC left unchanged.")

Get the date and time

Call ds.datetime() to get the current date and time. It prints a warning when the Oscillator Stop Flag (OSF) is set, but still returns a tuple; that tuple must not be treated as reliable solely because it was returned. Read ds.OSF() before using the time, including before copying it to the internal RTC. Reading OSF or the date/time does not clear OSF.

Alarm functions

The DS3231 has 2 internal alarms with can each be set independently to different match conditions. The alarm has match options ranging from every second to every month.

Call an alarm without arguments and it will return the current alarm setting register.

alarm 1

Set alarm 1: DS3231.alarm1([time[, match[, int_en[, weekday]]]])

time : tuple, (second,[ minute[, hour[, day]]]) weekday : bool, select mday (False) or wday (True) match : int, match const int_en : bool, enable interrupt on alarm match on SQW/INT pin (disables SQW output)

Alarm 1 has the following match options:

ds.AL1_EVERY_S      # Alarm every second
ds.AL1_MATCH_S      # Alarm when seconds match (every minute)
ds.AL1_MATCH_MS     # Alarm when minutes, seconds match (every hour)
ds.AL1_MATCH_HMS    # Alarm when hours, minutes, seconds match (every day)
ds.AL1_MATCH_DHMS   # Alarm when day|wday, hour, min, sec match (specific wday / mday) (once per month/week)

Example:

# Set alarm 1 for 16:10:15 every day
ds.alarm1((15, 10, 16), match=ds.AL1_MATCH_HMS)

alarm 2

Set alarm 2: DS3231.alarm2([time[, match[, int_en[, weekday]]]])

time : tuple, (minute[, hour[, day]]) weekday : bool, select mday (False) or wday (True) match : int, match const int_en : bool, enable interrupt on alarm match on SQW/INT pin (disables SQW output)

Alarm 2 has the following match options:

ds.AL2_EVERY_M # Alarm every minute on 00 seconds
ds.AL2_MATCH_M # Alarm when minutes match (every hour)
ds.AL2_MATCH_HM # Alarm when hours and minutes match (every day)
ds.AL2_MATCH_DHM # Alarm when day|wday match (once per month/week)

Example:

# Set alarm 1 for 19:30 every Tuesday
ds.alarm2((30, 19, 2), match=ds.AL2_MATCH_DHM, weekday=True)

Checking and clearing alarms

You can manually check to see if an alarm has triggered. This will return True when the alarm has been triggered and False otherwise. The act of checking clears the alarm register automatically.

DS3231.check_alarm(alarm)

alarm : alarm number, can be 1 or 2

Square wave output

The DS3231 can output a square wave on the SQW/INT output pin. The following frequencies are availalbe:

  • FREQ_1 1 Hz
  • FREQ_1024 1024 Hz
  • FREQ_4096 4096 Hz
  • FREQ_8192 8192 Hz

Example:

ds.square_wave(freq=ds.FREQ_1024) # Enable 1024 Hz output
ds.square_wave(freq=False) # Disable SQW output

Note:

  • When using the SQW/INT pin to output a square wave, the alarm interrupt is not avaialable. Checking for alarms will have to be done manually.
  • When disabling the SQW output signal, the alarm interrupts (even when set before) remain disabled. They can to be set manually afterwards, if desired with ds.alarm_int().

The DS3231 can also output the crystal frequency at 32768 Hz on a dedicated pin (32K). This output is enabled by default on powerup and can me changed as follows:

ds.output_32kHz() # Enable 32 kHz output
ds.output_32kHz(False) # Disable 32 kHz output