Learning Goals 5 min
I²C is two wires for many devices; SPI is four wires for many devices — slightly more wiring, much higher speed. SPI runs at MHz where I²C runs at hundreds of kHz, so it's the protocol of choice for screens that need fast refresh, SD cards, and the radio modules you'll meet in L04. By the end of this lesson you will:
- Name the four SPI wires (SCK, MOSI, MISO, SS / CS) and explain what each carries.
- Identify the UNO's hardware SPI pins (D13, D11, D12, D10) and the ICSP header that mirrors them.
- Explain why each peripheral on a shared SPI bus has its own SS / CS pin — the addressing mechanism, in contrast to I²C's 7-bit address byte.
Warm-Up 10 min
On your UNO, locate D11, D12, D13 on the digital header — these are the UNO's hardware SPI pins. There's also a 6-pin ICSP header near the top of the board that exposes the same three signals plus power.
Quick question
If SPI uses 4 wires and I²C uses 2, SPI must be worse, right?
Reveal
Trade-off. SPI's 4 wires let it be full-duplex (send and receive simultaneously) and much faster (up to ~25% of the CPU clock — about 8 MHz on a UNO, 10–80 MHz on faster chips). The cost: each peripheral on the bus needs its own dedicated chip-select wire, so an N-device SPI bus needs (3 + N) wires total, while an N-device I²C bus needs just 2. SPI wins for "a few fast devices"; I²C wins for "many slow devices".
New Concept · Four wires, one bus 25 min
The four signals
| Signal | Old name | Direction | Role | UNO pin |
|---|---|---|---|---|
| SCK (or SCLK) | Same | Controller → Peripheral | Clock — pulses generated by the controller | D13 |
| COPI (Controller Out, Peripheral In) | MOSI (Master Out, Slave In) | Controller → Peripheral | Data from controller to peripheral | D11 |
| CIPO (Controller In, Peripheral Out) | MISO (Master In, Slave Out) | Peripheral → Controller | Data from peripheral to controller | D12 |
| CS (Chip Select), aka SS (Slave Select) | SS | Controller → Peripheral | Active when low — selects which peripheral is "listening" | D10 (default; any pin works) |
The Arduino IDE and many libraries still use the older MOSI / MISO / SS names. The newer COPI / CIPO terminology is gradually replacing them — same wires, neutral language.
Pin reservations
D11, D12, D13 are fixed on the UNO — the hardware SPI peripheral is wired to these pins and you can't move it. (You could bit-bang SPI on any pins like SoftwareSerial, but the hardware version is much faster.) D10 is the default SS pin, but in practice each peripheral takes its own dedicated pin and the "official" D10 is just one option.
How addressing works (CS pins, not address bytes)
I²C: every device hears every byte; only the one whose address matches in the first byte responds. SPI: each device has its own CS wire from the controller. To talk to peripheral A, pull A's CS LOW; A starts paying attention to SCK and COPI. Pull A's CS HIGH again to end the conversation; A ignores all bus traffic until selected again.
So an SPI bus with 3 peripherals needs 7 wires total: SCK, COPI, CIPO (shared between all) plus 3 separate CS lines from the controller. Two peripherals = 5 wires. One peripheral = 4 wires.
The anatomy of one transaction
- Controller pulls the chosen peripheral's CS LOW.
- Controller pulses SCK 8 times (one byte's worth). On each clock edge:
- Controller drives COPI with the next bit (MSB first).
- Peripheral drives CIPO with its bit at the same time.
- After 8 clocks, both ends have exchanged 8 bits — controller's sent and the peripheral's response are both captured.
- If more bytes are needed, the controller keeps clocking. CS stays LOW.
- Controller raises CS HIGH. Transaction over.
Full-duplex falls out naturally: there's a shift-out and a shift-in happening every clock pulse on different wires.
Speed
SPI on a UNO runs up to 8 MHz (CPU clock / 2). That's 8 million bits per second = 1 MB/s of raw throughput per wire direction — about 80× faster than 100 kHz I²C. For an SD card or a fast OLED, that's the difference between "real-time" and "noticeable lag".
Clock polarity and phase (CPOL / CPHA)
The fine-print SPI gotcha: each chip expects clock edges in a specific way. CPOL = 0 or 1 (idle clock low or high); CPHA = 0 or 1 (sample on first or second edge). The four combinations are called SPI modes 0–3. Most chips use Mode 0 (CPOL=0, CPHA=0). The library handles this for you via SPISettings — you mostly only need to know the right value for your chip from its datasheet.
| Mode | CPOL | CPHA | Typical use |
|---|---|---|---|
| 0 | 0 | 0 | Most chips: SD cards, 74HC595, MAX31855, many sensors |
| 1 | 0 | 1 | Rare |
| 2 | 1 | 0 | Rare |
| 3 | 1 | 1 | Some flash chips, some ADCs |
Bit order
SPI is usually MSB-first (most-significant bit shifted out first). Some specific chips want LSB-first; SPISettings(speed, order, mode) takes the order as an argument so the library can swap.
Worked Example · Trace a 1-byte SPI transaction 20 min
No wiring today. We'll walk through what happens when you write one byte (0xA5 = 0b10100101) to a 74HC595 shift register.
Step 0 — idle state
SCK LOW (CPOL = 0). CS HIGH (chip not selected). COPI / CIPO: undefined / floating.
Step 1 — controller pulls CS LOW
The 74HC595 wakes up and starts listening.
Step 2 — eight clock pulses
For each pulse:
- Just before the rising edge of SCK, the controller puts the next bit on COPI (MSB first).
- On the rising edge, the 74HC595 latches that bit into its internal shift register.
Sending 0xA5 = bits 1, 0, 1, 0, 0, 1, 0, 1 (MSB to LSB):
| Clock # | SCK | COPI bit |
|---|---|---|
| 1 | HIGH | 1 |
| 2 | HIGH | 0 |
| 3 | HIGH | 1 |
| 4 | HIGH | 0 |
| 5 | HIGH | 0 |
| 6 | HIGH | 1 |
| 7 | HIGH | 0 |
| 8 | HIGH | 1 |
Meanwhile the 74HC595 also drives CIPO — but a 74HC595 doesn't actually have a CIPO output (it's an output-only chip). For chips that do have CIPO (an SD card, an ADC), they'd be shifting back a response byte on the same eight clocks.
Step 3 — controller raises CS HIGH
The 74HC595 sees the falling edge of CS — wait, actually the 74HC595 latches its internal shift register's content into its output pins on a separate "latch" pin (also called RCLK). CS just enables/disables clock input. So in the 74HC595 case there's an extra wire (the latch / RCLK) that you pulse once after the SPI write to actually update the LEDs. We'll see this in the L03-21 wiring tomorrow.
Step 4 — timing
At 8 MHz SPI clock, 8 clock pulses take 1 µs. Plus a few cycles to pull CS LOW and HIGH again. Total: ~2 µs to write one byte. Compare with I²C at 100 kHz: ~90 µs for the same single byte. SPI is faster by orders of magnitude.
Step 5 — what a logic analyser would show
Four traces: SCK (8 sharp pulses), COPI (changing in step with SCK), CS (LOW during the burst, HIGH otherwise), CIPO (whatever the peripheral drove back). Most logic analysers can auto-decode the bytes and print them above the traces.
Try It Yourself · Five paper questions 15 min
You want to put 5 SPI peripherals on one UNO. How many UNO pins do you need to dedicate (counting both shared and per-device wires)?
Reveal
3 shared (SCK, COPI, CIPO) + 5 CS wires = 8 pins. With I²C, you'd use just 2 pins for the same 5 devices — provided no address clashes. SPI's pin count grows with device count; I²C's stays at 2.
To send 1 KB of data: how long does SPI at 8 MHz take? I²C at 400 kHz?
Reveal
SPI: 1024 bytes × 8 bits / 8 Mbit/s = 1 ms. I²C at 400 kHz: 1024 × 9 bits (8 data + 1 ACK) / 400 kbit/s ≈ 23 ms. SPI is ~20× faster for the same payload. The reason SD card libraries use SPI, not I²C.
You're running a 74HC595, an SD card module, and an SSD1306 OLED (in SPI mode, not I²C) all on the same UNO. Which 5 UNO pins do you assign?
Reveal
D11 = COPI, D12 = CIPO, D13 = SCK (all fixed). Then pick three free pins for the CS lines — e.g. D7 for 74HC595 latch, D8 for SD card CS, D9 for OLED CS. Any free digital pins work for CS; just avoid pins used by other peripherals (e.g. the L298N's IN1/IN2 if you're also driving motors).
An 8 MHz SPI bus could in theory write 1 MB/s to an SD card. The Arduino SD library typically achieves 50 kB/s. Where does the time go?
Reveal
The SD card's internal flash erase + write cycle takes milliseconds per block (way longer than the SPI transfer). Plus the FAT filesystem code has to read directory entries, find a free block, update the file allocation table — lots of metadata reads/writes per data write. SPI is fast; the card is slow. To get closer to the raw SPI speed, you'd use raw block I/O (skip the filesystem) — fine for data loggers, awful for "normal files".
You wire a Mode 3 chip thinking it's Mode 0. What symptoms do you see?
Reveal
The data on COPI is sampled at the "wrong" clock edge — typically you read all-zeros or all-ones, or a pattern shifted by one bit (so 0xA5 reads as 0x5A or similar). Sometimes a chip is forgiving and partially works; usually it fails completely. Always check the datasheet for the right mode and pass it via SPISettings.
Mini-Challenge · Pin map a multi-device SPI build 10 min
Plan a UNO with: a 74HC595 (LED array), an SSD1306 in SPI mode (not I²C — many breakouts support both via a solder jumper), and an SD card module. Three peripherals, all on the same SCK/COPI/CIPO with different CS pins.
- Three shared pins: D11 (COPI), D12 (CIPO), D13 (SCK). Mark them on your pin-map.
- Each peripheral's CS: assign D10, D9, D8 (any free pins).
- For the 74HC595, you also need a "latch" pin distinct from CS (or you can wire latch and CS together — many tutorials do).
- Note which other pins are still free for buttons, LEDs, motors, etc.
You'll need this exact plan for the multi-display dashboard in L03-22.
Recap 5 min
SPI is four wires: SCK (clock), COPI (out from controller), CIPO (in to controller), CS (per-peripheral select). Full-duplex by design. Much faster than I²C — 8 MHz on UNO vs 100 kHz / 400 kHz for I²C. Each peripheral has its own CS wire (no address bytes). Four bus "modes" (clock polarity × phase) — usually Mode 0. The UNO's SPI pins are fixed: D11, D12, D13. SPI wins for "a few fast devices" (displays, SD cards, radios); I²C wins for "many slow devices". Tomorrow we wire the simplest possible SPI peripheral — a 74HC595 driving 8 LEDs from 3 pins.
- SPI (Serial Peripheral Interface)
- A four-wire synchronous serial bus designed by Motorola. The default for chip-to-chip fast communication on Arduino-class hardware.
- SCK / SCLK
- Serial Clock — pulses generated by the controller. Every data bit is timed to a clock edge.
- COPI (formerly MOSI)
- Controller Out, Peripheral In — data wire from the controller to the peripheral. Both names still in common use.
- CIPO (formerly MISO)
- Controller In, Peripheral Out — data wire from the peripheral to the controller.
- CS / SS (Chip Select / Slave Select)
- The active-low signal that tells one peripheral "you are being addressed now". Every peripheral has its own dedicated CS wire to the controller.
- Full-duplex
- Both directions of data flow simultaneously, on separate wires (COPI and CIPO). Distinct from half-duplex (one direction at a time on a shared wire).
- SPI Mode (CPOL / CPHA)
- The combination of clock idle polarity (CPOL) and sample edge (CPHA) that the peripheral expects. Mode 0 is most common; check the datasheet.
- SPISettings
- An Arduino library struct that bundles speed + bit order + mode for a single peripheral. Wrap each transaction with
SPI.beginTransaction(settings)/SPI.endTransaction(). - ICSP header
- The 6-pin header near the top of an UNO. Exposes SCK / COPI / CIPO / reset / power — mirrors the digital pins for shields that want SPI access without crossing the main header.
Homework 5 min
- Note the UNO SPI pin numbers in your engineering notebook: D11 COPI, D12 CIPO, D13 SCK. They're fixed — memorise them.
- Bring tomorrow: a 74HC595 IC + 8 LEDs + 8 × 220 Ω resistors + jumper wires. We'll wire and drive the chip in L03-21.
- Optional: look at the "Pinouts" tab on the Arduino SD library documentation page — note which pins it uses on a UNO (hint: D10 for CS by default).
Bring back next class:
- 74HC595, 8 LEDs, 8 × 220 Ω resistors.
- Your pin-map plan from §6.