I²C, SPI and UART: how chips and sensors talk to each other
In short: UART, I²C and SPI are the three classic serial interfaces between microcontrollers, sensors and displays, with different line counts, speeds and addressing.
In more detail: Sensors, displays, memory and modules connect to the microcontroller (Arduino, ESP32, Raspberry Pi) over few lines. Which protocol fits is decided by the component’s datasheet.
In Depth
UART (serial)
- 2 lines (TX, RX) + GND, asynchronous (no clock line): both sides agree on a baud rate (e.g. 9600, 115200), 8N1 (8 data bits, no parity, 1 stop bit).
- Cross TX ↔ RX; same levels (3.3 V vs 5 V!).
- Typical: serial monitor/debug, GPS modules, Bluetooth modules, console access (router/Raspberry Pi), USB-serial adapters.
- RS-232 (± 12 V) and RS-485 (differential, long lines, industry) are related levels/standards.
I²C (Inter-Integrated Circuit)
- 2 lines: SDA (data), SCL (clock) + GND; several devices on one bus, each with a 7-bit address (e.g. 0x3C OLED, 0x68 real-time clock).
- Pull-up resistors (4.7 kΩ) needed (often on breakout boards).
- Speed 100 kHz / 400 kHz (fast mode) up to MHz; master/slave (controller/target).
- Ideal for sensors (temperature, acceleration), small OLED displays, EEPROMs.
- Address conflicts (two identical sensors) → address pin/multiplexer. Debug:
i2cdetect -y 1(Raspberry Pi) / I²C scanner (Arduino).
SPI (Serial Peripheral Interface)
- 4 lines: MOSI, MISO, SCK, CS/SS (chip select per device) + GND; full duplex, very fast (MHz to tens of MHz).
- For displays (TFT), SD cards, flash memory, ADC/DAC, LED drivers, radio modules (nRF24).
- One CS line per device (more pins than I²C); no standard addressing scheme.
- Modes (clock polarity/phase 0–3) must match.
Choosing and practice
- Few pins, several devices, medium speed → I²C.
- High data rate (display/SD) → SPI.
- Point to point, simple → UART.
- Level shifter for 5 V ↔ 3.3 V (Pi/ESP32 are 3.3 V! 5 V on a GPIO can destroy it).
- Keep cable lengths short (I²C/SPI are meant for board/few decimetres).
- Libraries (Wire, SPI,
smbus,spidev, MicroPythonmachine) encapsulate details. - Logic analyser (cheap, Sigrok/PulseView) shows signals for debugging.
- Related: 1-Wire (DS18B20), CAN (cars), USB, Ethernet.
See also: Arduino and microcontrollers, ESP32, Raspberry Pi