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.
There are 128 different ouput that can be generated with a 7-segment display:
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:
All the 10 decimal digits (and all 16 hexadecimal digits) can be represented in a 7-segment display:
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|
|D||SDA (Data for I2C)||SDA|
|C||SCL (Clock for I2C)||SCL|
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
First we are going to connect the four-digit seven-segment display to the GPIO pins as follows:
Let’s run the provided test program, which shows the current time on the 7 segment display, to be sure it is working properly.
The example program does not support Python 3. The library itself does.
To verify that the address of our seven segment display is
0x70, try the following command in the terminal:
i2cdetect -y 1
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()
Write a program that does the following:
- Write a function
writeDecwhich outputs a decimal value [0,9999]
- Count from 0000 to 1000, in decimal, with no delay
- Write a function
writeHexwhich outputs a hexadecimal value [0,FFFF]
- Count from 0000 to 1000, in hexadecimal, with no delay
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))