Updated: 2 days ago

This article aims to show a simple method of setting up the SPI interface on the Raspberry Pi Computer with Python. The SPI interface is one of the busses made available on the 40 pin GPIO header of the RPi. After the setup has been completed, a test circuit using two TLC549 8 bit A/D converters with 10k potentiometers are used and the output displayed in a python shell using Thonny.

An image of the GPIO header is shown to the right with the necessary connections marked in purple. They include the MOSI, MISO, CLK, CE0, and CE1 pins. In addition, the +3.3v and GND pins will also be needed to power the slave device. I won't be using the MOSI pin as it is not needed. Notice that there is a second channel at the bottom. This article will only use the first channel.

Setup for SPI use is rather simple with the following procedure and assumes the latest install of Raspbian as of 9/4/2020.


On the right is the basic circuit used for this demonstration. I have used two TLC549CP ADC's in the photos and coded to measure both in order to show how it can be done. To do the same as I have, just connect the CLK and Data lines together between the two chips and wire the second CS to CE1 on the RPi.

I'm using an old Version 1 with the 26 pin header, but this will work on all models. I've also added 2 LED's connected to the CE lines to monitor when they are accessed but are optional.


Step 1

Enable the SPI peripheral by opening Preferences - Raspberry Pi Configuration from the desktop. Select the Interfaces Tab and click enable SPI. Click OK, then reboot the system.

Step 2

Open a terminal window enter the following to update the OS and install the SPI Python driver.

sudo apt-get update
sudo apt-get upgrade
git clone
cd py-spidev
sudo make install

If you are using the latest Raspbian then Python 3 will already be installed. If not you can install it using:

sudo apt-get install python-dev python3-dev

Step 3

Now we are ready to write some code. Put this into your favorite text editor. I am using Thonny that comes with Raspbian preinstalled.


import spidev
import time

spi1=spidev.SpiDev(),0) #(channel0,CE0),1) #(channel0,CE1)
while True:
    resp0 = spi0.xfer2([0x00])
    resp1 = spi1.xfer2([0x00])
    print ("{} {:.3f} : {} {:.3f}"

First we create two SPI devices and open each to the correct CE. Next we set the SPI speed which is needed to prevent slow devices from not responding. You will need to determine what that speed is from the datasheet of your device. Here I've used 10000Hz. If speed is not essential, 10kHz is a safe bet.

Then we send 1 byte of data(0x00) to each device and read back the result into resp0 and resp1, print the result, wait 250mS and repeat.

Below is a screenshot of the result after running the program on Thonny. The output has four columns CE0 raw, CE0 voltage : CE1 raw, CE1 voltage.

From this you can see that it is quite simple to access SPI devices using the SpiDev driver.

The driver has two main calls.

spi.xfer([array of bytes])

spi.xfer2([array of bytes])

The difference is that spi.xfer([array of bytes]) will de-asssert and re-assert CE with every byte whereas spi.xfer2([array of bytes]) with keep CE asserted for the full array of bytes.

Hopefully this will give you a quick intro into using the SPI on the Raspberry Pi and get you up and running on your own SPI project!

Like this article? Need help with your next project?

Learn how we can help you, click here.

See what products we have to offer, click here.


Contact us today with your requirements
and let us help you find a solution!

  • Pinterest

©2020 by