Simple test
examples/ds1302_simpletest.py
1# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
2# SPDX-FileCopyrightText: Copyright (c) 2026 Avery Ramsey
3#
4# SPDX-License-Identifier: Unlicense
5
6import time
7
8import board
9
10import ds1302
11
12# Set up the three interface pins
13ce_pin = board.GP13
14io_pin = board.GP14
15ck_pin = board.GP15
16
17# Instantiate the RTC
18rtc = ds1302.DS1302(ce_pin, io_pin, ck_pin)
19
20# Lookup table for names of days (nicer printing).
21days = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
22
23if True: # Change to True to set the clock
24 # year, mon, date, hour, min, sec, wday, yday, isdst
25 t = time.struct_time((2026, 7, 16, 12, 38, 0, 3, -1, -1))
26 # you must set all values
27 # setting tm_yday has no effect; it is calculated from the date when reading
28 # setting tm_isdst has no effect; it will always return -1
29 print("Setting time to:", t) # uncomment for debugging
30 rtc.datetime = t
31 print()
32
33# Main loop:
34while True:
35 t = rtc.datetime
36 # print(t) # uncomment for debugging
37 print(f"The date is {days[int(t.tm_wday)]} {t.tm_mday}/{t.tm_mon}/{t.tm_year}")
38 print(f"The time is {t.tm_hour}:{t.tm_min:02}:{t.tm_sec:02}")
39 time.sleep(1) # wait a second
Other tests
RAM Example
# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
# SPDX-FileCopyrightText: Copyright (c) 2026 Avery Ramsey
#
# SPDX-License-Identifier: Unlicense
import board
import ds1302
# Set up the three interface pins
ce_pin = board.GP13
io_pin = board.GP14
ck_pin = board.GP15
# Instantiate the RTC
rtc = ds1302.DS1302(ce_pin, io_pin, ck_pin)
# Clear the user-accessible RAM
rtc.clear_ram()
# Write value 29 to RAM address 5
number = 29
rtc.write_ram_single(5, number)
# Read address 5 and print the value
data = rtc.read_ram_single(5)
print("Single value:", data)
# Checks whether the returned value is equal to 29
print("Match:", data == number)
# Write values of 1 to 10 addresses 16 to 25
array = bytearray([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
rtc.write_ram(16, array)
# Read addresses 16 to 25 and print the values
data_array = rtc.read_ram(16, 10)
print("Block Values:")
for value in data_array:
print(value)
# Check whether data_array is equal to the original array
print("Match:", data_array == array)
Trickle Charger Example
# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
# SPDX-FileCopyrightText: Copyright (c) 2026 Avery Ramsey
#
# SPDX-License-Identifier: Unlicense
# WARNING:
# Only enable the trickle charger when using a rechargeable backup
# power source and an appropriate charging configuration.
# Incorrect use may damage the backup battery or power source.
# See page 7 of the DS1302 datasheet for details:
# https://www.analog.com/media/en/technical-documentation/data-sheets/ds1302.pdf
import board
import ds1302
# Set up the three interface pins
ce_pin = board.GP13
io_pin = board.GP14
ck_pin = board.GP15
# Instantiate the RTC
rtc = ds1302.DS1302(ce_pin, io_pin, ck_pin)
# Use one diode in the trickle charger
rtc.trickle_diode = ds1302.TRICKLE_DIODE_1
# Set the trickle charger resistance to 4kΩ
rtc.trickle_resistor = ds1302.TRICKLE_RESISTOR_4
# Enable the trickle charger
rtc.trickle_enable = True