Using Raspberry Pi SPI in Python

Contents

Intro

We already learnt how to use Raspberry Pi with GPIB interface to communicate with lab instruments. How about using SPI port, to talk with external SPI devices, such as ADC/DAC chips?

Software setup

  1. Comment blacklist spi-bcm2708 to enable SPI in /etc/modprobe.d/raspi-blacklist.conf
  1. Make sure your SPI port is visible in lsmod
root@tin:/etc/modprobe.d# lsmod
Module                  Size  Used by
i2c_dev                 6970  0
spidev                  6533  0
i2c_bcm2708             5306  0
spi_bcm2708             6578  0
  1. Make folder and install python-spi library
root@tin:/home# mkdir python-spi
root@tin:/home# cd python-spi
root@tin:/home/python-spi# wget https://github.com/doceme/py-spidev/archive/master.zip
--2015-11-24 08:24:53--  https://github.com/doceme/py-spidev/archive/master.zip
Resolving github.com (github.com)... 192.30.252.129
Connecting to github.com (github.com)|192.30.252.129|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://codeload.github.com/doceme/py-spidev/zip/master [following]
--2015-11-24 08:24:59--  https://codeload.github.com/doceme/py-spidev/zip/master
Resolving codeload.github.com (codeload.github.com)... 192.30.252.146
Connecting to codeload.github.com (codeload.github.com)|192.30.252.146|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 21177 (21K) [application/zip]
Saving to: `master.zip'
100%[=========================================================>] 21,177      93.9K/s   in 0.2s
  1. Now we can unpack it and install
root@tin:/home/python-spi# unzip master.zip
...
root@tin:/home/python-spi# cd py-spidev-master/
root@tin:/home/python-spi/py-spidev-master# python setup.py install
running install
running build
running build_ext
building 'spidev' extension
....
running install_egg_info
Writing /usr/local/lib/python2.7/dist-packages/spidev-3.0.egg-info

Example Python program

Let’s create simple test python app:

import spidev
import time
spi = spidev.SpiDev()
spi.open(0,0)
while True:
   resp = spi.xfer2([0xDE])
   print resp[0]
   time.sleep(1)

Correct output should be

root@tin:/repo/spitest# python ./spitest.py
SPI Test tool
0
0
0

Projects like this are born from passion and a desire to share how things work. Education is the foundation of a healthy society - especially important in today's volatile world. xDevs began as a personal project notepad in Kherson, Ukraine back in 2008 and has grown with support of passionate readers just like you. There are no (and never will be) any ads, sponsors or shareholders behind xDevs.com, just a commitment to inspire and help learning. If you are in a position to help others like us, please consider supporting xDevs.com’s home-country Ukraine in its defense of freedom to speak, freedom to live in peace and freedom to choose their way. You can use official site to support Ukraine – United24 or Help99. Every cent counts.

Author: Ilya Tsemenko
Created: Nov. 24, 2015, 8:41 a.m.
Modified: Nov. 24, 2015, 8:56 a.m.