{
  "id": "mattytrentini/micropython-ds3231",
  "name": "micropython-ds3231",
  "prettyName": "micropython-ds3231",
  "description": "Standalone MicroPython I2C driver for the DS3231 RTC",
  "version": "",
  "author": "mattytrentini",
  "license": "MIT",
  "tags": [],
  "status": "VALID",
  "official": false,
  "source": "https://github.com/mattytrentini/micropython-ds3231",
  "updatedAt": "2026-11-21T22:29:15.003Z",
  "page": "https://checkmim.com/packages/mattytrentini+micropython-ds3231",
  "createdAt": "2026-11-21T22:29:13.103Z",
  "submittedBy": "Matt",
  "install": {
    "mpremote": "mpremote mip install github:mattytrentini/micropython-ds3231",
    "mip": "import mip; mip.install(\"github:mattytrentini/micropython-ds3231\")"
  },
  "readme": "# DS3231 for MicroPython\n\nMIT-licensed I²C driver for the DS3231 real-time clock. Provides date and time,\nboth alarms, oscillator-stop status and square-wave output. The driver accepts\nan existing I²C bus and defaults to address `0x68`.\n\nThis is the unchanged DS3231 driver by Willem Peterse from the\n[original repository](https://github.com/pangopi/micropython-DS3231-AT24C32),\npackaged separately. Some DS3231 breakout boards also carry an EEPROM; use\n[the AT24C32 driver](https://github.com/mattytrentini/micropython-at24c32)\nfor that chip. The two packages have no dependency on each other.\n\n## Install\n\n```sh\nmpremote mip install github:mattytrentini/micropython-ds3231\n```\n\nOn a network-connected board:\n\n```python\nimport mip\nmip.install(\"github:mattytrentini/micropython-ds3231\")\n```\n\nThe package installs `ds3231.py` with no external dependencies. The driver\nhandles years 2000–2099. Temperature and aging offset are not implemented.\n\n## Usage\n\n### Create instance\nCreate an instance of the DS3231 class:\n\n```python\nfrom machine import Pin, I2C\nfrom ds3231 import DS3231\n\ni2c = I2C(sda=Pin(4), scl=Pin(5))\n\nds = DS3231(i2c)\n```\n\nFor a KinCony CO16, use I2C0 with SDA GPIO8, SCL GPIO18, and the RTC at `0x68`:\n\n```python\nfrom machine import Pin, I2C\nfrom ds3231 import DS3231\n\ni2c = I2C(0, sda=Pin(8), scl=Pin(18), freq=100_000)\nds = DS3231(i2c, addr=0x68)\nif ds.OSF():\n    print(\"RTC time is not trustworthy; set it from a known-good source.\")\nelse:\n    print(ds.datetime())\n```\n\nThis example only reads the RTC; it does not set the clock or clear its status.\n\nA read-only CO16 smoke verified that the mip-installed driver reads advancing\ntime fields. OSF was set on that board, so the smoke did not trust the calendar,\nset the clock, clear OSF, or change alarm/output configuration.\n\n### Time functions\n\nExample of setting the date and time:\n```python\nyear = 2020 # Can be yyyy or yy format\nmonth = 10\nmday = 3\nhour = 13 # 24 hour format only\nminute = 55\nsecond = 30 # Optional\nweekday = 6 # Optional\n\ndatetime = (year, month, mday, hour, minute, second, weekday)\nds.datetime(datetime)\n```\n\nThe getter `ds.datetime()` returns an eight-element tuple in ESP8266 RTC order:\n`(2020, 10, 3, 6, 13, 55, 30, 0) # (year, month, mday, weekday, hour, minute, second, 0)`\n\n**The setter and getter use different tuple orders.** The setter accepts\n`(year, month, mday, hour, minute[, second[, weekday]])`, not the getter's\ntuple. Do not pass a getter result directly back to `ds.datetime(...)`.\nOmitted seconds and weekday are written as zero; an explicitly supplied\nweekday follows the DS3231's 1–7 convention. Setting the clock clears OSF,\nso first check `ds.OSF()` and use a known-good time source when setting it.\n\nSet the ESP8266 internal RTC using the DS3231\n```python\nimport machine\nrtc = machine.RTC()\nif not ds.OSF():\n    rtc.datetime(ds.datetime())\nelse:\n    print(\"RTC time is not trustworthy; internal RTC left unchanged.\")\n```\n\n#### Get the date and time\nCall `ds.datetime()` to get the current date and time. It prints a warning when\nthe Oscillator Stop Flag (OSF) is set, but still returns a tuple; that tuple\nmust not be treated as reliable solely because it was returned. Read\n`ds.OSF()` before using the time, including before copying it to the internal\nRTC. Reading OSF or the date/time does not clear OSF.\n\n### Alarm functions\n\nThe DS3231 has 2 internal alarms with can each be set independently to different match conditions.\nThe alarm has match options ranging from every second to every month.\n\nCall an alarm without arguments and it will return the current alarm setting register.\n\n#### alarm 1\nSet alarm 1:\n`DS3231.alarm1([time[, match[, int_en[, weekday]]]])`\n\ntime    : tuple, (second,[ minute[, hour[, day]]])\nweekday : bool, select mday (False) or wday (True)\nmatch   : int, match const\nint_en  : bool, enable interrupt on alarm match on SQW/INT pin (disables SQW output)\n\n\nAlarm 1 has the following match options:\n```python\nds.AL1_EVERY_S      # Alarm every second\nds.AL1_MATCH_S      # Alarm when seconds match (every minute)\nds.AL1_MATCH_MS     # Alarm when minutes, seconds match (every hour)\nds.AL1_MATCH_HMS    # Alarm when hours, minutes, seconds match (every day)\nds.AL1_MATCH_DHMS   # Alarm when day|wday, hour, min, sec match (specific wday / mday) (once per month/week)\n```\n\nExample:\n```python\n# Set alarm 1 for 16:10:15 every day\nds.alarm1((15, 10, 16), match=ds.AL1_MATCH_HMS)\n```\n\n#### alarm 2\nSet alarm 2:\n`DS3231.alarm2([time[, match[, int_en[, weekday]]]])`\n\ntime    : tuple, (minute[, hour[, day]])\nweekday : bool, select mday (False) or wday (True)\nmatch   : int, match const\nint_en  : bool, enable interrupt on alarm match on SQW/INT pin (disables SQW output)\n\n\nAlarm 2 has the following match options:\n```python\nds.AL2_EVERY_M # Alarm every minute on 00 seconds\nds.AL2_MATCH_M # Alarm when minutes match (every hour)\nds.AL2_MATCH_HM # Alarm when hours and minutes match (every day)\nds.AL2_MATCH_DHM # Alarm when day|wday match (once per month/week)\n```\n\nExample:\n```python\n# Set alarm 1 for 19:30 every Tuesday\nds.alarm2((30, 19, 2), match=ds.AL2_MATCH_DHM, weekday=True)\n```\n\n#### Checking and clearing alarms\nYou 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.\n\n`DS3231.check_alarm(alarm)`\n\nalarm : alarm number, can be 1 or 2\n\n### Square wave output\nThe DS3231 can output a square wave on the SQW/INT output pin. The following frequencies are availalbe:\n* `FREQ_1`       1 Hz\n* `FREQ_1024` 1024 Hz\n* `FREQ_4096` 4096 Hz\n* `FREQ_8192` 8192 Hz\n\nExample:\n```python\nds.square_wave(freq=ds.FREQ_1024) # Enable 1024 Hz output\nds.square_wave(freq=False) # Disable SQW output\n```\n\nNote:\n* 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.\n* 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()`.\n\nThe 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:\n\n```python\nds.output_32kHz() # Enable 32 kHz output\nds.output_32kHz(False) # Disable 32 kHz output\n```\n"
}