CircuitPython Code

The following code illustrates how to display integers, characters, floating point, hex values and toggle the colon on a 7-segment display.

# SPDX-FileCopyrightText: 2019 Mikey Sklar for Adafruit Industries
#
# SPDX-License-Identifier: MIT

import time
import board
import busio
from adafruit_ht16k33 import segments

# 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 the display.
display.fill(0)

# Can just print a number
display.print(42)
time.sleep(1)

# Set the first character to '1':
display[0] = '1'
# Set the second character to '2':
display[1] = '2'
# Set the third character to 'A':
display[2] = 'A'
# Set the forth character to 'B':
display[3] = 'B'
time.sleep(1)

numbers = [0.0, 1.0, 0.55, 10.23, 100.5]

# print floating point numbers
for i in numbers:
    display.print(str(i))
    time.sleep(0.5)

# print hex values, enable colon
for i in range(0xFF):
    display.fill(0)
    display.print(':')
    display.print(hex(i))
    time.sleep(0.25)

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.

cd
wget https://raw.githubusercontent.com/adafruit/Adafruit_Learning_System_Guides/master/Matrix_7-Segment_LED_Backpack_Raspberry_Pi/sevensegment_test.py
python3 ./sevensegment_test.py

This will briefly scroll through printing  integers, floating point values, some text characters and hex values on a seven segment display.

This guide was first published on Aug 24, 2012. It was last updated on Mar 28, 2024.

This page (7-Segment) was last updated on Mar 28, 2024.

Text editor powered by tinymce.