Enable SPI Interface on the Raspberry Pi

7

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 :

sudo raspi-config

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

Raspi-Config - Interfacing

Highlight the “SPI” option and activate “<Select>”.Raspi-Config - Interfacing - SPI

Select and activate “<Yes>” :
Raspi-Config - Interfacing - SPI 1

Highlight and activate “<Ok>” :

Raspi-Config - Interfacing - SPI 2When prompted to reboot highlight and activate “<Yes>” :

Raspi-Config - Reboot Prompt

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

Raspberry Pi Configuration launch from menu

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

Raspberry Pi Configuration - Interfaces Tab - SPI

Click the “OK” button. If prompted to reboot select “Yes” so that the changes will take effect.

Raspberry Pi Configuration - Reboot Prompt

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 :

dtparam=spi=on

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

sudo reboot

Step 2 – 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.

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 python3-spidev python-spidev

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).


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.

Share.

7 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

  4. Matthew Najmon on

    From the Python SPI Wrapper section:
    “sudo apt-get install python3-spidev python-spidev”

    When I tried running that on my pi (immediately after running apt-get update and apt-get upgrade), I got an error:

    The following packages have unmet dependencies:
    python3-spidev : Depends: python3 (>= 3.4~) but 3.2.3-6 is to be installed
    E: Unable to correct problems, you have held broken packages

    What does this mean? Best I can tell, it seems to be dependent on a version of python3 that’s newer than what’s on that repository. Is this something I screwed up? Does the package have an incorrect dependency on the repository? How do I fix this?

    • The default Python 3 on Raspbian at this time is 3.5.3. I would re-write the SD card with a fresh copy of Raspbian as it will be quicker than trying to work out why you’ve got 3.2.3.

Leave A Reply