Microcontrollers, embedded systems and FPGAs · Part 35 of 36

UART, I²C and SPI

Expert 17 min

How the three classic serial buses work, their wiring and speeds, how to read them with a logic analyser, and common pitfalls.

What you will learn

  • You can wire and configure UART, I²C and SPI.
  • You can decode a frame of each bus from a timing diagram.
  • You can choose a bus for a sensor or display.

Before this: Microcontrollers and firmware

Why serial?

Chips on a board must exchange data with as few wires as possible. Three simple serial buses cover most needs: UART, I²C and SPI.

Two circuit boards connected by coloured wires with glowing pulses travelling along

UART

A UART (universal asynchronous receiver-transmitter) sends bytes over one wire per direction (TX → RX, crossed between devices), plus ground. There's no clock line: both sides agree on the baud rate (e.g. 115,200 bit/s). Each byte is framed:

idle high → start bit (low) → 8 data bits, least significant first → optional parity → stop bit (high)

That's 10 bits per byte with "8N1", so 115,200 baud carries 11,520 bytes/s. Because timing comes from each side's own clock, the two must agree to within a few percent. UART is the classic debug console, GPS and Bluetooth module interface. RS-232 (±12 V levels) and RS-485 (differential, long cables, multiple devices) are electrical standards for the same framing. Never connect RS-232 levels directly to a 3.3 V pin.

I²C

I²C (pronounced "I-squared-C", by Philips, 1982) uses two wires shared by many devices: SDA (data) and SCL (clock), both open-drain with pull-up resistors (typically 2.2–10 kΩ). Devices only pull lines low; the resistors pull them high.

  • Each device has a 7-bit address; the controller sends a start condition, the address plus a read/write bit, then waits for an ACK (the device pulls SDA low).
  • Data bytes follow, most significant bit first, each acknowledged.
  • Speeds: 100 kbit/s (standard), 400 kbit/s (fast), 1 Mbit/s (fast-mode plus).

Ideal for many slow sensors on two wires. Pitfalls: missing or too-weak pull-ups (slow, rounded edges), two devices with the same address, and too much bus capacitance on long wires. NXP's manual calls the roles controller/target since revision 7 (2021).

SPI

SPI uses four signals: SCLK (clock), MOSI/COPI (controller out), MISO/CIPO (controller in) and one CS (chip select, active low) per device. It is full-duplex and simple: on each clock edge one bit goes out and one comes in. No addresses, no acknowledgements, push-pull drivers — so it's fast: tens of MHz are common, used for displays, flash memory, SD cards and fast ADCs.

The catch: four modes (clock polarity CPOL and phase CPHA) — controller and device must use the same one, as listed in the device datasheet. And each extra device needs another CS pin.

A chain of small sensor boards linked by two thin wires to one main board

Comparison

UARTI²CSPI
Wires (+ GND)2 (TX, RX)2 shared3 + 1 CS per device
Clocknone (agreed baud)SCL from controllerSCLK from controller
Devices2 (point to point)many (addresses)many (CS lines)
Typical speed9.6 k–1 Mbit/s0.1–1 Mbit/s1–50+ Mbit/s
Good forconsoles, modulessensors, EEPROMdisplays, flash, ADCs

Debugging with a logic analyser

A cheap USB logic analyser records the lines as 0/1 traces, and protocol decoders (sigrok/PulseView, or Ohmforge's capture tools) turn them into bytes, addresses and ACKs. When "the sensor doesn't answer", the capture shows at once whether the address was wrong, the ACK missing, or the clock too fast — much faster than guessing in firmware.

Try it: Logic gate trainer

Toggle the inputs of a gate, watch the output, and fill in its truth table yourself.

Turn on JavaScript to use this lab. Worked example:

An I²C ACK is a wired-AND: any device pulling SDA low makes the line low. The AND gate's truth table shows the same logic — the output is 1 only when every input is 1 (nobody pulls low).

Key points

  • UART: asynchronous, start bit + 8 data bits LSB first + stop bit; both sides must agree on the baud rate.
  • I²C: two open-drain wires with pull-ups, 7-bit addresses, ACK per byte, MSB first, 100 k–1 Mbit/s.
  • SPI: clock + two data lines + chip select per device, full-duplex, fast; match CPOL/CPHA mode.
  • Choose UART for point-to-point consoles, I²C for many slow sensors, SPI for fast peripherals.
  • Debug with a logic analyser and protocol decoders.

Flashcards

Question: How many bits does a UART send per byte in 8N1?
Answer: 10 (start, 8 data, stop).
Question: Why does I²C need pull-up resistors?
Answer: Its lines are open-drain: devices only pull low.
Question: What selects the target device on SPI?
Answer: Its chip-select (CS) line going low.
Question: Which bit does a UART send first?
Answer: The least significant bit.

Check yourself

Think about your answer, then open it. In the app the quiz is interactive, earns points and brings back what you missed.

  1. How many bytes per second can a 9,600 baud 8N1 UART carry?

    Show the answer

    960 bytes/s

    10 bits per byte → 9,600 / 10 = 960 bytes/s.

    Source: Making Embedded Systems, ch. 7

  2. Match the bus with a feature.

    Show the answer

    UART = No clock line; I²C = Device addresses and ACK bits; SPI = A chip-select line per device

    The defining traits of each bus.

    Source: NXP UM10204 / Harris & Harris

  3. Two I²C sensors have the same fixed address. What happens?

    • They conflict; you need a different address option or a multiplexer
    • The controller detects and renames them automatically
    • SPI mode takes over
    • Nothing — I²C has no addresses
    Show the answer

    They conflict; you need a different address option or a multiplexer

    Addresses must be unique on one bus.

    Source: NXP UM10204, 3.1.12

  4. Which are typical I²C problems?

    • Missing pull-up resistors
    • Too much bus capacitance on long wires
    • Wrong CPOL/CPHA mode
    • Duplicate addresses
    Show the answer

    Missing pull-up resistors; Too much bus capacitance on long wires; Duplicate addresses

    CPOL/CPHA belongs to SPI.

    Source: NXP UM10204

Sources

Facts checked on 2026-11-30. Skillgrove lessons are free learning material — no certificate and no accreditation.