- 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. Our introduction 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, 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 code.py
Next connect to the board's serial REPL so you are at the CircuitPython >>> prompt.
Then save this script to code.py (back up or remove whatever was there before)
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT # Simple demo of reading and writing the time for the PCF8523 real-time clock. # Change the if False to if True below to set the time, otherwise it will just # print the current date and time every second. Notice also comments to adjust # for working with hardware vs. software I2C. import time import board from adafruit_pcf8523.pcf8523 import PCF8523 i2c = board.I2C() # uses board.SCL and board.SDA # i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller rtc = PCF8523(i2c) # Lookup table for names of days (nicer printing). days = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday") if False: # change to True if you want to set the time! # year, mon, date, hour, min, sec, wday, yday, isdst t = time.struct_time((2017, 10, 29, 10, 31, 0, 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() # pylint: enable-msg=using-constant-test # Main loop: while True: t = rtc.datetime # print(t) # uncomment for debugging print(f"The date is {days[int(t.tm_wday)]} {t.tm_mday}/{t.tm_mon}/{t.tm_year}") print(f"The time is {t.tm_hour}:{t.tm_min:02}:{t.tm_sec:02}") time.sleep(1) # wait a second
if False: # change to True if you want to set the time! # year, mon, date, hour, min, sec, wday, yday, isdst t = time.struct_time((2017, 10, 29, 10, 31, 0, 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()
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
Page last edited March 08, 2024
Text editor powered by tinymce.