API Reference
ds1302
CircuitPython driver for the Maxim Integrated / Analog Devices DS1302 Real Time Clock.
Author(s): Avery Ramsey
Implementation Notes
Hardware:
Maxim Integrated / Analog Devices DS1302 Real Time Clock
Software and Dependencies:
Adafruit CircuitPython firmware for the supported boards: https://circuitpython.org/downloads
- class ds1302.DS1302(ce_pin: microcontroller.Pin, io_pin: microcontroller.Pin, ck_pin: microcontroller.Pin)
Interfaces with the DS1302 real-time clock
Create a DS1302 RTC.
- Parameters:
ce_pin (microcontroller.Pin) – Chip Enable pin.
io_pin (microcontroller.Pin) – I/O pin.
ck_pin (microcontroller.Pin) – Clock pin.
Quickstart: Importing and using the device
Here is an example of using the
DS1302class.First, import the required libraries:
import time import board import ds1302
Next, define your Chip Enable, I/O, and Clock 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)
Set the current date and time:
t = time.struct_time((2026, 7, 16, 12, 38, 0, 3, -1, -1)) rtc.datetime = t
Read the current date and time from the
datetimeproperty:current_time = rtc.datetime
- static clamp(num: int | float, a: int | float, b: int | float) int | float
Clamp
numbetweenaandb.- Parameters:
- Returns:
numconstrained to the range defined byaandb.- Return type:
aandbmay be provided in either order.
- property clock_enable: bool
Get or set whether the RTC clock is enabled as a
bool.When disabled, the oscillator is stopped and the DS1302 enters a low-power standby mode with a current draw of less than 100nA.
- property datetime: struct_time
Get or set the current date and time as a
time.struct_time.Reading this property returns the current date and time.
The
tm_ydayfield of an assignedtime.struct_timeis ignored. When readingdatetime,tm_ydayis calculated from the stored date.Assign a
time.struct_timeto update the RTC date and time.- Raises:
TypeError – If the assigned value is not a
time.struct_time.
- property is_12_hour: bool
Whether the RTC uses 12-hour or 24-hour mode internally as a
bool.This does not affect
datetime, which always uses 24-hour time.
- read_ram(offset: int, length: int = 1) bytearray
Read a block of user-accessible RAM.
- Parameters:
- Returns:
The data read from RAM.
- Return type:
- Raises:
ValueError – If
offsetorlengthis out of range.IndexError – If the read would extend beyond the end of RAM.
- read_ram_single(offset: int) int
Read a single byte of user-accessible RAM.
- Parameters:
offset (int) – The RAM address (0-30) to read.
- Returns:
The value stored at
offset.- Return type:
- Raises:
ValueError – If
offsetis out of range.
- property trickle_enable: bool
Get or set whether the trickle charger is enabled as a
bool.The trickle charger is only active when both
trickle_diodeandtrickle_resistorare configured.
- property trickle_resistor: int
Get or set the trickle charger resistor configuration.
Valid values are:
TRICKLE_RESISTOR_2(2kΩ)TRICKLE_RESISTOR_4(4kΩ)TRICKLE_RESISTOR_8(8kΩ)
- write_ram(offset: int, data: ReadableBuffer) None
Write a block of user-accessible RAM.
- Parameters:
offset (int) – The starting RAM address (0-30) to write to.
data (ReadableBuffer) – The byte data to write to RAM.
- Raises:
ValueError – If
offsetis out of range or any value indatais not between 0 and 255.IndexError – If the write would extend beyond the end of RAM.
- ds1302.TRICKLE_DIODE_1 = 4
One diode drop in the trickle charger path.
- ds1302.TRICKLE_DIODE_2 = 8
Two diode drops in the trickle charger path.
- ds1302.TRICKLE_RESISTOR_2 = 1
2kΩ trickle charger resistor.
- ds1302.TRICKLE_RESISTOR_4 = 2
4kΩ trickle charger resistor.
- ds1302.TRICKLE_RESISTOR_8 = 3
8kΩ trickle charger resistor.