from machine import Pin, SoftI2C, RTC
import ssd1306
import time
# Initialize I2C connection
i2c = SoftI2C(sda=Pin(0), scl=Pin(1))
# Initialize 128x64 OLED display
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
# Initialize the Raspberry Pi Pico Real-Time Clock
rtc = RTC()
# Set initial time: (year, month, day, weekday, hours, minutes, seconds, subseconds)
# You can change these numbers to match your current local time
rtc.datetime((2026, 6, 3, 3, 14, 30, 0, 0))
while True:
# 1. Fetch current timestamp tuple from RTC
current_time = rtc.datetime()
# 2. Extract values from tuple structure
year = current_time[0]
month = current_time[1]
day = current_time[2]
hour = current_time[4]
minute = current_time[5]
second = current_time[6]
# 3. Format strings with leading zeros using string formatting syntax
date_str = "{:04d}-{:02d}-{:02d}".format(year, month, day)
time_str = "{:02d}:{:02d}:{:02d}".format(hour, minute, second)
# 4. Refresh display canvas
oled.fill(0)
# Write text labels at target pixel coordinates
oled.text("LOCAL CLOCK", 24, 4)
oled.text(date_str, 24, 24)
oled.text(time_str, 32, 44)
# Render updated text frames to hardware
oled.show()
# 5. Delay execution to prevent display flickering
time.sleep(1)