# Reference:
# https://wokwi.com/projects/395700228833743873
# https://wokwi.com/projects/359558101922696193
from machine import I2C, Pin
from time import sleep
import utime
# OLED libray
from ssd1306 import SSD1306_I2C
# very important
# this modules needs to be saved in the Raspberry Pi Pico in order for the RTC to be used
import ds1307
# I2C Initialisation:
# creating I2C objects, specifying the data (SDA) and clock (SCL) pins used in the Raspberry Pi Pico
# any SDA and SCL pins in the Raspberry Pi Pico can be used (check documentation for SDA and SCL pins)
i2c_rtc = I2C(0, scl=Pin(9), sda=Pin(8), freq=100000)
# I2C object
# creating an DS1307 RTC object using I2C
ds1307rtc = ds1307.DS1307(i2c_rtc, 0x68)
pix_res_x = 128
pix_res_y = 64
def init_i2c(scl_pin, sda_pin): # GPIO 27=SCL, SDA=26 for OLED
# Initialize I2C device
i2c_dev = I2C(1, scl=Pin(scl_pin), sda=Pin(sda_pin), freq=200000)
i2c_addr = [hex(ii) for ii in i2c_dev.scan()]
if not i2c_addr:
print('No I2C Display Found')
sys.exit()
else:
print("I2C Address : {}".format(i2c_addr[0]))
print("I2C Configuration: {}".format(i2c_dev))
return i2c_dev
def get_date_string():
dt = ds1307rtc.datetime
return str(dt[2]) + "." + str(dt[1]) + "." + str(dt[0])
def get_time_string():
dt = ds1307rtc.datetime
return str(dt[3]) + ":" + str(dt[4])+ ":" + str(dt[5])
def display_time(oled):
while True:
# Clear the specific line by drawing a filled black rectangle
oled.fill_rect(5, 40, oled.width - 5, 8, 0)
oled.text("Date:", 5, 10)
oled.text(get_date_string(),5, 20)
oled.text("Time:", 5, 30)
oled.text(get_time_string(),5, 40)
oled.show()
sleep(5)
def main():
i2c_dev = init_i2c(scl_pin=27, sda_pin=26)
oled = SSD1306_I2C(pix_res_x, pix_res_y, i2c_dev)
while True:
display_time(oled)
if __name__ == '__main__':
main()