📡 SPI (Serial Peripheral Interface)
SPI is a synchronous, full-duplex protocol used for short-distance communication, usually between a microcontroller and fast peripherals like SD cards, displays, or high-speed ADCs.
Unlike I2C, SPI does not use device addresses. Instead, it uses a hardware Chip Select (CS) or Slave Select (SS) wire for every device on the bus.
1. Physical Wiring
SPI typically uses four wires:
- SCLK (Serial Clock):Â Generated by the Master to synchronize data.
- MOSI (Master Out Slave In):Â Data sent from the Master to the Slave.
- MISO (Master In Slave Out):Â Data sent from the Slave to the Master.
- CS / SS (Chip Select): The Master pulls this line LOW to “talk” to a specific slave.
2. Full-Duplex Nature
Because there are two independent data lines (MOSI and MISO), SPI can send and receive data at the exact same time. This makes it significantly faster than I2C or UART for data-heavy tasks.
| Parameter | Description |
| Communication | Full-duplex (simultaneous TX/RX). |
| Speed | Very high (typically 1MHz to 100MHz+). |
| Configuration | Single Master / Multiple Slaves. |
| Clock Polarity | CPOL and CPHA settings define when data is sampled (4 modes). |
| Feature | UART | I2C | SPI |
| Wires | 2 | 2 (plus pull-ups) | 3 + N (Slaves) |
| Speed | Slow | Medium | Very High |
| Distance | Medium | Short | Very Short |
| Complexity | Simple | Moderate | Moderate |
When setting up SPI, you must match the Mode of your Master to your Slave.
- CPOL (Polarity):Â Is the clock High or Low when idle?
- CPHA (Phase):Â Is data sampled on the leading or trailing edge of the clock?
- If these don’t match, your data will be shifted by one bit or completely corrupted.
