Language: EN

como-usar-spi-raspberry-pi

What is SPI and how to use it on Raspberry Pi

  • 4 min

In this tutorial, we will see how to use the SPI protocol alongside a Raspberry Pi to communicate with high-speed external devices.

SPI (Serial Peripheral Interface) is a synchronous serial communication protocol widely used for communication between a microcontroller and peripheral devices, such as sensors, displays, or flash memory.

SPI is known for its high speed and simplicity, making it a popular choice in hardware projects (in contrast, it requires more wires than I2C, for example)

If you want to learn more about the SPI bus, check out,

What is SPI?

SPI is a full-duplex communication protocol, which means that data can be sent and received simultaneously. It uses four main lines for communication:

  • SCLK (Serial Clock): Clock signal generated by the master to synchronize communication.
  • MOSI (Master Out Slave In): Data line from the master to the slave.
  • MISO (Master In Slave Out): Data line from the slave to the master.
  • SS/CS (Slave Select/Chip Select): Line used to select the slave device with which you want to communicate.

Each slave device has its own select line, allowing the master to communicate with only one slave at a time.

Connecting SPI devices

The Raspberry Pi has two available SPI ports: SPI0 and SPI1. The SPI0 port is the most commonly used and is available on the following GPIO pins:

  • GPIO 8 (CE0): Chip Select 0
  • GPIO 7 (CE1): Chip Select 1
  • GPIO 11 (MOSI): Master Out Slave In
  • GPIO 9 (MISO): Master In Slave Out
  • GPIO 10 (SCLK): Serial Clock

If you have multiple slave devices connected to the SPI bus, you need to use the select lines (CS) to choose the device you want to communicate with.

Configuring SPI on Raspberry Pi

The Raspberry Pi has native support for SPI through its GPIO (General Purpose Input/Output). To use SPI, we first need to enable it in the system configuration.

Enabling SPI on Raspberry Pi

Open the terminal on your Raspberry Pi.

Run the following command to open the Raspberry Pi configuration:

sudo raspi-config

Go to Interfacing Options > SPI and select Yes to enable I2C. raspberry-pi-spi

Reboot the Raspberry Pi to apply the changes:

sudo reboot

:::::

After rebooting, you can check if SPI is enabled by running the following command in the terminal:

lsmod | grep spi

If SPI is enabled, you should see something like spi_bcm2835 in the output.

Programming SPI in Python

To interact with SPI devices on the Raspberry Pi, we can use the spidev library, which provides a simple interface for working with SPI in Python.

First, install the spidev library by running the following command in the terminal:

sudo apt-get install python3-spidev

Let’s go with a simple example of how to send and receive data through SPI using Python:

import spidev

# Create an instance of SPI
spi = spidev.SpiDev()

# Open the SPI bus (bus 0, device 0)
spi.open(0, 0)

# Set the clock speed (in Hz)
spi.max_speed_hz = 1000000  # 1 MHz

# Send data to the SPI device
data_sent = [0x01, 0x02, 0x03]  # Data to send
data_received = spi.xfer(data_sent)  # Send and receive data

# Print the received data
print("Received data:", data_received)

# Close the SPI connection
spi.close()

In this example,

  • We open the SPI0 bus and set the clock speed to 1 MHz.
  • Then, we send a list of bytes ([0x01, 0x02, 0x03]) to the SPI device and receive the response.

Practical examples