Raspberry Pi SPI Bus Functionality Explained
The Serial Peripheral Interface (SPI) bus on the Raspberry Pi GPIO header serves as a synchronous, full-duplex serial communication interface used to connect the Pi to high-speed peripheral devices over short distances. This article explores how the SPI bus operates on the Raspberry Pi, its key pin assignments, common use cases, and how to enable it for your hardware projects.
How the SPI Bus Works on Raspberry Pi
SPI operates using a master-slave architecture where the Raspberry Pi acts as the master device and controls the communication clock. Because it is synchronous, it relies on a shared clock signal to synchronize data transmission between devices. Because it is full-duplex, data can be sent and received simultaneously, making it significantly faster than standard I2C or UART connections.
The standard SPI bus on the Raspberry Pi GPIO header requires four primary signals:
- SCLK (Serial Clock): The clock pulses generated by the Raspberry Pi to synchronize data transfer.
- MOSI (Master Out Slave In): The line used by the Raspberry Pi to send data to the connected peripheral.
- MISO (Master In Slave Out): The line used by the peripheral to send data back to the Raspberry Pi.
- CE (Chip Enable / Chip Select): The line used by the Raspberry Pi to initiate communication with a specific peripheral. When multiple devices are on the same bus, the Pi toggles the specific CE line of the device it wants to talk to.
Raspberry Pi GPIO SPI Pinout
By default, the primary SPI bus (SPI0) is exposed on the standard 40-pin Raspberry Pi GPIO header. It includes two hardware Chip Enable pins, allowing you to connect two separate SPI devices directly out of the box.
| Pin Number | GPIO Pin | SPI Function | Description |
|---|---|---|---|
| Pin 19 | GPIO 10 | MOSI | Master Out Slave In |
| Pin 21 | GPIO 9 | MISO | Master In Slave Out |
| Pin 23 | GPIO 11 | SCLK | Serial Clock |
| Pin 24 | GPIO 8 | CE0 | Chip Enable 0 |
| Pin 26 | GPIO 7 | CE1 | Chip Enable 1 |
On newer Raspberry Pi models (such as the Raspberry Pi 4 and 5), additional SPI buses (SPI1 through SPI6) can be mapped to other GPIO pins using device tree overlays if your project requires connecting more than two devices.
Common Use Cases for SPI
Due to its high data throughput capability, SPI is favored for peripherals that require rapid data updates. Common applications on the Raspberry Pi include:
- Small Displays: Driving OLED, TFT, and LCD screens where high refresh rates are necessary for smooth visual rendering.
- Storage Devices: Communicating with external SD card modules or RFID/NFC card readers.
- Sensors and Converters: Reading high-resolution Analog-to-Digital Converters (ADCs) or controlling Digital-to-Analog Converters (DACs) used in audio and scientific instrumentation.
- Network Modules: Interfacing with external Ethernet controllers or wireless transceivers like LoRa or Nordic nRF24L01 modules.
Enabling SPI on the Raspberry Pi
To utilize the SPI bus, the interface must be explicitly enabled in the Raspberry Pi configuration, as it is disabled by default to free up the GPIO pins for general use.
You can enable it by opening the terminal and running
sudo raspi-config, navigating to Interface
Options, selecting SPI, and choosing to enable
the driver. Alternatively, you can add dtparam=spi=on to
the /boot/firmware/config.txt file and reboot the system.
Once enabled, the SPI devices will appear in the Linux directory as
/dev/spidev0.0 and /dev/spidev0.1, ready to be
programmed using languages like Python or C.