SPI (Serial Peripheral Interface), establishes communication between multiple peripheral devices or microcontrollers. The SPI interface bus exchanges data between microcontrollers. It also interfaces small peripherals such as shift registers, sensors, and SD cards in Pi. It uses the different clock and data lines along with a select line to choose to communicate with which device. Learn using SPI in Raspberry Pi.
Generally, there are three lines common to all the devices, MISO (Master In, Slave Out) and MOSI (Master Out, Slave In). For device selection, we use SCK Clock Line. Now, let’s see how SPI Communication in Raspberry Pi works.
Configure of SPI in Raspberry Pi
First of all, we will enable SPI in Pi because SPI is turned off by default in the Raspberry Pi. To enable follow below-given steps.
- Go down and select
5 Interfacing Options
- Arrow down and
Yesin enable SPI option
- Go down and select the
Yeswhen it asks for a reboot
Sample Python code
Finally, it is code time. This is a sample code for using SPI.
Here first, we enabled SPI in code by,
spi = spidev.SpiDev() After that, open a connection to a specific bus and device. Set the SPI Speed in Hz and at last transfer 8-bit value to SPI. So, by using you can transfer or communicate two hardware devices.
For running the python code in Raspberry Pi official link.
Learn basic hardware information about Raspberry Pi 3.
2 thoughts on “Using Serial Peripheral Interface (SPI) in Raspberry Pi”
I wish that you would know better not to make statements like this: “For device selection, we use SCK Clock Line”. SCK Clock line is to manage the data flow. In multi-slave systems the specific Slave Device is selected by Master by activating dedicated CS line.
how to alter this code for multiple sensor connection via spi interface? for example connecting seven same sensors. (sensor detail: ADXL345 accelerometer sensor)