Skip to content
Tutorials Center
My learning

Topics

Lesson 27 of 37 ยท Serial and Communication

The SPI Bus

Understand SPI on Arduino: the four wires, clock modes, chip select, the SPI library and transactions, shift registers, and when to choose SPI over I2C.

SPI (Serial Peripheral Interface) is the fast, simple bus used by SD card modules, many displays, radio modules, ADCs and shift registers. Where I2C uses addresses on shared wires, SPI uses a dedicated chip select line per device and clocks data in and out at the same time. The cost is more wires; the benefit is speed (many megabits per second) and simplicity.

No SPI hardware runs on this site. The Arduino sketches are read-only, and the C programs are labelled stand-ins that simulate the clocked bit exchange between a controller and a peripheral.

The four signals

Signal Direction Meaning
SCK controller to peripheral the clock
MOSI controller to peripheral Master Out, Slave In: data to the device
MISO peripheral to controller Master In, Slave Out: data from the device
CS / SS controller to peripheral chip select: low selects that device

On an Uno the hardware SPI pins are 11 (MOSI), 12 (MISO), 13 (SCK); pin 10 is the usual chip select. Each extra device shares SCK, MOSI and MISO and gets its own CS pin (any free digital pin). Only the device whose CS is low listens and drives MISO; all others keep quiet.

flowchart LR
  A["Arduino"] -- "SCK, MOSI, MISO" --> B["SD card"]
  A -- "SCK, MOSI, MISO" --> C["Display"]
  A -- "CS1" --> B
  A -- "CS2" --> C

Shared data lines, separate chip selects.

A full-duplex exchange

SPI transfers bits both ways during the same clock pulses. Think of two shift registers joined in a ring: on each clock tick the controller shifts one bit out on MOSI while the peripheral shifts one bit out on MISO. After 8 ticks the two have swapped bytes. That is why SPI.transfer(byte) both sends a byte and returns the byte received. To read from a device you often send a dummy byte (0x00) to generate clock pulses.

C Stand-in: software SPI exchange of one byte (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

SPI settings: speed, bit order and mode

Devices differ in three settings, listed in their datasheets:

  • Clock speed: the maximum the device supports (SD cards, displays: many MHz). The Uno's SPI clock can be up to 8 MHz.
  • Bit order: most-significant bit first (MSBFIRST) is the common one.
  • Mode (0 to 3): combines the clock polarity (CPOL, whether SCK idles low or high) and phase (CPHA, whether data is sampled on the first or second clock edge). Mode 0 (idle low, sample on rising edge) is the most common.
Mode CPOL CPHA
0 0 0
1 0 1
2 1 0
3 1 1

A device in the wrong mode returns shifted or garbage bytes, a typical symptom of a datasheet mismatch.

The SPI library

C++
#include <SPI.h>

const int CS_PIN = 10;

void setup() {
  pinMode(CS_PIN, OUTPUT);
  digitalWrite(CS_PIN, HIGH);        // deselected
  SPI.begin();
}

byte readRegister(byte reg) {
  SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));
  digitalWrite(CS_PIN, LOW);         // select the device
  SPI.transfer(reg | 0x80);          // many chips: bit 7 set means "read"
  byte value = SPI.transfer(0x00);   // dummy byte clocks the answer in
  digitalWrite(CS_PIN, HIGH);        // deselect
  SPI.endTransaction();
  return value;
}

void loop() { }

SPISettings collects speed, order and mode; beginTransaction() applies them (and protects against interrupts from other code using SPI). Pull CS high again when done. A forgotten digitalWrite(CS_PIN, HIGH) after the transaction leaves the device selected and breaks the next one on the bus.

Shift registers: SPI in miniature

The 74HC595 shift register turns three pins into eight outputs, ideal for LED rows. You clock a byte in; a latch pin copies it to the outputs. Arduino has a helper, shiftOut(), that does the bit-banging for you; you can also use hardware SPI.

C++
const int LATCH = 8;     // ST_CP, with CS role
const int CLOCK = 12;    // SH_CP
const int DATA = 11;     // DS

void setup() {
  pinMode(LATCH, OUTPUT);
  pinMode(CLOCK, OUTPUT);
  pinMode(DATA, OUTPUT);
}

void showPattern(byte pattern) {
  digitalWrite(LATCH, LOW);
  shiftOut(DATA, CLOCK, MSBFIRST, pattern);
  digitalWrite(LATCH, HIGH);         // outputs update all at once
}

void loop() {
  for (int i = 0; i < 8; i++) {
    showPattern(1 << i);             // one LED running along the row
    delay(100);
  }
}
C Stand-in: shift register patterns (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

SPI or I2C?

I2C SPI
Wires 2 (plus power) 4, plus one CS per device
Speed 100 to 400 kHz (up to a few MHz) typically 1 to 20 MHz or more
Addressing built in (7-bit) by chip select
Best for many slow sensors, small displays SD cards, fast displays, radios, memory

Use I2C when pins are scarce and data is small; use SPI for bulk data. Many projects use both: a display and sensors on I2C, an SD card on SPI. For logging to an SD card, see the data logger project.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which SPI line does the controller use to select one particular device?

    Each peripheral has its own CS line; a low level tells that device to listen and respond.
  2. 2. What does SPI.transfer(0x00) return?

    SPI is full duplex: sending a byte clocks in a byte at the same time, and transfer() returns it.
  3. 3. A device returns shifted garbage. What setting might be wrong?

    Devices expect a particular clock polarity and phase. A wrong mode samples bits at the wrong time.
  4. 4. Why leave a device's CS pin high after a transaction?

    A selected device may drive MISO and interpret clock pulses. Deselecting it keeps the shared bus free.

Summary

  • SPI uses SCK, MOSI, MISO and one chip select per device; data flows both ways at once.
  • Match the device's speed, bit order and mode (0 to 3) using SPISettings.
  • Wrap access in beginTransaction and endTransaction, and always release CS.
  • Choose SPI for speed, I2C for simplicity and few pins; shift registers expand outputs cheaply.

Next: Installing and Managing Libraries