RTC with CircuitPython

Wiring

Wiring it up is easy, connect

  • GND to GND on your board
  • VCC to the logic level power of your board - every CircuitPython board uses 3.3V
  • SDA to the SDA i2c data pin
  • SCL to the SCL i2c clock pin

There are internal 10K pull-ups on the PCF8523 on SDA and SCL to the VCC voltage

Adafruit CircuitPython Library Install

To use the RTC sensor with your Adafruit CircuitPython board you'll need to install the Adafruit_CircuitPython_PCF8523 module on your board.

First make sure you are running the latest version of Adafruit CircuitPython for your board.

Next you'll need to install the necessary libraries to use the hardware--carefully follow the steps to find and install these libraries from Adafruit's CircuitPython library bundle.  For example the Circuit Playground Express guide has a great page on how to install the library bundle for both express and non-express boards.

Remember for non-express boards like the Trinket M0, Gemma M0, and Feather/Metro M0 basic you'll need to manually install the necessary libraries from the bundle:

  • adafruit_bus_device folder
  • adafruit_register folder
  • adafruit_pcf8523.mpy

Before continuing make sure your board's lib folder or root filesystem has the adafruit_pcf8523.mpy module, the adafruit_register folder, and the adafruit_bus_device folder copied over.

Usage

To demonstrate the usage of the PCF8523 module you can connect to your board's serial REPL to see the output while saving our example sketch to main.py

First connect to the board's serial REPL so you are at the CircuitPython >>> prompt.

Then save this script to main.py (back up or remove whatever was there before)

import busio
import adafruit_pcf8523
import time
import board

myI2C = busio.I2C(board.SCL, board.SDA)
rtc = adafruit_pcf8523.PCF8523(myI2C)

days = ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")

if False:   # change to True if you want to write the time!
    #                     year, mon, date, hour, min, sec, wday, yday, isdst
    t = time.struct_time((2017,  10,   29,   15,  14,  15,    0,   -1,    -1))
    # you must set year, mon, date, hour, min, sec and weekday
    # yearday is not supported, isdst can be set but we don't do anything with it at this time
    
    print("Setting time to:", t)     # uncomment for debugging
    rtc.datetime = t
    print()
    
while True:
    t = rtc.datetime
    #print(t)     # uncomment for debugging

    print("The date is %s %d/%d/%d" % (days[t.tm_wday], t.tm_mday, t.tm_mon, t.tm_year))
    print("The time is %d:%02d:%02d" % (t.tm_hour, t.tm_min, t.tm_sec))
    
    time.sleep(1) # wait a second

Setting the time

The first time you run the program, you'll need to set the time

find these lines:

if False:   # change to True if you want to write the time!
    #                     year, mon, date, hour, min, sec, wday, yday, isdst
    t = time.struct_time((2017,  10,   29,   15,  14,  15,    0,   -1,    -1))
    # you must set year, mon, date, hour, min, sec and weekday
    # yearday is not supported, isdst can be set but we don't do anything with it at this time

Change the False to True in the first line, and update the time.struct_time to have the current time starting from year to weekday. The last two entries can stay at -1

Re-run the sketch by saving and you'll see this out of the REPL:

Note the part where the program says it is Setting time to:

Now you can go back and change the if True to if False and save, so you don't re-set the RTC again.

The script will now output the time and date

Last updated on 2017-10-29 at 03.48.46 PM Published on 2017-10-29 at 03.51.59 PM