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.

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:

The I2C bus lines
Line Purpose
SDA Data
SCL Clock

Digits

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

Displaying the hexadecimal digits 0 to F
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

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:

The I2C bus lines
7-segment Pin Purpose GPIO Pin
+ Power 5.0v
- Ground GND
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 class for our display, called SevenSegment. To get this code, along with some test code, download the following package from their GitHub repository:

git clone https://github.com/adafruit/AdaFruit-Raspberry-Pi-Python-Code
cd AdaFruit-Raspberry-Pi-Python-Code/Adafruit_LEDBackpack

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

segment = SevenSegment(address=0x70)

segment.writeDigit(0, 10)
segment.writeDigit(1, 11)
segment.writeDigit(3, 12)
segment.writeDigit(4, 13)

Note: The digit ‘2’ is the colon in our display. You can turn it on or off:

segment = SevenSegment(address=0x70)

segment.setColon(True)
segment.setColon(False)

Exercise

Now, we are going to write a Python program to test our seven segment display. For simplicity, put your code into AdaFruit-Raspberry-Pi-Python-Code/Adafruit_LEDBackpack. Write a program that does the following:

  1. Write a function 'writeDec' which outputs a decimal value [0,9999]
  2. Count from 0000 to 1000, in decimal, with no delay
  3. Write a function 'writeHex' which outputs a hexadecimal value [0,FFFF]
  4. 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 ith 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)