from machine import Pin, I2C
import utime
import tm1637
import ds3231
# Setup for TM1637 4-digit display
clk = Pin(20) # Clock pin (GPIO 20)
dio = Pin(21) # Data pin (GPIO 21)
display = tm1637.TM1637(clk=clk, dio=dio)
# Setup for DS3231 RTC (I2C)
# Use I2C on GPIO 0 (SDA) and GPIO 1 (SCL) for I2C bus 0
i2c = I2C(0, scl=Pin(1), sda=Pin(0)) # Correct I2C bus and pins for the Pico
rtc = ds3231.DS3231(i2c)
# Function to display time on terminal and 7-segment display
def display_time():
while True:
# Get the current datetime from the DS3231
current_time = rtc.datetime()
# Extract the hour, minute, and second
hour = current_time[4] # Hour is at index 4
minute = current_time[5] # Minute is at index 5
second = current_time[6] # Second is at index 6
# Print the time to the terminal in HH:MM:SS format
print(f"Current Time: {hour:02}:{minute:02}:{second:02}")
# Display the hour and minute on the TM1637 display
display.numbers(hour, minute)
# Wait for 1 second before updating the display
utime.sleep(1)
try:
display.brightness(7) # Set brightness level (0-7)
display_time() # Start displaying time on both terminal and TM1637
except KeyboardInterrupt:
display.clear() # Clear the display when exiting
print("Program stopped.")