Controlling a seven-segment display from the Raspberry Pi - Part 1

A seven-segment display (SSD), is a form of electronic display device for displaying decimal numerals. They are widely used in digital clocks, electronic meters, and other electronic devices for displaying numerical information.

The seven elements of the display can be lit in different combinations to represent the Arabic numerals. The seven segments are arranged as a rectangle of two vertical segments on each side with one horizontal segment on the top, middle, and bottom. Additionally, the seventh segment bisects the rectangle horizontally. The segments of a 7-segment display are referred to by the letters A to G, where the optional DP decimal point (an eighth segment) is used for the display of non-integer numbers.

Labelled segments of 7-segment display
Labelled segments of 7-segment display — By user:h2g2bob [GFDL, CC-BY-SA-3.0, or CC BY-SA 2.5], from Wikimedia Commons

There are 128 different ouput that can be generated with a 7-segment display:

Possible outputs of a 7-segment display
Possible outputs of a 7-segment display — [Public domain], from Wikimedia Commons

The I2C bus

The HT16K33 7-segment display uses the I2C bus. An I2C bus can support up to 256 different devices (e.g. sensors, motors, outputs) at the same time. Each component has a unique hexadecimal address. The HT16K33 generally uses a 0x70 address, but we’ll verify that later. The I2C bus uses only two GPIO pins:

Line Purpose
SDA Data
SCL Clock
The I2C bus lines

Digits

All the 10 decimal digits (and all 16 hexadecimal digits) can be represented in a 7-segment display:

Digit Input
0 0x3F
1 0x06
2 0x5B
3 0x4F
4 0x66
5 0x6D
6 0x7D
7 0x07
8 0x7F
9 0x6F
A 0x77
B 0x7C
C 0x39
D 0x5E
E 0x79
F 0x71
Displaying the hexadecimal digits 0 to F

Controlling the 4-digit 7-segment display

The four-digit seven-segment display that we are going to use has 4 pins (+, -, D, C). The table below describes each pin, as well as how it should be connected to the Raspberry Pi:

Seven segment display
Seven segment display
7-Segment Pin Purpose GPIO Pin
+ Power 3.3V
- Ground GND
D SDA (Data for I2C) SDA
C SCL (Clock for I2C) SCL
The I2C bus lines

Download the support code

Adafruit, who makes the 7-segment display, has created a Python library for our display, which provides the segments class. The library is preinstalled on the Pi, but you’ll need to download the example code from GitHub.

Run the below code on the bash terminal (Not the IDLE Shell).

git clone https://github.com/adafruit/Adafruit_CircuitPython_HT16K33
cd Adafruit_CircuitPython_HT16K33

Connections

First we are going to connect the four-digit seven-segment display to the GPIO pins as follows:

Testing connections

Let’s run the provided test program, which shows a text and a scrolling text on the 7 segment display, to be sure it is working properly.

python3 examples/ht16k33_segments_7x4customchars.py

To verify that the address of our seven segment display is 0x70, try the following command in the terminal:

i2cdetect -y 1

Test program

Once connected as in the above diagram, you can test if everything was properly connected using the following program. This program displays ‘AbCd’:

# Import all board pins.
import board
import busio

# Import the HT16K33 LED segment module.
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)

# Set the first character to 'A':
display[0] = "A"
# Set the second character to 'B':
display[1] = "B"
# Set the third character to 'C':
display[2] = "C"
# Set the fourth character to 'D':
display[3] = "D"

You can turn the colon (:) on or off:

# Import all board pins.
import board
import busio

# Import the HT16K33 LED segment module.
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)

display.colon = True
display.colon = False

Exercise

Write a program that does the following:

  • Write a function writeDec which outputs a decimal value.
    • Count from 0000 to 1000 in decimal, with no delay
  • Write a function writeHex which outputs a hexadecimal value.
    • Count from 0000 to 1000 in hexadecimal (which is 4096 in decimal), with no delay

Last updated March 14, 2024 Updated and Specified SSD Part 1 (a7d5efd)