Knowledge Base

The AB Electronics UK Knowledge Base provides support solutions, tutorials and troubleshooting guides.

Knowledge Base

SPI and Python on Raspberry Pi OS

Enabling SPI on the Raspberry Pi and installing python py-spidev

This tutorial will set up SPI Python support on Raspberry Pi OS. SPI can be used with Python versions 2 and 3. You can download Raspberry Pi OS from https://www.raspberrypi.org/software/operating-systems/

The SPI bus is disabled by default on Raspberry Pi OS. If you use Raspberry Pi OS (Raspian Linux) 3.18 or later, you must go into the Raspberry Pi config utility and enable the SPI kernel module.

sudo raspi-config

Select 3 Interface Options and then  I3 SPI - Enable/disable automatic loading of SPI kernel module. A prompt will appear asking Would you like the SPI interface to be enabled? Select Yes, select OK on the following prompt, and then select Finish to exit the utility; 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 if it does not already exist:

dtparam=spi=on

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

Reboot the Raspberry Pi.

sudo reboot

Installing the py-spidev Python module

Now that the SPI port is enabled, we can install the py-spidev python module. As py-spidev is unavailable through apt-get, we must 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 python-setuptools git

For Python 3, use:

sudo apt-get install python3-dev python3-setuptools git

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 the git command.

git clone https://github.com/abelectronicsuk/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.


Was this article helpful? Yes No

Please tell us how we can improve this article:

Submit
Created 02/10/2015 | Last Updated: 10/11/2023

Related Expansion Boards

Related Articles

Raspberry Pi GPIO Pin Usage
Raspberry Pi GPIO Pin mapping and connections
PCB Header Assembly Jig
Using the PCB Header Assembly Jig
Samba Setup on Raspberry Pi
How to install and setup Samba on the Raspberry Pi
Set a static IP Address on Raspberry Pi OS Bookworm
How to use a static network IP Address on the Raspberry Pi
Set a static IP Address on Raspberry Pi OS Buster
How to use a static network IP Address on the Raspberry Pi
Set a static IP Address on Raspberry Pi OS Wheezy
Static Network IP Address Setup on the Raspberry Pi
I2C Part 1 - Introducing I2C
An introduction to the I²C protocol
I2C Part 2 - Enabling I2C on the Raspberry Pi
How to enable I2C on the Raspberry Pi
I2C Part 3 - I2C tools in Linux
An introduction to I²C tools in Linux
I2C Part 4 - Programming I2C with Python
How to get started with Programming I²C with Python
SPI and Python on Raspberry Pi OS
Enabling SPI on the Raspberry Pi and installing python py-spidev
Using Pythonpath with the Python Libraries
Adding the library path to startup
Connecting Expansion Boards to the Raspberry Pi 400
Expanding the Potential of Your Raspberry Pi 400