Sub topic 1
What SPI Is and Where It Is Used
Prerequisites
This chapter assumes that you know the basic Arduino sketch structure, including setup() and loop(). You should also be comfortable with pinMode(), digitalWrite(), binary and hexadecimal numbers, and simple breadboard wiring.
Serial Peripheral Interface, or SPI, is a synchronous digital communication protocol. A controller uses a clock signal to coordinate data exchange with one or more peripheral devices. An Arduino usually acts as the controller. A sensor, display, memory chip, or converter usually acts as a peripheral.
SPI is useful when a project needs higher speed than a simple, low-pin-count protocol can provide. It has no fixed addressing system. Instead, the controller gives each peripheral its own chip-select wire.
Where SPI Fits
SPI commonly connects an Arduino to devices such as:
- SD card modules
- OLED and TFT displays
- Flash memory chips
- Analog-to-digital converters
- Digital-to-analog converters
- Inertial sensors
- Radio transceivers
- Real-time clocks
SPI usually needs more wires than I2C, but it can transfer data faster and with less protocol overhead. Unlike UART, SPI does not normally connect two devices through separate transmit and receive settings. It provides a shared clock and can support several peripherals on the same bus.
| Protocol | Typical wires | Addressing | Common strength |
|---|---|---|---|
| SPI | 3 shared data/clock wires plus one chip-select wire per peripheral | Chip-select pins | High speed and simple transfers |
| I2C | 2 | Device addresses | Many devices with few wires |
| UART | 2 signal wires | Usually point-to-point | Simple serial links |
The Basic SPI Bus
The shared SPI signals are:
- SCLK, or serial clock, carries timing pulses from the controller.
- MOSI, or controller-out peripheral-in, carries data toward the selected peripheral.
- MISO, or peripheral-out controller-in, carries data back to the controller.
- CS, or chip select, identifies the active peripheral. It is often labeled
SS.
A peripheral normally becomes active when its CS line is driven low. While CS is low, clock pulses move bits between the controller and peripheral. When CS returns high, the peripheral ends that framed exchange.
Only one peripheral should be selected at a time. All selected peripherals would otherwise try to use the shared MISO line simultaneously.
Starting SPI in an Arduino Sketch
The Arduino SPI library controls the board's hardware SPI pins. The exact pin numbers depend on the board, so use its pinout rather than assuming that every board matches an Uno.
#include <SPI.h>
const uint8_t CS_PIN = 10;
void setup() {
pinMode(CS_PIN, OUTPUT);
digitalWrite(CS_PIN, HIGH); // Keep the peripheral deselected.
SPI.begin(); // Enable the hardware SPI peripheral.
}
void loop() {
// A later chapter will add a complete transaction here.
}The controller must configure each chip-select pin as an output and keep it high when the peripheral is idle. A floating CS input can randomly select a device and produce apparently corrupted data.
SPI moves one bit in each direction during every clock cycle. The controller may send a command while receiving status or data at the same time. The next chapters explain the clock timing and byte format that determine exactly how those bits are exchanged.
Exercise
Why does a second SPI peripheral usually need its own chip-select wire?
Show answer
The shared clock and data wires do not identify which peripheral should respond. A separate chip-select wire lets the controller activate exactly one peripheral for each exchange.