|
| 1 | +# FILE: DS3234-simpleRTC.py |
| 2 | +# AUTHOR: Soldered |
| 3 | +# BRIEF: Example showing how to Initialize the DS3234 RTC |
| 4 | +# and set the time and alarms |
| 5 | +# WORKS WITH: DS3234 RTC Breakout: www.solde.red/333358 |
| 6 | +# LAST UPDATED: 2025-09-15 |
| 7 | + |
| 8 | +import machine |
| 9 | +import time |
| 10 | +from ds3234 import DS3234 |
| 11 | + |
| 12 | +# Configurable Pin Definitions for ESP32 |
| 13 | +DS3234_CS_PIN = 5 # DS3234 RTC Chip-select pin |
| 14 | + |
| 15 | +# ESP32 SPI pin assignments (change these based on your wiring) |
| 16 | +# Default ESP32 SPI pins: |
| 17 | +# HSPI: SCLK=14, MOSI=13, MISO=12 |
| 18 | +# VSPI: SCLK=18, MOSI=23, MISO=19 |
| 19 | + |
| 20 | +# Using HSPI |
| 21 | +spi = machine.SPI( |
| 22 | + 1, |
| 23 | + baudrate=1000000, |
| 24 | + polarity=1, |
| 25 | + phase=1, |
| 26 | + sck=machine.Pin(18), |
| 27 | + mosi=machine.Pin(23), |
| 28 | + miso=machine.Pin(19), |
| 29 | +) |
| 30 | +cs_pin = machine.Pin(DS3234_CS_PIN, machine.Pin.OUT) |
| 31 | + |
| 32 | +# Create an instance of the RTC object |
| 33 | +rtc = DS3234(spi, cs_pin) |
| 34 | + |
| 35 | +# Create an instance of the RTC object |
| 36 | +# If using the SQW pin as an interrupt (uncomment if needed) |
| 37 | +# interrupt_pin = machine.Pin(INTERRUPT_PIN, machine.Pin.IN, machine.Pin.PULL_UP) |
| 38 | + |
| 39 | +# Use the serial monitor to view time/date output |
| 40 | +print("Initializing DS3234 RTC...") |
| 41 | + |
| 42 | +# Initialize the RTC |
| 43 | +# (The __init__ method already handles initialization in the class) |
| 44 | + |
| 45 | +# Set to 12-hour mode if desired (uncomment) |
| 46 | +# rtc.set12Hour() |
| 47 | + |
| 48 | +# Now set the time... |
| 49 | +# You can use the autoTime() function to set the RTC's clock and |
| 50 | +# date to the system's current time |
| 51 | +rtc.autoTime() |
| 52 | + |
| 53 | +# Or you can use the rtc.setTime(s, m, h, day, date, month, year) |
| 54 | +# function to explicitly set the time: |
| 55 | +# e.g. 7:32:16 | Monday October 31, 2016: |
| 56 | +# rtc.setTime(16, 32, 7, 2, 31, 10, 16) # Uncomment to manually set time |
| 57 | + |
| 58 | +# Update time/date values, so we can set alarms |
| 59 | +rtc.update() |
| 60 | + |
| 61 | +# Configure Alarm(s): |
| 62 | +# (Optional: enable SQW pin as an interrupt) |
| 63 | +rtc.enableAlarmInterrupt() |
| 64 | + |
| 65 | +# Set alarm1 to alert when seconds hits 30 |
| 66 | +rtc.setAlarm1(30) |
| 67 | + |
| 68 | +# Set alarm2 to alert when minute increments by 1 |
| 69 | +rtc.setAlarm2(rtc.minute() + 1) |
| 70 | + |
| 71 | +print("RTC initialized and alarms set") |
| 72 | + |
| 73 | + |
| 74 | +def print_time(): |
| 75 | + # Print hour |
| 76 | + print(str(rtc.hour()) + ":", end="") |
| 77 | + |
| 78 | + # Print minute with leading zero if needed |
| 79 | + if rtc.minute() < 10: |
| 80 | + print("0", end="") |
| 81 | + print(str(rtc.minute()) + ":", end="") |
| 82 | + |
| 83 | + # Print second with leading zero if needed |
| 84 | + if rtc.second() < 10: |
| 85 | + print("0", end="") |
| 86 | + print(str(rtc.second()), end="") |
| 87 | + |
| 88 | + # If we're in 12-hour mode |
| 89 | + if rtc.is12hour(): |
| 90 | + # Use rtc.pm() to read the AM/PM state of the hour |
| 91 | + if rtc.pm(): |
| 92 | + print(" PM", end="") # Returns true if PM |
| 93 | + else: |
| 94 | + print(" AM", end="") |
| 95 | + |
| 96 | + print(" | ", end="") |
| 97 | + |
| 98 | + # Few options for printing the day, pick one: |
| 99 | + print(rtc.dayStr(), end="") # Print day string |
| 100 | + # print(rtc.dayChar(), end="") # Print day character |
| 101 | + # print(rtc.day(), end="") # Print day integer (1-7, Sun-Sat) |
| 102 | + |
| 103 | + print(" - ", end="") |
| 104 | + |
| 105 | + # Print date in USA format (MM/DD/YYYY) or international (DD/MM/YYYY) |
| 106 | + PRINT_USA_DATE = True # Change to False for international format |
| 107 | + |
| 108 | + if PRINT_USA_DATE: |
| 109 | + print( |
| 110 | + str(rtc.month()) + "/" + str(rtc.date()) + "/", end="" |
| 111 | + ) # Print month/date |
| 112 | + else: |
| 113 | + print( |
| 114 | + str(rtc.date()) + "/" + str(rtc.month()) + "/", end="" |
| 115 | + ) # Print date/month |
| 116 | + |
| 117 | + print(str(rtc.year())) # Print year |
| 118 | + |
| 119 | + |
| 120 | +last_second = -1 |
| 121 | + |
| 122 | +while True: |
| 123 | + rtc.update() |
| 124 | + |
| 125 | + if rtc.second() != last_second: # If the second has changed |
| 126 | + print_time() # Print the new time |
| 127 | + last_second = rtc.second() # Update last_second value |
| 128 | + |
| 129 | + # Check for alarm interrupts |
| 130 | + # If using interrupt pin (uncomment if needed): |
| 131 | + # if interrupt_pin.value() == 0: # Interrupt pin is active-low |
| 132 | + # Check rtc.alarm1() to see if alarm 1 triggered the interrupt |
| 133 | + |
| 134 | + if rtc.alarm1(clear=True) == True: # Clear the alarm flag |
| 135 | + print("ALARM 1!") |
| 136 | + # Re-set the alarm for when s=30: |
| 137 | + rtc.setAlarm1(30) |
| 138 | + |
| 139 | + # Check rtc.alarm2() to see if alarm 2 triggered the interrupt |
| 140 | + if rtc.alarm2(clear=True) == True: # Clear the alarm flag |
| 141 | + print("ALARM 2!") |
| 142 | + # Re-set the alarm for when m increments by 1 |
| 143 | + rtc.setAlarm2(rtc.minute() + 1) |
| 144 | + |
| 145 | + time.sleep(0.1) # Small delay to prevent busy waiting |
0 commit comments