7 segment displays are a well established way of electronically displaying numbers and a small set of letters. They’ve been around for as long as I’ve been playing with electronics but were a pain to wire up given the number of LEDs involved.
Luckily things are a lot easier now that they are available on pre-made modules that uses an SPI interface. This is easy to setup and use on the Raspberry so for about £3 you can add a strip of 8 7 segment digits.
These modules tend to all use the Maxim MAX7219. This IC does all the hard work and we can communicate with it using SPI and a few lines of Python.
They have pins at both ends because you can daisy-chain multiple modules.
The module has five pins on the left hand side. These can be connected directly to the Pi’s GPIO header with some female to female jumper cables.
|Module||Pi GPIO Header|
|Vcc||Pin 17 (3V3)|
|GND||Pin 20 (GND)|
|DIN||Pin 19 (GPIO10)|
|CS||Pin 24 (GPIO8)|
|CLK||Pin 23 (GPIO11)|
Other pins on the GPIO header can be used for 3V3 and GND. The pins suggested above are close to the required SPI pins which made it easier for me to attach my jumper cables.
I used the latest version of Raspbian but you might want to update your SD card using :
sudo apt-get update sudo apt-get upgrade
The module uses the SPI interface so we need to set that up first. It isn’t enabled by default so follow my Enabling SPI on the Raspberry Pi guide.
If you’ve enabled SPI correctly you should see the “spi_bcm####” module listed when you run :
lsmod | grep spi
MAX7219 Library Setup
To make use of the MAX7219 device we need to install a library by Richard Hull. This is easy to do using the following commands :
git clone https://github.com/rm-hull/max7219.git cd max7219 sudo python setup.py install
Assuming you have rebooted since enabling SPI and connecting your hardware you should be ready to run an example. There is an example script in the examples folder but this script assumes you’ve got three modules daisy-chained together. For a single module I created a simplified script. You can download it directly to your Pi using :
This script can be run using :
sudo python 7segment.py
If you are using the latest version of Raspbian you may be able to run the script without the “sudo” on the front.
With any luck your display should be lit up and showing random numbers something like this :
If you are having issues here are some tips :
- Check your wiring. Make sure the connections to the Pi are correct
- Check the 7 segment modules are firmly attached to the module’s PCB
- Use “lsmod | grep spi” to check the SPI module is loaded
- Did you remember to install the MAX7219 library?
- Reboot to make sure all the changes are active