import machine
import time
from ds1307 import DS1307
# Initialize I2C bus
i2c = machine.I2C(-1, machine.Pin(22), machine.Pin(21))
rtc = DS1307(i2c)
# Define buzzer pin
BUZZER_PIN = 4
# Initialize buzzer
buzzer = machine.Pin(BUZZER_PIN, machine.Pin.OUT)
# Function to get current date and time from RTC
def get_datetime():
return rtc.datetime()
# Function to format date and time
def format_datetime(dt):
return "{:02d}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}".format(dt[0], dt[1], dt[2], dt[4], dt[5], dt[6])
# Function to check if it's time for the alarm
def check_alarm(hours, minutes):
dt = get_datetime()
return dt[4] == hours and dt[5] == minutes
# Function to sound the buzzer
def sound_buzzer(duration_ms):
buzzer.on()
time.sleep_ms(duration_ms)
buzzer.off()
# Main loop
while True:
dt = get_datetime()
formatted_datetime = format_datetime(dt)
# Print date and time
print(formatted_datetime)
# Check if it's time for the alarm (adjust hours and minutes as needed)
if check_alarm(5, 25): # Set alarm for 10:00 AM
# Sound the buzzer for 1 second (adjust as needed)
sound_buzzer(40000) # 1000 ms = 1 second
# Wait for a while (adjust as needed)
time.sleep(1)