Sub topic 15
SPI Compared with I2C and UART
Assume you understand Arduino setup() and loop(), digital pins, hexadecimal numbers, and basic wiring. This chapter compares three common ways for a microcontroller to communicate with another device: SPI, I2C, and UART.
The main differences
SPI, I2C, and UART all move bytes, but they solve different wiring and coordination problems.
| Interface | Typical signals | Clocking | Device selection | Main strength |
|---|---|---|---|---|
| SPI | SCLK, MOSI, MISO, CS | Shared clock | Separate chip-select pin | High speed and simple transfers |
| I2C | SCL, SDA | Shared clock | Address on the bus | Many devices with only two signal wires |
| UART | TX, RX, GND | No shared clock | Usually point-to-point | Simple full-duplex serial links |
SPI uses a controller-generated clock. The controller selects one peripheral with its chip-select line, then shifts bits in both directions. A separate chip-select pin is normally needed for each peripheral.
I2C uses two shared wires. Each peripheral has an address, so the controller can select it by sending that address. The wires use open-drain signaling, which means devices pull a line low but do not actively drive it high. External pull-up resistors return the lines to high. Bus capacitance limits speed and cable length.
UART sends data asynchronously. The transmitter and receiver agree on a baud rate, such as 115200 bits per second, but they do not share a clock wire. Each byte includes framing bits, such as a start bit and stop bit. A UART link commonly connects two devices through TX and RX lines.
Choosing between them
Choose SPI when the peripheral needs fast transfers, such as a display, flash memory, ADC, or SD card. SPI has low protocol overhead, but it uses more pins. It also requires the correct clock mode, bit order, chip-select timing, and maximum clock rate.
Choose I2C when several moderate-speed peripherals must share a small number of wires. Sensors, real-time clocks, and port expanders commonly use I2C. Check for address conflicts, required pull-up resistors, and the total bus capacitance.
Choose UART for a simple point-to-point connection, such as a GPS receiver, computer console, or radio module. UART needs matching baud rate, data bits, parity, and stop-bit settings. A raw logic-level UART connection is usually intended for nearby devices. Use a suitable transceiver for longer cables or different voltage levels.
A device may support more than one interface, but the choice still affects wiring, speed, software complexity, and troubleshooting. SPI usually gives the highest throughput on a short PCB trace. I2C minimizes wiring for multiple addressed devices. UART is often the simplest link between two endpoints.
Exercise
You need to connect four sensors on one board, each operating at moderate speed, and you have few spare pins. Which interface is usually the best starting point, and what issue must you check?
Show answer
Start with I2C because all sensors can share SDA and SCL. Check that their addresses do not conflict and that the bus has suitable pull-up resistors.