Enabling The SPI Interface On The Raspberry Pi

4

The Raspberry Pi has a Serial Peripheral Interface (SPI) 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.

There is more technical information about SPI on the SPI Wikipedia page if you are interested.

The default Raspbian image disables SPI by default so before you can use it the interface must be enabled. This can be done uses either of two methods. I’ll describe both methods but the first one is probably easier and quicker.

Method 1 – Enabling Using “Raspberry Pi Configuration”

If your Pi boots to the desktop you can either “Shutdown” and use Method 2 or use the graphical tool “Raspberry Pi Configuration”. This is found under Menu > Preferences > Raspberry Pi Configuration

raspiconfig_gui_01

Then you simply need to select the “Interfaces” tab and set SPI to “Enabled” :

Raspberry Pi Configuration

When prompted select “Yes” to reboot so that the changes take effect.

Method 2 – Enabling Using “Raspi-config”

From the command line start by running the following command :

sudo raspi-config

This will launch the raspi-config utility. Select “Advanced Options”.

Enable SPI Using Raspi-config

Select the “SPI” option.

Enable SPI Using Raspi-config

Set the option to “Yes”.

Enable SPI Using Raspi-config

Select “OK”.

Enable SPI Using Raspi-config

Select “Finish”.

Enable SPI Using Raspi-config

When prompted select “Yes” to reboot so that the changes take effect.

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 :

dtparam=spi=on

Use CTRL-X, then Y, then RETURN to save the file and exit. Reboot using the following :

sudo reboot

Checking If SPI Is Enabled (Optional)

To check if the SPI module is loaded by the system run the following command :

lsmod

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.

Python SPI Wrapper

In order to read data from the SPI bus in Python we need a library. This library is included in the latest Raspbian image so if you’ve got an uptodate SD card you don’t need to do anything else. You can confirm you’ve got “python-spidev” and “python3-spidev” installed by using :

apt-mark showauto | grep spi

You should see the two python spidev packages listed in the output.

Obsolete Installation Step

Previously you needed to install a library called ‘py-spidev’. I’ve left the instructions here for anyone who is using an older image.

To install it we first need to install ‘python-dev’ :

sudo apt-get install python2.7-dev

Then to finish we can download ‘py-spidev’ and compile it ready for use :

wget https://github.com/Gadgetoid/py-spidev/archive/master.zip
unzip master.zip
rm master.zip
cd py-spidev-master
sudo python 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).

Share.

4 Comments

  1. Hi ; had my spi working before , recently updated my firmware and I now get error: spi_bcm2708 not found . I have followed your steps to getting spidev. It still isn’t listed on lsmod. There is spi_bcm2835 and i2c_bcm2708 . Any suggestions on my next step ?

  2. I’m having the same problem on all my raspis.
    I can’t make it work.
    Activation via raspi-config fails because it can’t load the driver.
    gpio load spi fails with the same message.
    Did something change in raspian?

    • I tested this with the 2015-09-24 version of Raspbian when I setup my Temperature Logger MkII. It is always worth downloading the latest image and writing a fresh SD card.

  3. It worked for me only with the last method. Thanks a lot. I spend a whole day trying to figure out how to solve this error. Thank you

Leave A Reply