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

Custom characters

You can create your own character output on a per-segment basis. Each character is simply an 8-bit binary number, or two hexadecimal digits. The table below describes the hexadecimal and binary codes for each segment:

Segment Codes
Segment Hexadecimal Binary
![](ssd_a.png) 0x01 00000001
![](ssd_b.png) 0x02 00000010
![](ssd_c.png) 0x04 00000100
![](ssd_d.png) 0x08 00001000
![](ssd_e.png) 0x10 00010000
![](ssd_f.png) 0x20 00100000
![](ssd_g.png) 0x40 01000000
Decimal point 0x80 10000000

Segment Codes

For example, to generate a capital H:

Calculating codes for a capital H
Character Hexadecimal Binary
![](ssd_bcefg.png) 0x10 (bottom left)
0x20 (top left)
0x40 (centre)
0x02 (top right)
0x04 (bottom right)
0001 0000 (bottom left)
0010 0000 (top left)
0100 0000 (centre)
0000 0010 (top right)
0000 0100 (bottom right)
0111 0110


Let’s connect the seven segment display along with a momentary button, as in the following diagram:

Exercise #1

Write a program that uses the button to increment the count displayed on the seven segment display. The counter will increase if the button is depressed, and remain the same otherwise.

Exercise #2

Now, we are going to write a program inspired by the reaction tester at the Ontario Science Centre. The tester is similar in appearance to the driver’s seat of a vehicle. The simulation starts when the user presses the accelerator pedal. At a random time, a STOP indicator is shown. When the user sees the STOP indicator, they are supposed to hit the brake pedal. The tester then shows you a comparative view of your reaction time.

Write a program that displays ‘go’ to simulate acceleration. It will then delay for a random amount of time (between 2 seconds and 5 seconds in duration), before displaying ‘stop’. To generate a random number and sleep for that amount of time (in seconds):

import time
import random

delay = random.randrange(2000, 5000) / 1000.0

Once stop is displayed, record the time and start polling the button input (GPIO #17). Then the button is pressed, record the time again. Use the following code as a template:

import datetime

startTime =

... do something ...

endTime =

elapsed = endTime - startTime
elapsedSeconds = elapsed.total_seconds()

Display the user’s reaction time using the seven segment display.