Table of Contents
Electronics Reference: Microcontroller Communication Buses (UART, SPI & I2C)
1. Protocol Comparison Matrix
Select the appropriate serial protocol based on required data throughput, wiring simplicity, distance, and device count:
| Feature | UART (Universal Asynchronous Receiver-Transmitter) | SPI (Serial Peripheral Interface) | I2C (Inter-Integrated Circuit) |
|---|
| Communication Type | Asynchronous (no shared clock wire) | Synchronous (dedicated `SCLK` wire) | Synchronous (shared `SCL` wire) |
| Duplex Mode | Full-Duplex (simultaneous TX & RX) | Full-Duplex (simultaneous MOSI & MISO) | Half-Duplex (bidirectional single `SDA` line) |
| Required Signal Wires | 2 wires: `TX`, `RX` (+ `GND`) | 4+ wires: `MOSI`, `MISO`, `SCLK`, `CS` (+ `GND`) | 2 wires: `SDA`, `SCL` (+ `GND` + Pull-up resistors) |
| Typical Data Speed | 9,600 bps – 1 Mbps (up to 4+ Mbps on modern MCUs) | 1 MHz – 50+ MHz (fastest board-level bus) | Standard: 100 kHz / Fast: 400 kHz / Fast+: 1 MHz |
| Maximum Devices | Point-to-point (2 devices only per port) | 1 Master + Multiple Slaves (1 `CS` pin per slave) | Up to 127 devices (7-bit addressing) on 2 wires |
| Hardware Pull-Ups | Not required (push-pull drivers) | Not required (push-pull drivers) | Mandatory (`2.2 kΩ – 10 kΩ` on `SDA` and `SCL`) |
| Max Cable Distance | Moderate (~1–3 m TTL; up to 1,200 m via RS-485) | Very Short (< 15–30 cm on PCB/harnesses) | Short (< 0.5–1 m; limited by 400 pF bus capacitance) |
| Common Applications | GPS/GNSS, telemetry radios, RC receivers (CRSF/SBUS), USB-to-Serial debug | IMUs (gyros/accelerometers), SD cards, flash memory, high-speed TFT displays | Barometers, magnetometers (compasses), OLED displays, EEPROMs, BMS monitors |
2. UART (Universal Asynchronous Receiver-Transmitter)
UART transmits data asynchronously byte-by-byte without a shared clock signal. Both devices must be pre-configured to the exact same timing parameters.
2.1 Wiring & Crossover Rule
- Crossover Connection: Always connect `TX` (Transmit) on Device A to `RX` (Receive) on Device B, and `RX` on Device A to `TX` on Device B. Connecting `TX → TX` and `RX → RX` is the most common UART wiring mistake.
- Common Ground: Both devices must share a direct `GND` wire so logic voltage levels share the same reference.
2.2 Frame Structure & Baud Rate
A standard UART packet (`8N1` configuration) consists of:
- Idle State: Line held `HIGH` (`3.3 V` or `5.0 V`).
- Start Bit: 1 bit pulled `LOW` to alert the receiver that data is starting.
- Data Bits: 8 bits (1 byte), transmitted Least Significant Bit (LSB) first.
- Parity Bit: None (`N`), Even (`E`), or Odd (`O`) for basic error checking.
- Stop Bit(s): 1 bit (or 2 bits) driven `HIGH` to mark the end of the frame.
- Baud Rate: Both sides must match within ±2% (standard rates: `9600`, `57600`, `115200`, `420000` for CRSF, `921600`).
2.3 Logic-Level UART vs. RS-232 vs. RS-485
- TTL / CMOS UART (`0 V` to `3.3 V` / `5.0 V`): Used directly between microcontrollers and sensors.
- RS-232 (`-15 V` to `+15 V`): Legacy PC serial standard with inverted logic and high voltages. Never connect RS-232 signals directly to a microcontroller UART pin—use a transceiver IC such as `MAX232` or `MAX3232`.
- RS-485 (Differential Pair `A` / `B`): Noise-immune industrial standard capable of multi-drop networking up to 1,200 meters (4,000 ft) using twisted-pair wiring (`MAX485` transceiver).
3. SPI (Serial Peripheral Interface)
SPI is a high-speed synchronous master-slave bus ideal for real-time sensors (such as flight controller IMUs) and bulk memory transfers.
3.1 Signal Lines & Addressing
- `SCLK` (Serial Clock): Generated exclusively by the Master to synchronize data bits.
- `MOSI` (Master Out, Slave In) / `COPI`: Carries data from the Master to the Slave.
- `MISO` (Master In, Slave Out) / `CIPO`: Carries data from the Slave back to the Master simultaneously.
- `CS` / `SS` (Chip Select / Slave Select): Active-LOW (`0 V`) dedicated line from the Master to each individual Slave. Only the Slave whose `CS` pin is pulled `LOW` listens to `SCLK`/`MOSI` and drives `MISO`; all other slaves keep `MISO` in a high-impedance (`Hi-Z`) state.
- External `CS` Pull-Up (`10 kΩ`): Add a pull-up resistor to $V_{CC}$ on every `CS` line so slaves remain cleanly deselected while the MCU is resetting.
3.2 SPI Clock Modes (CPOL & CPHA)
Master and Slave must match one of four clock timing modes defined by Clock Polarity (`CPOL`) and Clock Phase (`CPHA`):
| SPI Mode | CPOL (Idle Clock State) | CPHA (Data Sampling Edge) |
|---|
| Mode 0 (Most Common) | `0` (Clock idles `LOW`) | `0` (Sampled on rising edge, shifted on falling edge) |
| Mode 1 | `0` (Clock idles `LOW`) | `1` (Sampled on falling edge, shifted on rising edge) |
| Mode 2 | `1` (Clock idles `HIGH`) | `0` (Sampled on falling edge, shifted on rising edge) |
| Mode 3 | `1` (Clock idles `HIGH`) | `1` (Sampled on rising edge, shifted on falling edge) |
4. I2C (Inter-Integrated Circuit)
I2C allows up to 127 peripheral devices to share just two wires—`SDA` (Serial Data) and `SCL` (Serial Clock)—using unique 7-bit hardware addresses.
4.1 Open-Drain Architecture & Pull-Up Resistors
- Open-Drain Drivers: I2C devices can only pull `SDA` and `SCL` `LOW` to `GND`; they never actively drive the lines `HIGH`. This prevents electrical bus contention (short circuits) if two devices transmit at the same time.
- Mandatory Pull-Up Resistors: External resistors connected between `SDA` → $V_{CC}$ and `SCL` → $V_{CC}$ pull the lines `HIGH` whenever no device is pulling them down.
- Standard Values: Use `4.7 kΩ` for standard `100 kHz` / `400 kHz` buses at `3.3 V` or `5.0 V`. Use `2.2 kΩ` for longer wires or high-speed `400 kHz – 1 MHz` buses with higher parasitic capacitance.
- Multiple Modules Warning: Many sensor breakout boards include built-in `10 kΩ` pull-ups. Connecting 5+ breakout boards in parallel drops total pull-up resistance below `2 kΩ`, which can prevent weak sensors from pulling the line fully `LOW`.
4.2 I2C Addressing & Transaction Flow
- START Condition: Master pulls `SDA` `LOW` while `SCL` stays `HIGH`.
- 7-Bit Address + R/W Bit: Master clocks out the 7-bit slave address (e.g., `0x68` for MPU6050 IMU, `0x76` for BMP280 barometer) followed by a `0` (Write) or `1` (Read) bit.
- ACK / NACK Bit: The matching slave pulls `SDA` `LOW` on the 9th clock pulse to send an Acknowledge (`ACK`). If no device responds, the line stays `HIGH` (`NACK`).
- Data Bytes & STOP Condition: Data bytes follow (each followed by an `ACK`), ending when the Master releases `SDA` from `LOW` to `HIGH` while `SCL` is `HIGH` (STOP).
5. Voltage Level Shifting Between `3.3 V` and `5.0 V`
Connecting a `5.0 V` device to a `3.3 V` microcontroller without level translation can permanently damage non-5V-tolerant GPIO pins:
- UART & SPI Level Shifting: Use unidirectional or directional logic buffers such as `74LVC245`, `74AHCT125`, or a simple resistor voltage divider (`1.8 kΩ` + `3.3 kΩ`) on `5 V TX → 3.3 V RX` lines.
- I2C Bidirectional Level Shifting: Because `SDA` and `SCL` are bidirectional open-drain lines, use a BSS138 N-Channel MOSFET bidirectional level shifter (or dedicated I2C buffer like `PCA9306`).
6. Serial Bus Troubleshooting & Debugging Checklist
When a sensor or peripheral fails to communicate, diagnose the bus systematically:
- [ ] Verify Power & Common Ground: Measure $V_{CC}$ at the peripheral pin and confirm `0.0 Ω` continuity between Master `GND` and Slave `GND`.
- [ ] Check UART `TX` / `RX` Polarity: Swap `TX` and `RX` wires if no data arrives; verify matching Baud Rate (`115200` vs. `9600`).
- [ ] Run an I2C Address Scanner: Flash an I2C scanner sketch to verify the peripheral responds with an `ACK` and confirm whether its 7-bit address matches your code (e.g., `0x76` vs. `0x77` depending on the `SDO`/`ADDR` pin state).
- [ ] Measure Idle I2C Bus Voltage: Both `SDA` and `SCL` should read steady `3.3 V` (or `5.0 V`) when idle. If either line is stuck at `0 V`, a device is holding the bus low or pull-up resistors are missing.
- [ ] Inspect Waveforms with a Logic Analyzer / Oscilloscope:
- On I2C, rounded “shark-fin” rising edges on `SCL`/`SDA` indicate excessive bus capacitance or pull-up resistor values that are too high—lower pull-ups from `10 kΩ` to `2.2 kΩ – 4.7 kΩ`.
- On SPI, confirm `CS` drops `LOW` before `SCLK` begins pulsing and returns `HIGH` after the final byte.
Electronics Reference: 1. Diodes & Transistors | 2. Integrated Circuits (ICs) | 3. Communication Buses (UART, SPI & I2C)
