SPI and Raspbian Linux on a Raspberry Pi

Created 02/10/2015 | Enabling SPI on the Raspberry Pi and installing python py-spidev

In this tutorial we will set up SPI python support on Raspbian linux. SPI can be used with Python versions 2 and 3.  You can download Raspbian from http://www.raspberrypi.org/downloads/

SPI is disabled by default on Raspbian linux.

If you are using Raspian Linux 3.18 or later you need to go into the raspberry pi config utility and enable SPI.

sudo raspi-config

Select 8 Advanced Options and then  A6 SPI - Enable/Disable automatic loading. A prompt will appear asking Would you like the ARM SPI interface to be enabled?, select Yes, then select OK on the next prompt and then Yes, then OK and then select Finish to exit the utility and then you need to reboot your Raspberry Pi.

sudo reboot

For previous releases before Raspian Linux 3.18 you need to complete the following steps:

In a terminal window enter the following command to open raspi-blacklist.conf within the nano text editor.

sudo nano /etc/modprobe.d/raspi-blacklist.conf

Comment out the line blacklist spi-bcm2708 by adding a # to the beginning of the line so it looks like this.

#blacklist spi-bcm2708

Save the file by pressing Ctrl-X and press Y to confirm.

For recent versions of the Raspberry Pi (3.18 kernel or later) you will need to update the /boot/config.txt file.  Open the file with nano using the command:

sudo nano /boot/config.txt

Add the following text to the bottom of the file:

dtparam=spi=on

Save the file by pressing Ctrl-X and press Y to confirm.

Reboot the Raspberry Pi.

sudo reboot

All versions:

Now we have the SPI port enabled we can install the py-spidev python module.  As py-spidev is not available through apt-get we will need to download it from github and install it using python-dev.

Start by updating apt-get to make sure we get the latest software.

sudo apt-get update

Once updated install python-dev.

For Python 2 use:

sudo apt-get install python-dev

For Python 3 use:

sudo apt-get install python3-dev

Now we have python-dev installed we can download py-spidev.  

You can download the latest version of py-spidev and the setup python script for github using git command.

git clone https://github.com/doceme/py-spidev.git

Once downloaded install py-spidev by running setup.py

cd py-spidev

For Python 2 use:

sudo python setup.py install

For Python 3 use:

sudo python3 setup.py install

py-spidev should now be available to use on your Raspberry Pi.

Shop

Support

Click to contact us.
X

Have any Questions?

Send us any questions about this page or product and we will reply within 24 hours.

Submit your Message

X

Have any Questions?

Thank you for contacting us.

We will respond to your enquiry in the next 24 hours.

You can find answers to many questions and additional product support in our Knowledge Base and the Community Forum.