Demo

examples/pcf8523_simpletest.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4# Simple demo of reading and writing the time for the PCF8523 real-time clock.
 5# Change the if False to if True below to set the time, otherwise it will just
 6# print the current date and time every second.  Notice also comments to adjust
 7# for working with hardware vs. software I2C.
 8
 9import time
10import board
11from adafruit_pcf8523.pcf8523 import PCF8523
12
13i2c = board.I2C()  # uses board.SCL and board.SDA
14# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
15rtc = PCF8523(i2c)
16
17# Lookup table for names of days (nicer printing).
18days = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
19
20
21# pylint: disable-msg=using-constant-test
22if False:  # change to True if you want to set the time!
23    #                     year, mon, date, hour, min, sec, wday, yday, isdst
24    t = time.struct_time((2017, 10, 29, 10, 31, 0, 0, -1, -1))
25    # you must set year, mon, date, hour, min, sec and weekday
26    # yearday is not supported, isdst can be set but we don't do anything with it at this time
27    print("Setting time to:", t)  # uncomment for debugging
28    rtc.datetime = t
29    print()
30# pylint: enable-msg=using-constant-test
31
32# Main loop:
33while True:
34    t = rtc.datetime
35    # print(t)     # uncomment for debugging
36    print(
37        "The date is {} {}/{}/{}".format(
38            days[int(t.tm_wday)], t.tm_mday, t.tm_mon, t.tm_year
39        )
40    )
41    print("The time is {}:{:02}:{:02}".format(t.tm_hour, t.tm_min, t.tm_sec))
42    time.sleep(1)  # wait a second