Before using the real time clock (RTC) for the first time on the PiCowbell Adalogger, you need to calibrate it by setting the time with the code.py file below. After setting the time, the RTC module will use the coin cell battery to keep time even when you unplug the PiCowbell from the Raspberry Pi Pico.
Begin by inserting a CR1220 coin cell battery into the PiCowbell Adalogger battery holder. Then, attach the PiCowbell to a Pico or Pico W as described in the assembly pages.
CircuitPython Usage
To use with CircuitPython, you need to first install the Adafruit_CircuitPython_PCF8523 module, and its dependencies, into the lib folder on your CIRCUITPY drive. Then you need to update code.py with the example script.
Thankfully, we can do this in one go. In the example below, click the Download Project Bundle button below to download the necessary libraries and the code.py file in a zip file. Extract the contents of the zip file.
Connect your Pico + PiCowbell sandwich to your computer via a known good USB data+power cable. Your board should show up as a thumb drive named CIRCUITPY in your File Explorer or Finder (depending on your operating system). Copy the entire lib folder and the code.py file to your CIRCUITPY drive.
Your CIRCUITPY/lib folder should contain the following folders and files:
- /adafruit_bus_device
- /adafruit_register
- adafruit_pcf8523.mpy
Code
Once everything is saved to the CIRCUITPY drive, connect to the serial console to see the data printed out!
# SPDX-FileCopyrightText: 2017 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT import time import board import busio from adafruit_pcf8523.pcf8523 import PCF8523 I2C = busio.I2C(board.GP5, board.GP4) rtc = PCF8523(I2C) days = ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday") set_time = False if set_time: # change to True if you want to write the time! # year, mon, date, hour, min, sec, wday, yday, isdst t = time.struct_time((2023, 3, 6, 10, 55, 00, 1, -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_mon, t.tm_mday, 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
set_time = False if set_time: # change to True if you want to write the time! # year, mon, date, hour, min, sec, wday, yday, isdst t = time.struct_time((2023, 3, 6, 11, 05, 00, 1, -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 set_time
in the first line to be True
:
set_time = True
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 set_time
to False
:
set_time = False
and save, so you don't reset the RTC again.
The code will now output the time and date.
Text editor powered by tinymce.