Display the system time on a four digit seven segment display.
# SPDX-FileCopyrightText: 2019 Mikey Sklar for Adafruit Industries # # SPDX-License-Identifier: MIT import time import datetime from adafruit_ht16k33 import segments import board import busio # Create the I2C interface. i2c = busio.I2C(board.SCL, board.SDA) # Create the LED segment class. # This creates a 7 segment 4 character display: display = segments.Seg7x4(i2c) # clear display display.fill(0) while True: # get system time now = datetime.datetime.now() hour = now.hour minute = now.minute second = now.second # setup HH:MM for display and print it clock = '%02d%02d' % (hour,minute) # concat hour + minute, add leading zeros display.print(clock) # Toggle colon when displaying time if second % 2: display.print(':') # Enable colon every other second else: display.print(';') # Turn off colon time.sleep(0.5)
We can easily copy this code onto our Pi's home directory using the 'wget' command and then run it using the following commands.
cd wget https://raw.githubusercontent.com/adafruit/Adafruit_Learning_System_Guides/master/Matrix_7-Segment_LED_Backpack_Raspberry_Pi/sevensegment_clock.py python3 ./sevensegment_clock.py
You should see the time appear on your 7-segment display in 24-hour format with a slow blinking colon. It should look like this:
Page last edited January 22, 2025
Text editor powered by tinymce.