Overview
Curated: · Written: · Reviewed:
Communication Protocols: SPI, I2C, UART
In one line
SPI, I2C and UART are electrical and framing mechanisms for talking to nearby peripherals and other boards. Reach for SPI for fast full-duplex links to a few devices, I2C for many low-speed devices on two shared wires, and UART for a point-to-point link with no shared clock.
Key definitions
- Push-pull: both ends actively drive the line, so two enabled drivers on one line is contention, not a logic level.
- Open-drain: devices only pull low and the pull-up sets the high level, which makes the bus a wired-AND.
- CPOL and CPHA: CPOL sets the SCLK idle level, CPHA sets which edge samples. Together they give modes 0 to 3.
- Repeated START: a START issued without a STOP, beginning the next address phase.
- Framing error versus overrun: framing means a bad start, stop or sample position; overrun means a received byte was lost because the previous one was not read in time.
- STOP: SDA rising while SCL is high.
Formulas and complexity
UART divisor: At 16 MHz for 115200 baud the divisor is 8.68, rounded to 9, giving an actual rate of 111111 baud, an error of -3.55%. I2C Fast-mode limits:
How it works
- SPI: the master asserts CS and drives SCLK. Each bit is shifted out on one edge and sampled on the other, as set by the mode. A wrong mode gives no clean error; the bits simply land in the wrong positions.
- I2C: START, then the 7-bit address plus R/W, ACK on the ninth clock, then data bytes, then STOP. The master NACKs the last byte it wants to read.
- UART: idle is high. A start bit (low), 5 to 9 data bits LSB-first, optional parity, then 1, 1.5 or 2 stop bits. The receiver oversamples 16 times and samples mid-bit.
When to use and when not to
| Choice | Reach for it when | Avoid it when |
|---|---|---|
| SPI | Fast full-duplex link to a few devices; one CS per slave | Many peripherals on the pin budget; slow sensors |
| I2C | Many low-speed devices on two shared wires, addressed by 7 or 10 bits | Bus capacitance is high and rise time cannot meet 300 ns |
| UART | Point-to-point link to a console, board or modem with no shared clock | Both ends cannot hold baud error to a few percent |
SPI has a clock line and no addressing; I2C is open-drain and multi-drop; UART has no clock and both ends must agree on baud, data bits, parity, stop bits and polarity.
Traps interviewers probe
- Wrong SPI mode. Sampling one edge off turns 0xA5 into 0x4B with an idle-high clock. Fix: match CPOL and CPHA to the datasheet and confirm the edges on a logic analyzer.
- Connecting mismatched logic levels directly. A 5 V push-pull output into a 3.3 V part, or powering it from the 5 V rail, can damage the device. Fix: use a level shifter or a 3.3 V supply and check the absolute maximum ratings.
- Treating every NACK as device absent. An address NACK means nothing answered, a data-byte NACK means the device rejected that byte, and a final read NACK is normal. Fix: classify the NACK by stage before retrying.
- Retrying a non-idempotent write. A write that NACKs after the data byte may already have taken effect. Fix: guard the retry with a transaction id or write an absolute value.
- Ignoring I2C rise time. The rising edge is the pull-up charging the bus capacitance, and it must reach 0.7 VDD before the master samples. Fix: check the RC against 300 ns and reduce the pull-up or the capacitance.
Interview tips
Open by naming the bus, the number of devices, and what the link must guarantee. For example: 'I2C, three low-speed sensors, so the link must give addressing, ACK checking and a bus timeout.'
Expect these follow-ups: the rise-time budget at temperature and voltage corners, arbitration-loss handling, DMA cache maintenance, the baud-error budget, 9-bit multidrop framing, and NACK classification.
30-second answer outline:
- Name the bus and its wiring: SPI with CS per slave, I2C on SCL and SDA with pull-ups, or UART TX and RX with a common ground.
- State the mode, baud or speed, and framing that both ends must match.
- Name the failure you guard against: a wrong mode, a NACK misread as absent, a timeout, or a retry that applies twice.
- State how you prove it on the wire: a logic-analyzer capture of START, address, ACK and STOP, or the baud error calculation.
Feedback on this overview
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.