Simple test

Ensure your device works with this simple test.

examples/ds3231_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 DS3231 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
11import adafruit_ds3231
12
13i2c = board.I2C()  # uses board.SCL and board.SDA
14rtc = adafruit_ds3231.DS3231(i2c)
15
16# Lookup table for names of days (nicer printing).
17days = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
18
19
20# pylint: disable-msg=using-constant-test
21if False:  # change to True if you want to set the time!
22    #                     year, mon, date, hour, min, sec, wday, yday, isdst
23    t = time.struct_time((2017, 10, 29, 15, 14, 15, 0, -1, -1))
24    # you must set year, mon, date, hour, min, sec and weekday
25    # yearday is not supported, isdst can be set but we don't do anything with it at this time
26    print("Setting time to:", t)  # uncomment for debugging
27    rtc.datetime = t
28    print()
29# pylint: enable-msg=using-constant-test
30
31# Main loop:
32while True:
33    t = rtc.datetime
34    # print(t)     # uncomment for debugging
35    print(
36        "The date is {} {}/{}/{}".format(
37            days[int(t.tm_wday)], t.tm_mday, t.tm_mon, t.tm_year
38        )
39    )
40    print("The time is {}:{:02}:{:02}".format(t.tm_hour, t.tm_min, t.tm_sec))
41    time.sleep(1)  # wait a second