When connecting peripherals that demand substantial data throughput—such as color TFT displays, SD card modules, or high-speed flash memories—the I2C protocol quickly reaches its bandwidth limits. In these scenarios, developers rely on SPI (Serial Peripheral Interface), renowned as one of the fastest and most robust synchronous serial buses available in embedded electronics.
The Four Lines of the SPI Bus
SPI achieves simultaneous, full-duplex communication using four dedicated hardware lines.
| Line | Identifier | Description and Signal Direction |
|---|---|---|
| MOSI | Master Out Slave In | Transmits data bits from the microcontroller (Master) to the peripheral. |
| MISO | Master In Slave Out | Returns data bits from the peripheral back to the microcontroller. |
| SCK / SCLK | Serial Clock | Synchronizing clock generated solely by the master controller. |
| CS / SS | Chip Select / Slave Select | Dedicated pin to activate a specific peripheral (active LOW). |
Key Advantages of the SPI Architecture
SPI stands out due to its straightforward hardware architecture and massive data transfer rates.
- High Data Rates: Easily clocks at speeds of 10 MHz, 20 MHz, or even exceeding 50 MHz depending on the hardware.
- Full-Duplex Capability: Dedicated send (MOSI) and receive (MISO) lines allow simultaneous two-way transmission.
- Direct Hardware Addressing: Eliminates software address overhead; selecting a target simply requires pulling its dedicated CS line LOW.
Arduino SPI Code Example (SPI Library)
The code below demonstrates how to configure an SPI peripheral and exchange a byte using the native SPI.h library.
#include <SPI.h>
const int chipSelectPin = 10; // Dedicated Chip Select (CS) pin
void setup() {
pinMode(chipSelectPin, OUTPUT);
digitalWrite(chipSelectPin, HIGH); // Keep peripheral deselected at boot
SPI.begin(); // Initialize SCK, MOSI, and MISO lines
}
void loop() {
digitalWrite(chipSelectPin, LOW); // Select and wake target peripheral
byte response = SPI.transfer(0xAA); // Send command byte and capture reply simultaneously
digitalWrite(chipSelectPin, HIGH); // Deselect peripheral
delay(500);
}
When to Prefer SPI over I2C
Although SPI demands more GPIO lines (each peripheral requires an individual CS pin), its speed makes it irreplaceable for heavy bandwidth applications like logging data to SD cards, streaming audio, or driving high-resolution TFT displays.