Skip to content

Commit ad2fa6d

Browse files
mrmcwethytannewt
authored andcommitted
added examples folder and .py file
1 parent e32222e commit ad2fa6d

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

examples/demo.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Simple demo of reading and writing the time for the PCF8523 real-time clock.
2+
# Change the if False to if True below to set the time, otherwise it will just
3+
# print the current date and time every second. Notice also comments to adjust
4+
# for working with hardware vs. software I2C.
5+
6+
import time
7+
import board
8+
# For hardware I2C (M0 boards) use this line:
9+
import busio as io
10+
# Or for software I2C (ESP8266) use this line instead:
11+
#import bitbangio as io
12+
13+
import adafruit_pcf8523
14+
15+
# Change to the appropriate I2C clock & data pins here!
16+
i2c_bus = io.I2C(board.SCL, board.SDA)
17+
18+
# Create the RTC instance:
19+
rtc = adafruit_pcf8523.PCF8523(i2c_bus)
20+
21+
# Lookup table for names of days (nicer printing).
22+
days = ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
23+
24+
25+
#pylint: disable-msg=bad-whitespace
26+
#pylint: disable-msg=using-constant-test
27+
if False: # change to True if you want to set the time!
28+
# year, mon, date, hour, min, sec, wday, yday, isdst
29+
t = time.struct_time((2017, 10, 29, 10, 31, 0, 0, -1, -1))
30+
# you must set year, mon, date, hour, min, sec and weekday
31+
# yearday is not supported, isdst can be set but we don't do anything with it at this time
32+
print("Setting time to:", t) # uncomment for debugging
33+
rtc.datetime = t
34+
print()
35+
#pylint: enable-msg=using-constant-test
36+
#pylint: enable-msg=bad-whitespace
37+
38+
# Main loop:
39+
while True:
40+
t = rtc.datetime
41+
#print(t) # uncomment for debugging
42+
print("The date is {} {}/{}/{}".format(days[int(t.tm_wday)], t.tm_mday, t.tm_mon, t.tm_year))
43+
print("The time is {}:{:02}:{:02}".format(t.tm_hour, t.tm_min, t.tm_sec))
44+
time.sleep(1) # wait a second

0 commit comments

Comments
 (0)