Dear Ms Santos,
Following your suggestion on how to download the driver for the ds1302, I tried to adapt the code on page 456 to suit the ds1302. The modified code is shown hereunder:
import time
import ds1302
from machine import I2C, Pin
days_of_week = [‘Monday’, ‘Tuesday’, ‘Wednesday’, ‘Thursday’, ‘Friday’, ‘Saturday’, ‘Sunday’]
# Initialize RTC (connected to I2C)
#i2c = I2C(1, scl=Pin(15), sda=Pin(14))
rtc = ds1302.DS1302(Pin(5), Pin(14), Pin(15))
# Set the current time using a specified time tuple
# Time tuple: (year, month, day, day of week, hour, minute, seconds, milliseconds)
#initial_time = (2024, 1, 30, 1, 12, 30, 0, 0)
# Or get the local time from the system
initial_time_tuple = time.localtime() # tuple (MicroPython)
initial_time_seconds = time.mktime(initial_time_tuple) # local time in seconds
# Convert to tuple compatible with the library
initial_time = ds1302.seconds2tuple(initial_time_seconds)
# Sync the RTC
rtc.datetime(initial_time)
while True:
current_datetime = rtc.datetime()
print(‘Current date and time:’)
print(‘Year:’, current_datetime.year)
print(‘Month:’, current_datetime.month)
print(‘Day:’, current_datetime.day)
print(‘Hour:’, current_datetime.hour)
print(‘Minute:’, current_datetime.minute)
print(‘Second:’, current_datetime.second)
print(‘Day of the Week:’, days_of_week[current_datetime.weekday])
time.sleep(1)
When trying to run the code I get the error for line 21 “‘module’ object has no attribute ‘seconds2tuple'”.
Can you please help?
Philip