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:

7-Segment Pin Purpose GPIO Pin
+ Power 5.0V
- 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 SevenSegment class. The library is preinstalled on the Pi, but you’ll need to download the example code from GitHub.

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

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 the current time on the 7 segment display, to be sure it is working properly.

sudo python examples/ex_7segment_clock.py
cd examples
sudo python ex_7segment_clock.py

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

sudo 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’:

from Adafruit_LED_Backpack.SevenSegment import SevenSegment

segment = SevenSegment()

segment.set_digit(0, 'A')
segment.set_digit(1, 'B')
segment.set_digit(2, 'C')
segment.set_digit(3, 'D')

# The display won't update without this line
segment.write_display()

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

from Adafruit_LED_Backpack.SevenSegment import SevenSegment

segment = SevenSegment()

segment.set_colon(True)
segment.set_colon(False)

segment.write_display()

Exercise

Write a program that does the following:

Hint: You can use // and % to extract a single digit from a number. The following code demonstrates how to retrieve the $i$-th number (counted from 0, starting on the right) of the number in num:

def get_digit(i, num):
    return (num / 10 ** i) % 10

num = 12345
for i in range(0, 5):
    print("get_digit(", i, ", 12345) =", get_digit(i, num))