Serial Peripheral Interface (SPI) is a synchronous communication protocol used to enable communication between the raspberry pi and slave peripherals. SPI uses a synchronous serial bus, which means that data is sent synchronously with a common clock between master and slaves.
The SPI bus consists of four main lines:
The image above shows two squares: blue and green. The blue square corresponds to the wires you’ll need to connect to talk to your peripheral. The green square corresponds to the two chip select pins that allow you to connect two different components to the SPI. You must connect a component to only one of these two pins. As a reminder, CEO corresponds to SPI spidev0.0 and CE1 to spidev0.1.
You can also use other GPIOs like chip select if you want to use more components on the SPI. In this case, you’ll need to define the pin yourself, following the timeline indicated in the datasheet for the component you wish to control.
The first step is to install the spi-tools library:
We will now activate the spi in the raspi-config menu:
This is the raspi-config menu. Next, click on Interface Options :
In Interface Options, we can see the SPI line, which we will click on:
We are asked to activate the SPI. We click on yes:
We are then presented with a page indicating that the SPI has been activated:
By typing the line ls -l /dev/spi* we can see that both SPIs have been activated:
We’ll see how to send information and read through the SPI.
Here’s an example of SPI reading for the RFID-RC522 component connected to the CE0 select chip. This program is easily adaptable to other components:
Here’s a program to send two numbers 125 and 34 over the SPI to chip select CE0 :
Here’s the program test showing the MOSI and clock on the oscilloscope: