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.