7-Segment Clock

CircuitPython Code

Display the system time on a four digit seven segment display.

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)

Download and Run the Code

We can easily copy this code onto our Pi's home directory using the 'wget' command and then run it using the following commands.

Download: file
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:

This guide was first published on Aug 24, 2012. It was last updated on Aug 24, 2012. This page (7-Segment Clock) was last updated on Sep 18, 2019.