Sub topic 15
SPI compared with I2C and UART
This chapter assumes you can already move bytes with the Arduino SPI library and understand chip select, modes and clock rate from the earlier chapters. You should also know what a pull-up resistor is. You will not need to write any I2C or UART code. The goal is to choose among the three protocols for a given part and to recognize the failures that come from picking wrongly.
Three ways to move bytes
All three protocols carry bytes between a microcontroller and another chip, but they make different trade-offs.
- SPI uses a shared clock (SCLK), two data lines (MOSI and MISO) and one chip-select line per peripheral. It is synchronous and full duplex. Every output is push-pull, so edges are fast.
- I2C (Inter-Integrated Circuit) uses two shared lines, SDA for data and SCL for the clock. Every chip on the bus has an address. The lines are open-drain with pull-up resistors, which means a device can only pull a line low and the resistors pull it back high.
- UART (Universal Asynchronous Receiver/Transmitter) uses one line for each direction, TX and RX. There is no shared clock. Both ends must be configured with the same baud rate (bits per second) in advance.
How each protocol delimits data
The protocols differ in how a receiver knows where a message starts and ends.
SPI uses the chip-select line. Chip select going low begins a frame and going high ends it. The clock only runs when the controller drives it, so there is nothing to synchronize beyond the mode. There are no addresses. Selecting a peripheral means driving its own pin.
I2C uses in-band signaling. The controller creates a START condition (SDA falls while SCL is high), sends a 7-bit address plus a read/write bit, and each byte is followed by an acknowledge bit. A peripheral that is present pulls SDA low for that bit. A STOP condition ends the transfer.
UART uses framing bits. In the common 8N1 format, a start bit (low) is followed by 8 data bits, no parity, and a stop bit (high). The receiver samples the line at the agreed baud rate. If the two clocks differ by more than a few percent, bits are sampled in the wrong place and you see garbage characters.
Side-by-side comparison
| Property | SPI | I2C | UART |
|---|---|---|---|
| Signal wires | 3 shared + 1 chip select per peripheral | 2 shared | 2 per link (TX, RX) |
| Clock | Shared, driven by the controller | Shared, can be stretched by a peripheral | None, agreed baud rate |
| Topology | One controller, many peripherals | Many controllers and peripherals | Point to point |
| Selecting a device | Chip-select pin | 7-bit or 10-bit address | Not applicable |
| Duplex | Full | Half | Full |
| Typical speed | 1 to 50 MHz and above | 100 kHz, 400 kHz, 1 MHz | 9600 to 1,000,000 baud |
| Acknowledge | None | Per byte | None (optional flow control) |
| Output type | Push-pull | Open-drain with pull-ups | Push-pull |
Throughput in practice
Raw clock speed overstates the difference, because each protocol adds overhead. Rough numbers for sending data to a single device:
- SPI at 8 MHz moves about 1,000,000 bytes per second. Each byte costs 8 clocks and there is no per-byte overhead on the wire.
- I2C at 400 kHz needs 9 clocks per byte, including the acknowledge bit, and extra clocks for the address byte. Its ceiling is about 44,000 bytes per second, and short transfers are slower than that.
- UART at 115200 baud, 8N1 needs 10 bit times per byte, so it moves about 11,520 bytes per second.
You can measure the SPI figure on your own board. This sketch times a 64-byte transfer with no peripheral attached, which is safe because the chip-select pin is not connected to anything.
#include <SPI.h>
const uint8_t CS_PIN = 10;
uint8_t buffer[64];
void setup() {
Serial.begin(115200);
pinMode(CS_PIN, OUTPUT);
digitalWrite(CS_PIN, HIGH);
SPI.begin();
SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0));
digitalWrite(CS_PIN, LOW);
unsigned long start = micros();
SPI.transfer(buffer, sizeof(buffer));
unsigned long elapsed = micros() - start;
digitalWrite(CS_PIN, HIGH);
SPI.endTransaction();
Serial.print("64 bytes in ");
Serial.print(elapsed);
Serial.println(" us");
}
void loop() {}At 4 MHz the 512 bits need 128 microseconds on the wire. Expect a result a little above that, because the library adds a small cost per byte. On boards where the SPI clock is derived by dividing the CPU clock, the real speed can be lower than requested.
Failure modes that differ by protocol
Each protocol fails in its own way, and the symptom often points to the cause.
- SPI has no acknowledgement. If the peripheral is missing, unpowered or on the wrong chip select, the transfer still completes. You read bytes that look like data. A floating MISO line typically gives all ones, all zeros or random noise. Always check an ID register (a fixed "who am I" value) at startup so you know a device is really there.
- SPI mode mismatches corrupt data without an error. Values come back shifted by one bit or inverted in odd positions.
- I2C reports absence.
Wire.endTransmission()returns a nonzero status when no device acknowledges the address. That makes bus scanning easy, which SPI cannot do. - I2C depends on pull-up values. Resistors that are too large give slow rising edges and fail at higher speeds. Resistors that are too small exceed what the drivers can sink. A stuck peripheral can hold SDA low and lock the bus until it is reset.
- I2C address clashes. Two identical sensors with the same fixed address cannot share a bus unless the part has address pins or you use a multiplexer. On SPI, identical parts only need separate chip-select pins.
- UART mismatches show as garbage text. Wrong baud rate, wrong frame format and mismatched voltage levels all look similar. Both ends also need a common ground. TX connects to RX, not TX to TX.
- UART has no built-in error check beyond parity. A framing error is detected only when the stop bit is not high. Lost bytes happen when software does not read the receive buffer fast enough.
Choosing between them
Start from the part's requirements, because many chips offer only one interface. Where you have a choice, use these criteria.
- Need high throughput (displays, SD cards, flash memory, ADCs sampling quickly): choose SPI.
- Need few pins and have several slow devices (temperature sensors, real-time clocks, small displays): choose I2C. The two-wire cost does not grow with the device count.
- Need a long cable or a link between two boards (GPS modules, serial consoles, Bluetooth modules): choose UART. Its asynchronous nature tolerates separate clocks, and with a line driver such as RS-485 it runs over long distances. SPI and I2C are intended for short runs on one board or a short cable.
- Need multiple controllers on one bus: I2C supports this with arbitration. SPI and UART do not support it without extra logic.
- Need a peripheral to slow the controller down: I2C clock stretching allows it. SPI has no way for a peripheral to pause the clock.
Combining them on one board
Real projects mix protocols. A common layout is a display and an SD card on SPI, a handful of sensors on I2C, and a GPS module on a hardware UART. Pin budget is the usual limit. SPI costs one pin per extra peripheral, while I2C costs none. If you run out of chip-select pins, move slow sensors to I2C and keep SPI for the fast parts.
Exercise
A 128 KB flash chip must be filled in under one second, and the project also has three small temperature sensors. Which protocol would you use for each, and why? Check that your choice meets the flash timing.
Show answer
Use SPI for the flash and I2C for the sensors. 128 KB is about 131,000 bytes. At 400 kHz I2C the ceiling is about 44,000 bytes per second, so it would need about 3 seconds. UART at 115200 baud gives about 11,520 bytes per second and would need over 11 seconds. SPI at 8 MHz gives about 1,000,000 bytes per second and finishes in roughly 0.13 seconds. The three temperature sensors need little bandwidth, so I2C lets them share two wires and avoids using three more chip-select pins.
Exercise
A new SPI sensor always returns 0xFF for every read, but no error appears anywhere. Give two causes that SPI itself cannot report, and one check that would catch them.
Show answer
Possible causes are that the sensor is unpowered or disconnected (a floating or pulled-up MISO reads as all ones), and that the chip-select pin in the sketch does not match the wiring. A wrong SPI mode or a clock rate above the part's limit could also produce this. Read the device's ID register at startup and compare it to the value in the datasheet. If it does not match, check power, wiring, chip select, mode and clock speed in that order.