Monday 8 December 2014

How to enable SPI on Raspbery PI and enable SPI module for Python


  • Enable SPI on Raspberry PI

  • Enable SPI for use in Python






Enable SPI on Rasperry PI


From the command line type:
sudo nano /etc/modprobe.d/raspi-blacklist.conf

You will then see
2014-12-04-100914_568x125_scrot


Comment out the blacklist spi-bcm2708 by prefixing it with #

2014-12-04-100618_533x137_scrot


 Press and hold 'CTRL' and press 'O',  then press 'ENTER', then close nano with 'CTRL' + 'X'

You will need to reboot the type:
sudo reboot

When it has restarted you can test if it is working by typing 'lsmod', this display kernel modules loaded, you can wee from the image below the spi_bcm2708 module is now loaded,

2014-12-04-101948_405x237_scrot


SPI is now enabled on the Raspberry PI




 Enable SPI for use in Python


To enable SPI to be used in python you need to install the python module for interfacing with SPI.

You will need internet access so you can download the code from github.
git clone https://github.com/doceme/py-spidev

2014-12-04-105003_609x43_scrot


Once you have cloned the py-spidev you will need to change to the directory and use python to install  the module.
cd py-spidev
sudo python setup.py install

2014-12-04-105310_575x147_scrot


It is now possible to use import spidev more information can be found here http://tightdev.net/SpiDev_Doc.pdf
import spidev

1 comment:

  1. Is there a difference between spidev (3.2) and the library 'python-periphery', or the 'py-libbcm2835' library? They all seem to do spi, however not sure if they have different applications or if they are just deprecated libraries?

    Interestingly, the spidev documentation says you can set the number of bits per word(transfer) , 8..16, however according to the pi documentation, the spi peripheral connected to the header only supports 8/9! So perhaps spidev is a generic driver whose functionality may or may not be fully supported by the rpi?

    BTW, doesn't a python list ([0x90,0,0,0,0]) consist of ints and not bytes? Does the driver take this into account and convert it to 8-bit bytes before sending?

    ReplyDelete