Using a Four-Digit LED Display
You want to display a four-digit number in an old-fashioned, seven-segment LED display.
Use an I2C LED module, such as the model shown in Figure 13-1 attached via a breadboard to a Raspberry Pi.
To make this recipe, you need:
• Breadboard and jumper wires (see “Prototyping Equipment” on page 380)
• Adafruit 4 × 7-segment LED with I2C backpack (see “Modules” on page 381)
Figure 13-2 shows the arrangement of components on the breadboard.
For this recipe to work, you will also need to set up your Raspberry Pi for I2C, so follow Recipe 8.4 first.
The display has an accompanying Python library written by Adafruit. It isn’t installed as a proper library, so to use it, you first need to download the folder structure. If you do not have Git installed, install it now with the following command (see Recipe 3.19).
$ sudo apt-get install git |
Now, you can download the folder structure from GitHub:
$ git clone https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code.git |
Change directory into the Adafruit code using:
$ cd Adafruit-Raspberry-Pi-Python-Code $ cd Adafruit_LEDBackpack |
In this folder, you will find a test program that will display the time. Run it using the command:
$ sudo python ex_7segment_clock.py |
Discussion
If you open the example file ex_7segment_clock.py in nano, you’ll see that the key commands are:
from Adafruit_7Segment import SevenSegment |
which import the library code into your program. You then need to create a instance of SevenSegment using the next line of code. The address supplied as an argument is the I2C address (see Recipe 8.4).
Every I2C slave device has an address number. The LED board has three pairs of solder pads on the back that can be bridged with solder if you want to change the address. This is essential if you need to operate more than one of the displays from a single Raspberry Pi.
segment = SevenSegment(address=0x70) |
To actually set the contents of a particular digit, use a line like this one:
segment.writeDigit(0, int(hour / 10)) |
The first argument (0) is the digit position. Note that these positions are 0, 1, 3, and 4. Position 2 is reserved for the two dots in the center of the display.
The second argument is the number to display.
See Also
You can find out more about the Adafruit library at http://bit.ly/HQBE6W.
Table A-3. Prototyping equipment | |
Description | Suppliers |
M-M jumper wires | SparkFun: PRT-08431, Adafruit: 759 |
M-F jumper wires | SparkFun: PRT-09140, Adafruit: 825 |
F-F jumper wires | SparkFun: PRT-08430, Adafruit: 794 |
Half-sized breadboard | SparkFun: PRT-09567 Adafruit: 64 |
Pi Cobbler | Adafruit: 1105 |
Table A-8. Modules | |
Raspberry Pi camera module | Adafruit: 1367, MCM: 28-17733, CPC: SC13023 |
Arduino Uno | SparkFun: DEV-11021, Adafruit: 50, CPC: A000066 |
Level converter, four-way | SparkFun: BOB-11978, Adafruit: 757 |
Level converter eight-way | Adafruit: 395 |
LiPo boost converter/charger | SparkFun: PRT-11231 |
PowerSwitch tail | Adafruit: 268 |
16-channel servo controller | Adafruit: 815 |
Motor driver 1A dual | SparkFun: ROB-09457 |
RaspiRobot board | Sparkfun: KIT-11561, raspirobot.com |
PiFace digital interface board | MCM: 83-14472, CPC: SC12827 |
Humble Pi | MCM: 83-14637, CPC: SC12871 |
Pi Plate | Adafruit: 801 |
Gertboard | MCM: 83-14460, CPC: SC12828 |
Breakout board with paddle terminals | MCM: 83-14876, CPC: SC12885 |
PIR motion detector | Adafruit: 189 |
Venus GPS module | SparkFun: GPS-11058 |
Methane sensor | SparkFun: SEN-09404 |
Gas sensor breakout board | SparkFun: BOB-08891 |
ADXL335 triple-axis accelerometer | Adafruit: 163 |
4×7-segment LED with I2C backpack | Adafruit: 878 |
Bicolor LED square-pixel matrix with I2C backpack | Adafruit: 902 |
PiLite interface board | Ciseco, CPC: SC13018 |
aLaMode interface board | Makershed: MKWY1, Seeedstudio: ARD10251P |
Freetronics Arduino LCD shield | www.freetronics.com |
RTC module | Adafruit: 264 |
16 x 2 HD44780 compatible LCD module | SparkFun: LCD-00255, Adafruit: 181 |