Using Serial Peripheral Interface (SPI) in Raspberry Pi

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 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.

  1. Run sudo raspi-config
  2. Go down and select 5 Interfacing Options
  3. Arrow down and P4 SPI
  4. Select Yes in enable SPI option
  5. Go down and select the Finish button
  6. Select Yes when it asks for reboot



Sample Python code for SPI in Raspberry Pi

Finally, it is code time. This is 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 python code in Raspberry Pi official link.

Learn basic hardware information about Raspberry Pi 3.

Leave a Reply