The Raspberry Pi has an SPI (Serial Peripheral Interface) bus which can be enabled on Pins 19,21,23,24 & 26. It is a synchronous serial data link standard and is used for short distance single master communication between devices. As far as the Pi is concerned this is usually relevant to certain sensors and add-on boards.
Step 1 – Enable SPI Interface
The default Raspbian image disables SPI by default so before you can use it the interface must be enabled. This can be done using either of three methods. I’ll describe all methods but the first one is probably easier and quicker.
Method 1 – Using “Raspi-config” on Command Line
From the command line or Terminal window start by running the following command :
This will launch the raspi-config utility. Select “Interfacing Options” :
Highlight the “SPI” option and activate “<Select>”.
Select and activate “<Yes>” :
Highlight and activate “<Ok>” :
When prompted to reboot highlight and activate “<Yes>” :
The Raspberry Pi will reboot and the interface will be enabled.
Method 2 – Using “Raspberry Pi Configuration”
If your Pi boots to the desktop you can either open a Terminal window and use Method 2 or use the graphical tool “Raspberry Pi Configuration”. This is found under Menu > Preferences > Raspberry Pi Configuration
Then you simply need to select the “Interfaces” tab and set SPI to “Enabled” :
Click the “OK” button. If prompted to reboot select “Yes” so that the changes will take effect.
The Raspberry Pi will reboot and the SPI interface will be enabled.
Method 3 – Enabling By Editing A File Manually
This process assumes you are using the latest Raspbian build from the official downloads page. You can update your current image using :
sudo apt-get update sudo apt-get upgrade
To enable hardware SPI on the Pi we need to make a modification to a system file :
sudo nano /boot/config.txt
Add the following line at the bottom :
Use CTRL-X, then Y, then RETURN to save the file and exit. Reboot using the following :
Step 2 – Checking If SPI Is Enabled (Optional)
To check if the SPI module is loaded by the system run the following command :
You should see “spi_bcm2708″ or “spi_bcm2835” listed in the output. You can use the following command to filter the list and make it easier to spot the spi entry :
lsmod | grep spi_
SPI is now enabled.
Step 3 – Install Python SPI Wrapper
In order to read data from the SPI bus in Python we need some additional libraries. These may already be installed but you can check using :
sudo apt-get install -y python-dev python3-dev
sudo apt-get install -y python-spidev python3-spidev
Then to finish we can download ‘py-spidev’ and compile it ready for use :
cd ~ git clone https://github.com/Gadgetoid/py-spidev.git cd py-spidev sudo python setup.py install sudo python3 setup.py install cd ~
You should now be ready to either communicate with add-on boards using their own libraries (e.g. the PiFace) or other SPI devices (e.g. the MCP3008 ADC).
A full list of tutorials featuring this interface can be found by clicking here.
Finally, there is more technical information about SPI on the Wikipedia page if you are interested.