Skip to content

Commit b120cda

Browse files
authored
Merge pull request #1 from SolderedElectronics/dev
Merge dev into main
2 parents fc396af + c4d3f4d commit b120cda

14 files changed

Lines changed: 1889 additions & 0 deletions

File tree

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# FILE: DS3234-simpleRTC.py
2+
# AUTHOR: Soldered
3+
# BRIEF: Example showing how to wake up from deep sleep
4+
# Using the INT pin on the breakout board
5+
# WORKS WITH: DS3234 RTC Breakout: www.solde.red/333358
6+
# LAST UPDATED: 2025-09-15
7+
8+
import machine
9+
import time
10+
import esp32
11+
from DS3234 import DS3234
12+
13+
# Define CS pin for DS3234
14+
RTC_CS_PIN = 5
15+
16+
# Define wake-up pin (must be RTC-capable GPIO)
17+
# ESP32 RTC-capable pins: 0, 2, 4, 12-15, 25-27, 32-39
18+
WAKEUP_PIN = 26 # Must be RTC-capable pin
19+
20+
# Initialize SPI and CS pin for DS3234
21+
# Using VSPI (SPI ID 2) with default ESP32 pins
22+
spi = machine.SPI(
23+
2,
24+
baudrate=1000000,
25+
polarity=1,
26+
phase=1,
27+
sck=machine.Pin(18),
28+
mosi=machine.Pin(23),
29+
miso=machine.Pin(19),
30+
)
31+
cs_pin = machine.Pin(RTC_CS_PIN, machine.Pin.OUT)
32+
33+
# Create an instance of the RTC object
34+
rtc = DS3234(spi, cs_pin)
35+
36+
print("Initializing DS3234 RTC...")
37+
38+
# Check if this is a wake-up from deep sleep
39+
wake_reason = machine.wake_reason()
40+
41+
if wake_reason == machine.PIN_WAKE:
42+
print("Woke up from deep sleep (RTC alarm)!")
43+
44+
# Read and display current time
45+
rtc.update()
46+
47+
print(
48+
"Current time: {0}:{1:02d}:{2:02d}".format(
49+
rtc.hour(), rtc.minute(), rtc.second()
50+
)
51+
)
52+
53+
print("Date: {0}/{1}/{2}".format(rtc.month(), rtc.date(), rtc.year() + 2000))
54+
55+
# Clear alarm flags
56+
if rtc.alarm1(clear=True):
57+
print("Cleared Alarm 1 flag")
58+
if rtc.alarm2(clear=True):
59+
print("Cleared Alarm 2 flag")
60+
61+
else:
62+
# First boot - set up RTC and configure alarm
63+
print("First boot - setting up RTC")
64+
65+
# Clear any existing alarm flags first
66+
rtc.alarm1(clear=True)
67+
rtc.alarm2(clear=True)
68+
69+
# Set RTC time (use autoTime or set manually)
70+
rtc.autoTime()
71+
72+
# Display current time
73+
rtc.update()
74+
print(
75+
"Current RTC time: {0}:{1:02d}:{2:02d}".format(
76+
rtc.hour(), rtc.minute(), rtc.second()
77+
)
78+
)
79+
80+
# Configure alarm to trigger in 30 seconds from now
81+
current_second = rtc.second()
82+
alarm_second = (current_second + 30) % 60
83+
alarm_minute = rtc.minute() + ((current_second + 30) // 60)
84+
85+
if alarm_minute >= 60:
86+
alarm_minute %= 60
87+
88+
# Set alarm 1 to trigger at specific second (ignore minutes, hours, date)
89+
# 255 = "don't care" for that field
90+
rtc.setAlarm1(second=alarm_second, minute=255, hour=255, date=255, day=False)
91+
92+
# Enable alarm 1 interrupt - THIS IS CRITICAL!
93+
rtc.enableAlarmInterrupt(alarm1=True, alarm2=False)
94+
95+
# Verify alarm is set correctly
96+
print("Alarm set to trigger at second:", alarm_second)
97+
98+
# Configure the interrupt pin as input with pullup
99+
wakeup_pin = machine.Pin(WAKEUP_PIN, machine.Pin.IN, machine.Pin.PULL_UP)
100+
101+
# Configure deep sleep with RTC alarm wakeup
102+
print("Going to deep sleep...")
103+
time.sleep(1)
104+
105+
# Set up wakeup source - use external pin triggered by RTC alarm
106+
# The DS3234 INT pin goes LOW when alarm triggers
107+
esp32.wake_on_ext0(pin=wakeup_pin, level=esp32.WAKEUP_ALL_LOW)
108+
109+
# Go to deep sleep
110+
machine.deepsleep()
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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

Comments
 (0)