Almost every sensor, display and module you connect to a microcontroller talks over one of three serial connections: I²C, SPI or UART. They all send data one bit at a time, but they differ in wiring, speed and how many devices can share them.
At a glance
| I²C | SPI | UART | |
|---|---|---|---|
| Wires (plus ground) | 2: SDA, SCL | 4: SCK, MOSI, MISO, CS | 2: TX, RX |
| Devices | Many on one bus, chosen by address | Many, each with its own CS wire | Usually two, one at each end |
| Typical speed | 100 or 400 kbit/s | Several Mbit/s or more | 9,600 to 115,200 baud is common |
| Good for | Sensors, small displays, many devices | Displays, SD cards, fast data | GPS, Bluetooth modules, PC links |
I²C
I²C (often written I2C) uses two shared wires: SDA for data and SCL for the clock. Every device has an address, usually 7 bits, and the controller starts each message by sending the address of the device it wants. Only that device replies.
- Both lines need pull-up resistors to the supply voltage, because devices only ever pull the lines low. Many sensor boards include them.
- Two devices with the same fixed address can't share a bus. Many chips let you change the address with a pin, or you can use an I²C multiplexer.
- I²C is designed for short distances on a board. Long wires add capacitance, which slows the edges and causes errors.
SPI
SPI uses a clock (SCK), a line from controller to device (MOSI, also called COPI or SDO), a line back (MISO, also called CIPO or SDI), and a chip select (CS) line for each device. The controller pulls a device's CS low to talk to it. Data flows both ways at once, one bit per clock pulse.
- SPI is fast and simple, but each extra device needs another CS pin.
- Devices use one of four "modes", which set the clock's idle level and which clock edge reads the data. Check the datasheet and set the same mode in your code.
UART
A UART sends bytes with no clock wire at all. Both ends agree on a speed (the baud rate) in advance, and each byte is framed by a start bit and a stop bit. Connect one side's TX to the other side's RX, and vice versa, and share a ground.
- The usual format, "8N1", means 8 data bits, no parity bit and 1 stop bit, so each byte takes 10 bits on the wire.
- At 115,200 baud that's at most 11,520 bytes per second.
- USB-to-serial adapters let a computer talk to a UART, which is how most boards print debug messages.
Voltage levels
All three buses assume both ends use the same logic voltage. Connecting a 5 V device to a 3.3 V microcontroller can damage the 3.3 V side. Use a level shifter, or choose 3.3 V parts. Note that "RS-232" serial ports on older equipment use much higher voltages, and must never be connected directly to a microcontroller.
Going deeper
I²C speeds and addressing in detail
The I²C specification defines Standard mode (100 kbit/s), Fast mode (400 kbit/s), Fast-mode Plus (1 Mbit/s) and High-speed mode (3.4 Mbit/s). Every device on a bus must support the speed you choose. Of the 128 possible 7-bit addresses, a handful are reserved, and a 10-bit addressing scheme exists for larger systems, though it's rarely used by hobby parts.
Choosing I²C pull-ups
The pull-up resistor and the bus capacitance (wires plus every device's pins) set how quickly a line rises. Too large a resistor gives slow rises that break at 400 kbit/s, and too small a resistor needs more current than devices can sink. The specification limits Standard and Fast mode buses to 400 pF. Typical values are 4.7 kΩ at 100 kbit/s and 2.2 kΩ at 400 kbit/s, and remember that several boards' built-in pull-ups combine in parallel.
Clock stretching and stuck buses
An I²C device can hold SCL low to make the controller wait, called clock stretching. Some controllers handle this badly. If a device is reset mid-transfer it may hold SDA low, locking the bus. A common recovery is to toggle SCL about nine times until SDA is released, then send a stop condition.
SPI modes
The mode is set by two bits. CPOL is the clock's idle level (0 is low, 1 is high). CPHA selects whether data is sampled on the first or second clock edge. Mode 0 (CPOL 0, CPHA 0) is the most common. A wrong mode often gives data that's almost right, shifted by one bit, which is a useful clue.
UART timing tolerance
Because there's no clock wire, both ends must run at nearly the same speed. The receiver resynchronises at each start bit, so small errors only accumulate across one frame. A combined mismatch of more than a few percent between the two ends causes corrupted bytes. Crystal-controlled clocks are accurate enough, while some microcontrollers' internal oscillators drift with temperature.
Debugging with a logic analyser
Inexpensive USB logic analysers, used with free software such as sigrok PulseView, record the actual signals on the wires and decode I²C, SPI and UART into readable bytes. Seeing whether a device acknowledged its address, or whether the bytes on the wire match what your code meant to send, turns hours of guessing into minutes.
Sources
The main references behind this guide. Numbers we worked out ourselves, such as the code outputs, come from running the code.