{"id":13613421,"url":"https://github.com/mcauser/micropython-tinyrtc-i2c","last_synced_at":"2025-07-18T17:05:50.939Z","repository":{"id":48318198,"uuid":"127133870","full_name":"mcauser/micropython-tinyrtc-i2c","owner":"mcauser","description":"MicroPython driver for TinyRTC I2C modules with DS1307 RTC and AT24C32N EEPROM","archived":false,"fork":false,"pushed_at":"2023-01-13T02:09:57.000Z","size":913,"stargazers_count":64,"open_issues_count":2,"forks_count":21,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-04T15:50:41.285Z","etag":null,"topics":["at24c32","ds1307","eeprom","micropython","rtc","tinyrtc-i2c"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mcauser.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-03-28T12:03:09.000Z","updated_at":"2025-02-10T18:14:18.000Z","dependencies_parsed_at":"2023-02-09T14:15:35.067Z","dependency_job_id":null,"html_url":"https://github.com/mcauser/micropython-tinyrtc-i2c","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/mcauser/micropython-tinyrtc-i2c","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcauser%2Fmicropython-tinyrtc-i2c","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcauser%2Fmicropython-tinyrtc-i2c/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcauser%2Fmicropython-tinyrtc-i2c/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcauser%2Fmicropython-tinyrtc-i2c/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mcauser","download_url":"https://codeload.github.com/mcauser/micropython-tinyrtc-i2c/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcauser%2Fmicropython-tinyrtc-i2c/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265798339,"owners_count":23829922,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["at24c32","ds1307","eeprom","micropython","rtc","tinyrtc-i2c"],"created_at":"2024-08-01T20:00:46.458Z","updated_at":"2025-07-18T17:05:50.910Z","avatar_url":"https://github.com/mcauser.png","language":"Python","funding_links":[],"categories":["Libraries","精选驱动库"],"sub_categories":["Communications","通讯类"],"readme":"# MicroPython TinyRTC I2C module\n\nMicroPython driver for the TinyRTC I2C module, featuring a DS1307 RTC and AT24C32N EEPROM.\n\n![demo](docs/demo.jpg)\n\n## Pins\n\nPins are available on both sides. You can use either. One side exposes more pins.\n\nPin | Name            | Description\n--- | --------------- | ----------------------------------------------------\nBAT | Battery Voltage | For monitoring battery voltage\nGND | Ground          | Ground\nVCC | Input Supply    | +5V power for module and to charge coin cell battery\nSDA | I2C Data        | I2C data for DS1307 and EEPROM\nSCL | I2C Clock       | I2C clock for DS1307 and EEPROM\nDS  | DS18B20         | Optional temperature sensor 1-wire data\nSQ  | Square Wave     | Optional square wave or logic level output\n\n# DS1307 RTC Examples\n\n```python\n# VCC-GND STM32F407VET6 on SPI3\n\u003e\u003e\u003e from machine import I2C, Pin\n\u003e\u003e\u003e i2c = I2C(3) # SCL=A8, SDA=C9\n\u003e\u003e\u003e i2c.scan()\n[80, 104]\n\n# 80 == 0x50 == AT24C32N EEPROM\n# 104 == 0x68 == DS1307\n\n\u003e\u003e\u003e import ds1307\n\u003e\u003e\u003e ds = ds1307.DS1307(i2c)\nds.datetime()\nds.datetime(now)\n\n# initial datetime value, oscillator disabled\n\u003e\u003e\u003e ds.datetime()\n(2000, 1, 1, 0, 0, 0, 0, 0)\n\n# read datetime after 1 second (no change)\n\u003e\u003e\u003e ds.datetime()\n(2000, 1, 1, 0, 0, 0, 0, 0)\n\n# enable oscillator\n\u003e\u003e\u003e ds.halt(False)\n\n# read datetime after 1 second\n\u003e\u003e\u003e ds.datetime()\n(2000, 1, 1, 0, 0, 0, 1, 0)\n\n# read datetime after 1 more second\n\u003e\u003e\u003e ds.datetime()\n(2000, 1, 1, 0, 0, 0, 2, 0)\n\n# read datetime after 1 more second\n\u003e\u003e\u003e ds.datetime()\n(2000, 1, 1, 0, 0, 0, 3, 0)\n\n# set the datetime 24th March 2018 at 1:45:21 PM\n\u003e\u003e\u003e now = (2018, 3, 24, 6, 13, 45, 21, 0)\n\u003e\u003e\u003e ds.datetime(now)\n\n# read datetime after 1 second\n\u003e\u003e\u003e ds.datetime()\n(2018, 3, 24, 6, 13, 45, 22, 0)\n\n# read datetime after 1 second\n\u003e\u003e\u003e ds.datetime()\n(2018, 3, 24, 6, 13, 45, 23, 0)\n\n# disable oscillator\n\u003e\u003e\u003e ds.halt(True)\n\n# read datetime after 1 second\n\u003e\u003e\u003e ds.datetime()\n(2018, 3, 24, 6, 13, 45, 24, 0)\n\n# read datetime after 1 second (no change)\n\u003e\u003e\u003e ds.datetime()\n(2018, 3, 24, 6, 13, 45, 24, 0)\n\n# minute increment (00:00:59 -\u003e 00:01:00)\n\u003e\u003e\u003e ds.datetime((2018, 3, 24, 6, 0, 0, 58, 0))\n\u003e\u003e\u003e ds.datetime()\n(2018, 3, 24, 6, 0, 0, 59, 0)\n\u003e\u003e\u003e ds.datetime()\n(2018, 3, 24, 6, 0, 1, 0, 0)\n\n# hour increment (00:59:59 -\u003e 01:00:00)\n\u003e\u003e\u003e ds.datetime((2018, 3, 24, 6, 0, 59, 58, 0))\n\u003e\u003e\u003e ds.datetime()\n(2018, 3, 24, 6, 0, 59, 59, 0)\n\u003e\u003e\u003e ds.datetime()\n(2018, 3, 24, 6, 1, 0, 0, 0)\n\n# day + weekday increment (23:59:59 Sat -\u003e 00:00:00 Sun)\n\u003e\u003e\u003e ds.datetime((2018, 3, 24, 6, 23, 59, 58, 0))\n\u003e\u003e\u003e ds.datetime()\n(2018, 3, 24, 6, 23, 59, 59, 0)\n\u003e\u003e\u003e ds.datetime()\n(2018, 3, 25, 0, 0, 0, 0, 0)\n\n# month increment (Wed 28th Feb 2018 -\u003e Thu 1st Mar 2018)\n\u003e\u003e\u003e ds.datetime((2018, 2, 28, 3, 23, 59, 58, 0))\n\u003e\u003e\u003e ds.datetime()\n(2018, 2, 28, 3, 23, 59, 59, 0)\n\u003e\u003e\u003e ds.datetime()\n(2018, 3, 1, 4, 0, 0, 0, 0)\n\n# month increment on leap year Feb has 29 days (Sun 28th Feb 2016 -\u003e Mon 29th Feb 2016)\n\u003e\u003e\u003e ds.datetime((2016, 2, 28, 0, 23, 59, 58, 0))\n\u003e\u003e\u003e ds.datetime()\n(2016, 2, 28, 0, 23, 59, 59, 0)\n\u003e\u003e\u003e ds.datetime()\n(2016, 2, 29, 1, 0, 0, 0, 0)\n\n# month increment on leap year (Mon 29th Feb 2016 -\u003e Tue 1st Mar 2016)\n\u003e\u003e\u003e ds.datetime((2016, 2, 29, 1, 23, 59, 58, 0))\n\u003e\u003e\u003e ds.datetime()\n(2016, 2, 29, 1, 23, 59, 59, 0)\n\u003e\u003e\u003e ds.datetime()\n(2016, 3, 1, 2, 0, 0, 0, 0)\n\n# year increment (31st Dec 2018 -\u003e 1st Jan 2019)\n\u003e\u003e\u003e ds.datetime((2018, 12, 31, 1, 23, 59, 58, 0))\n\u003e\u003e\u003e ds.datetime()\n(2018, 12, 31, 1, 23, 59, 59, 0)\n\u003e\u003e\u003e ds.datetime()\n(2019, 1, 1, 2, 0, 0, 0, 0)\n\n# square wave output on pin SQ - 1Hz\n\u003e\u003e\u003e ds.square_wave(1, 0)\n\n# square wave output on pin SQ - 4.096kHz\n\u003e\u003e\u003e ds.square_wave(4, 0)\n\n# square wave output on pin SQ - 8.192kHz\n\u003e\u003e\u003e ds.square_wave(8, 0)\n\n# square wave output on pin SQ - 32.768kHz\n\u003e\u003e\u003e ds.square_wave(32, 0)\n\n# logic level output on pin SQ - high\n\u003e\u003e\u003e ds.square_wave(0, 1)\n\n# logic level output on pin SQ - low\n\u003e\u003e\u003e ds.square_wave(0, 0)\n```\n\n# DS18B20 1-Wire Temperature Sensor Examples\n\n```python\n\u003e\u003e\u003e from machine import Pin\n\u003e\u003e\u003e import onewire\n\u003e\u003e\u003e ow = onewire.OneWire(Pin('A9'))\n\u003e\u003e\u003e ow.scan()\n[bytearray(b'(\\xc9E\\x01\\x00\\x00\\x80/')]\n\n\u003e\u003e\u003e import time, ds18x20\n\u003e\u003e\u003e ds = ds18x20.DS18X20(ow)\n\u003e\u003e\u003e roms = ds.scan()\n\u003e\u003e\u003e ds.convert_temp()\n\u003e\u003e\u003e time.sleep_ms(750)\n\u003e\u003e\u003e for rom in roms:\n...     print(ds.read_temp(rom))\n23.8125\n```\n\n# AT24C32N EEPROM Examples\n\n```python\n\u003e\u003e\u003e from machine import I2C, Pin\n\u003e\u003e\u003e i2c = I2C(3) # SCL=A8, SDA=C9\n\u003e\u003e\u003e i2c.scan()\n[80, 104]\n\n\u003e\u003e\u003e import at24c32n\n\u003e\u003e\u003e eeprom = at24c32n.AT24C32N(i2c)\n\n# read 32 bytes starting from memory address 0\n\u003e\u003e\u003e eeprom.read(0, 32)\nb'\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff'\n\n# write hello world to memory address 0\n\u003e\u003e\u003e eeprom.write(0, 'hello world')\n\n# read back the hello world\n\u003e\u003e\u003e eeprom.read(0, 32)\nb'hello world\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff'\n\u003e\u003e\u003e eeprom.read(0, 11)\nb'hello world'\n\n# write multiple pages (32 bytes per page)\n\u003e\u003e\u003e eeprom.write(0, b'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ')\n\n# read multiple pages (32 bytes per page)\n\u003e\u003e\u003e eeprom.read(0,64)\nb'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\\xff\\xff'\n\n# mutliple page writes (fill pages 0 and 1 with test data)\n# write is performed in two steps, \"0\" x 32 in the first page, then \"1\" x 32 in the next page\n# memory address was at the start of a page boundary so each write is a full 32 byte page write\n\u003e\u003e\u003e eeprom.write(0, b'0000000000000000000000000000000011111111111111111111111111111111')\n\u003e\u003e\u003e eeprom.read(0,64)\nb'0000000000000000000000000000000011111111111111111111111111111111'\n\n# partial page writes\n# write some bytes spanning two pages, not starting at a page boundary\n# write is performed in two steps, \"abc\" in the first page then \"def\" in the next page\n\u003e\u003e\u003e eeprom.write(29, b'abcdef')\n\u003e\u003e\u003e eeprom.read(0,64)\nb'00000000000000000000000000000abcdef11111111111111111111111111111'\n\n# fill entire eeprom with 0xFF\n\u003e\u003e\u003e buf = b'\\xff' * 32\n\u003e\u003e\u003e for i in range(128):\n...     eeprom.write(i*32, buf)\n\n# show page boundaries\n\u003e\u003e\u003e for i in range(128):\n...     eeprom.write(i*32, '|-Page-{:-\u003c24}|'.format(i))\n\u003e\u003e\u003e eeprom.read(0,128)\nb'|-Page-0-----------------------||-Page-1-----------------------||-Page-2-----------------------||-Page-3-----------------------|'\n\n# read entire eeprom\n\u003e\u003e\u003e eeprom.read(0,4096)\n```\n\n## PyBoard / STM32F4 built-in RTC\n\nYou do not need this module if you are using a PyBoard or STM32 microcontroller with [built in RTC hardware](http://www.st.com/content/ccc/resource/technical/document/application_note/7a/9c/de/da/84/e7/47/8a/DM00025071.pdf/files/DM00025071.pdf/jcr:content/translations/en.DM00025071.pdf).\n\nSimply add a coin cell battery between GND + VBAT and use [pyb.RTC](http://docs.micropython.org/en/latest/pyboard/library/pyb.RTC.html#pyb-rtc).\n\nOn the early pyboards, VBAT is the backup battery for the RTC. On the newer boards, VBAT is for powering the pyboard using a battery and the RTC battery is called Vbackup.\n- Dave Hylands on the [micropython forum](https://forum.micropython.org/viewtopic.php?f=6\u0026t=229\u0026p=15352\u0026hilit=vbat#p15352)\n\n```python\n# on the PyBoard or a STM32F407\nrtc = pyb.RTC()\n# set time\nrtc.datetime((2018, 3, 24, 6, 13, 45, 21, 0))\n# attach battery between GND + VBAT\n# disconnect, reconnect\n# get time\nprint(rtc.datetime())\n```\n\n## Troubleshooting\n\nIf you are using the ESP32 [loboris port](https://github.com/loboris/MicroPython_ESP32_psRAM_LoBo), swap the `addrsize=16` for a `adrlen=2`.\n\n## Parts\n\n* [TinyRTC I2C module](https://www.aliexpress.com/item/Tiny-RTC-I2C-modules-24C32-memory-DS1307-clock-RTC-module-without-battery-good-quality-low-price/2020927349.html) $0.53 AUD\n* [LIR2032 rechargeable coin cell](https://www.aliexpress.com/item/2x-LIR2032-Rechargeable-Lithium-Li-ion-Batteries-3-6V-40mAh-Button-Coin-Cells-Battery-For-Watch/32842926682.html) $1.19 AUD\n\n## Connections\n\nSTM32F407VET6 | TinyRTC I2C module\n------------- | ----------------------\nA9 (any pin)  | DS\nA8 (I2C3 SCL) | SCL\nC9 (I2C3 SDA) | SDA\n3V3           | VCC\nGND           | GND\n\n## Links\n\n* [micropython.org](http://micropython.org)\n* [Binary-coded decimal](https://en.wikipedia.org/wiki/Binary-coded_decimal)\n* [DS1307 datasheet](https://datasheets.maximintegrated.com/en/ds/DS1307.pdf)\n* [AT24C32 datasheet](http://ww1.microchip.com/downloads/en/devicedoc/doc0336.pdf)\n\n## License\n\nLicensed under the [MIT License](http://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmcauser%2Fmicropython-tinyrtc-i2c","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmcauser%2Fmicropython-tinyrtc-i2c","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmcauser%2Fmicropython-tinyrtc-i2c/lists"}