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.
Hardware Setup
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.
Enable SPI
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 :
wget https://bitbucket.org/MattHawkinsUK/rpispy-misc/raw/master/python/7segment.py
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 :
https://vine.co/v/i6L3xtPK0qW
Troubleshooting
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
Buying a Module
These modules are available from various places but try looking here :
8 Comments
I wrote some C code to drive a MAX7219, it’s available for download at
https://github.com/DougieLawson/RaspberryPi/tree/master/max7219
nice work man !!!!!
can you add max7219 7 segment with bmp280 for Raspberry pi 3
Where can I buy this strip?
I got mine from eBay.
This is a great tutorial, got me going in the right direction straight away (after a little “which is pin one again” moment when it didn’t work first time!).
Thanks 🙂
Neat project, got it running pretty quick, Has anyone experienced blinking segments. segments 2 & 3 from the right hand side…..
I am looking for essentially the same thing as this but only smaller, you boys have any ideas or can point me in the right direction?
Actually I’ve just ordered an alternative module that should have smaller displays. When they arrive I’ll do a new blog post. The reason why I wanted smaller digits will become clear. I wonder if we want them for the same reason? 🙂