[otw_is sidebar=otw-sidebar-1]SPI Raspberry Pi LCD Python,SPI Interface of Raspberry Pi using Pythonin this article we will learn SPI Interface of Raspberry Pi using Python.In the previous article I have discussed about the enabling of the Serial Peripheral Interface (SPI) port in the Raspberry Pi because it is disabled by default in the Raspberry Pi. We have learned what the SPI port is and how it enables the serial communication between the Raspberry Pi and the SPI peripheral ICs. There are a number of Integrated Circuits that use only the SPI interface for communication between the master device and other integrated circuits.



SPI Interface of Raspberry Pi using Python

In this tutorial I will discuss the communication between the Raspberry PI and the SPI peripheral IC using the Python language. Raspbian which is the standard and recommended operating system for the Raspberry Pi has already installed Python shell using we can write and run the Code in Python language. So sit back keep reading and enjoy learning.

[otw_is sidebar=otw-sidebar-2]

Raspberry Pi SPI and the Python library:

Python is a very popular and flexible platform for writing the code in the Raspberry Pi. Although almost all the necessary files and libraries has already been installed in the Raspbian to support the programming in the Python language but still there are some libraries missing. As you have noticed in the I2C communication we need to install the I2C SMBus which supports the code writing in the Python IDLE in the Python language. In the same way Raspbian does not have the Python library to support the code writing in the Python language so we need to install the library for SPI port to enable its programming using the Python IDLE. In the steps to follow I will take you through the procedure to install the SPI library for Python(SPI Interface of Raspberry Pi using Python).

SPI Interface of Raspberry Pi using PythonStep1:

Open the Terminal Window of the Raspberry Pi.


Type the following command in the terminal window:

sudo apt get-update


Now type the following commands in the terminal window:

SPI Interface of Raspberry Pi using Pythonsudo apt get-upgrade

sudo apt get-install python-dev python3-devcd~
git clone https : //github.com/doceme/py-spidev.git
cd py-spidev
sudo make install


Here I am assuming that your Raspberry Pi is already connected to the internet through the Ethernet cable. Note that our SPI library for Python is called “spidev”. Once you entered the above mentioned commands in the terminal your library for Python SPI communication will be installed hopefully. That is all you are now ready to write the code

[otw_is sidebar=otw-sidebar-2]

Raspberry Pi SPI and the Python code:

So by following the above mentioned steps you can install the SPI library for Python. Let us now consider the basic code and circuit for enabling the communication between Raspberry Pi and SPI peripheral ICs via Serial Peripheral Interface (SPI). Before proceeding remember that like the Inter-Integrated circuit (I2C) Serial Peripheral Interface (SPI) communication also follow the master-slave architecture but unlike the I2C communication SPI can has only one master device which in our case is the Raspberry Pi and the number of the slave is limited not by the address register but by the chip select pins that are available on the master device. Although there are a number of devices and sensors available that can communicate serially through the Serial Peripheral Interface (SPI) here for this demo I am using the Pressure sensor to read the values over SPI interface on Raspberry Pi. I am using only one slave device here you can add other salve in your network but make sure you do not run out by the chip select pins on the master device (Raspberry Pi).

SPI Interface of Raspberry Pi using PythonLet us write the code for Raspberry Pi to talk to the slave device.


Open the Python IDLE in the Raspberry Pi.


You can copy and paste the following code in the Python window.

[otw_is sidebar=otw-sidebar-3]

import spidev
import time
spi = spidev.SpiDev()
spi.open(0, 0)
spi.mode = 0b11
while True:
resp = spi.readbytes(3)
if (resp[0] != 255):
value = resp[1] + resp[2]
byte1 = bin(resp[0])[2:].rjust(8,'0')
byte2 = bin(resp[1])[2:].rjust(8,'0')
byte3 = bin(resp[2])[2:].rjust(8,'0')
bits = byte1 + byte2 + byte3
except KeyboardInterrupt:


Notice in the above code that I have imported the spidev library. As described earlier this library is necessary for letting the Raspberry Pi communicate to the slave SPI device over the Python language. After you have write your code save it remember to put the “.py” extension at the end of the name. You can run the file through the Python window or by typing the name of your code in the Raspberry Pi terminal window.


Raspberry Pi SPI circuit:

The circuit configuration for the Raspberry SPI communication is not difficult. The data pins simply are connected to each other no need for external components like we needed for the I2C communication that is the pull up resistors. The simple circuit that I have used is shown below:

[otw_is sidebar=otw-sidebar-3]


SPI Raspberry Pi LCD Python,SPI Interface of Raspberry Pi using PythonThis is just to show that you can simply connect the MISO pin of the Raspberry Pi to the slave device and can communicate serially over the SPI interface.
That is all for now. I have just shown the simple demo of SPI serial communication between the Raspberry PI and the SPI slave device. There are a lot of practical examples where we can employ this type of communication. The importance of Serial Peripheral Interface (SPI) can be understood by the fact that almost all the processors have the SPI port for the communication between SPI peripheral ICs.
I hope this article would be helpful for you. In the next article I will discuss about how to use the General Purpose input/output pins (GPIO) to perform various feats as controlling the leds, motors etc. Till then stay connected. Keep reading and enjoy learning.