RobotGuide

Hardware & electrical · topic E16 · 30 hours

On-board trio: UART / I2C / SPI

90% of on-board robot communication runs on these three buses, and so do 90% of the traps. You have only really learned them once you can decode one complete timing sequence on a logic analyzer — otherwise you are stuck forever at «the library example runs».

Core stage · Hardcore
System software · Aware Embedded & controls · Proficient

Tools
Tools

What to learn

  • UART: the asynchronous frame (start + 8 data + parity + stop), baud-rate error accumulation (combined error at both ends beyond ±2%–3% starts producing occasional framing errors), and why 16× oversampling tolerates it
  • RTS / CTS hardware flow control; TTL levels vs RS-232 levels, and why a debug port needs level shifting
  • I2C: open-drain plus external pull-ups is the electrical core; how multi-master bit-by-bit arbitration avoids losing data; clock stretching (a slave saying «wait a moment»)
  • I2C's hard limits: bus capacitance 400 pF, 7-bit addresses and address conflicts, the three speed grades 100k / 400k / 1M
  • SPI: four wires (SCLK / MOSI / MISO / CS), the four CPOL / CPHA modes, «wrong mode reads back 0xFF or 0x00», and the essence of full-duplex shifting
  • SPI's boundaries: no ACK, no flow control, no arbitration — board-level short distance only
  • Be able to decode one complete I2C read on a logic analyzer: start → address+W → ACK → register → repeated start → address+R → data → NACK → stop
  • Be able to measure rise times on a scope and judge whether the bus capacitance is over the limit

The lesson · plain words

UART, I2C, and SPI are the on-board communication trio, and every sensor on a robot hangs off one of them. Their differences are visible at a glance in the waveforms — once you can read waveforms, wiring troubleshooting takes 5 minutes.

Who does what in the trio

UART: two wires (TX/RX), asynchronous, point-to-point, both sides agree on a baud rate — the default for debug logs and module interfacing. I2C: two wires (SDA/SCL), synchronous, multi-device bus distinguished by address — pins are cheap, speed 100k–3.4 MHz. SPI: four synchronous wires (MOSI/MISO/SCK/CS), full-duplex and fast (tens of MHz), one chip-select line per slave — more pins but speed.

Selection rule of thumb: human interfaces and logs go on UART; many low-speed sensors hang off I2C; high-throughput devices (displays / Flash / IMU fast modes) go on SPI. A robot carrying all three at once is the norm.

Interactive figure · drag it and see

What this figure shows: an I2C waveform decoder view with SDA and SCL traces — a START condition, then a 7-bit address plus R/W bit (0x50 write = 10100000), the 9th clock acting as the slave ACK (pulled low = received), data bytes with ACKs, and a STOP where SDA jumps low→high while SCL is high; segment markers switch between start + address, data + ACK, and stop. Takeaway: two wires (data + clock), many devices told apart by address — pin-cheap but slow, and every phase of the transaction is readable directly off the two lines.

I2C's traps and their fixes

Pull-up resistance (typically 4.7 kΩ) interacts with bus length and device count — hang too many devices and the rising edge slows; on a 3.3 V system at 400 kHz you start getting errors. Address conflicts (two sensors with the same address): use address jumpers or a multiplexer (TCA9548A).

Bus lockup (SDA held low and not released): some slave is waiting for clocks. Fix: the master issues 9 extra SCL clocks to make it release, or power-cycle the bus. One logic-analyzer capture tells you who was holding it (E13).

Engineering details of SPI and UART

SPI's four modes (CPOL/CPHA) must match between master and slave — when the waveforms do not line up it is a mode misconfiguration, and a logic analyzer shows it instantly. Each device needs its own chip select; long runs (>20 cm) need a lower clock or a buffer. Flash chips, displays, and IMUs all have a «mode table» — check it before wiring.

UART engineering discipline: keep baud-rate error <2% (MCUs with inaccurate clocks misalign at high speeds); add a frame header and checksum to your protocol (raw text silently drops bytes); RS485/CAN are its industrial long-distance versions (E18).

How deep to go

Recognize all three interfaces from their waveforms; decode one I2C read/write with a logic analyzer; explain how to compute the pull-up resistance (bus capacitance × rise time). Do these three and «the sensor will not connect» never stumps you again.

Three questions — do you really get it?

1. Multiple low-speed sensors must connect to an MCU while saving pins. Choose?

  • AUART
  • BI2C
  • CSPI
Show answer

B. I2C hangs many devices on two wires and addresses them. SPI needs one chip-select per device; UART is point-to-point.

2. The I2C device cannot be read and SDA is stuck low. What is step one?

  • AReplace the sensor
  • BCapture the bus with a logic analyzer and confirm who is holding SDA
  • CRaise the voltage
Show answer

B. Diagnose before acting. Issuing 9 extra clocks is the standard fix — but you must know who was stuck first.

3. What must both sides of an SPI link agree on?

  • ABaud rate
  • BWire color order
  • CThe CPOL/CPHA mode
Show answer

C. The four modes define the sampling instants. The classic symptom of a mismatch is «data all correct but shifted by one bit».

Keep going deeper

Links without a marker stay on this site; the rest point to the Chinese edition until its translation batch lands.

How it fails in the field

  • No I2C pull-ups, or pull-ups too large: the rising edge never climbs, and the fast speed grades simply stop working
  • Confusing 7-bit and 8-bit addresses: the read/write bit gets treated as an address bit, and the device NACKs forever
  • Mismatched SPI mode: you read back all 0s or all 1s and start suspecting the chip is dead
  • Treating UART as reliable transport: no CRC, no retransmission — errors can only be caught by the layer above

You have learned enough when

You can point out every byte and ACK bit of one I2C read on a logic analyzer and say whether the address is 7-bit or 8-bit; you can list three possible causes of «SPI reads back all 0xFF», ranked by likelihood.

The Chinese edition carries the full topic page for On-board trio: UART / I2C / SPI, including the complete outline, depth requirements per career track, and further derivations: robotguide.dev/k/E16/.