Skip to content
Tutorials Center
My learning

Topics

Lesson 15 of 36 ยท Connectivity Protocols

Serial Buses: UART, I2C and SPI

Learn how UART, I2C, SPI, 1-Wire and CAN connect microcontrollers to sensors, with framing, addressing, wiring, speed and runnable bus simulations.

Before a reading reaches the cloud, it often travels a few centimeters over wires between a sensor chip and the microcontroller. Three simple serial buses do most of that work: UART, I2C and SPI. Each uses a different number of wires and a different way of deciding who talks when. Knowing them lets you read datasheets, wire modules correctly and debug silent sensors.

A bus is a shared set of wires plus agreed rules (a protocol). "Serial" means bits travel one after another on a wire instead of many in parallel, which keeps wiring simple.

Comparing the three

UART I2C SPI
Wires 2 (TX, RX) + ground 2 (SDA, SCL) + ground 4+ (MOSI, MISO, SCK, CS per device)
Devices 2 (point to point) Up to 112 on one bus, each with an address Many, each needs its own chip-select
Clock None (agreed baud rate) Yes, from the controller Yes, from the controller
Speed 9,600 to 1,000,000+ baud 100 kHz, 400 kHz, 1 MHz 1 to 80 MHz
Distance Meters (RS-232/485 for far) Centimeters to about 1 m Centimeters
Typical use GPS, debug console, modems Sensors, displays, EEPROM Flash, SD cards, fast displays, ADCs

UART: asynchronous serial

UART has no clock wire. Both sides agree on a baud rate (bits per second, for example 9600 or 115200) and a frame format. The common "8N1" frame is: one start bit (low), eight data bits (least significant first), no parity, one stop bit (high). The line idles high. TX of one device connects to RX of the other (cross over), and grounds are shared.

Python Encoding a byte as a UART 8N1 frame

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Because only 8 of the 10 bits carry data, throughput is 80% of the baud rate. Mismatched baud rates produce garbage characters, the number one beginner symptom.

C++
// GPS module on a second hardware UART (ESP32)
HardwareSerial gps(1);
void setup() { Serial.begin(115200); gps.begin(9600, SERIAL_8N1, 16, 17); }
void loop()  { while (gps.available()) Serial.write(gps.read()); }

Variants: RS-485 uses differential signalling for hundreds of meters in noisy factories (Modbus RTU runs over it); RS-232 uses higher voltages and older connectors.

I2C: two wires, many devices

I2C (also called TWI) uses SDA (data) and SCL (clock) with open-drain lines and pull-up resistors (about 4.7 kohm to the supply voltage). One controller drives the clock; up to 112 targets (older docs say master and slave) each have a 7-bit address. A transaction: START, address plus read/write bit, acknowledge, data bytes with acknowledges, STOP.

sequenceDiagram
    participant C as Controller
    participant T as Sensor 0x76
    C->>T: START + address 0x76 + Write
    T-->>C: ACK
    C->>T: register 0xFA
    T-->>C: ACK
    C->>T: RESTART + address 0x76 + Read
    T-->>C: ACK + temperature byte 1
    C->>T: ACK
    T-->>C: temperature byte 2
    C->>T: NACK + STOP

Reading two bytes from register 0xFA of a sensor at address 0x76.

Practical points: two devices with the same address conflict (change an address pin, or use an I2C multiplexer such as the TCA9548A); more devices and longer wires mean more capacitance and need smaller pull-ups or lower speed; 3.3 V and 5 V devices need level shifting.

Python A tiny I2C bus simulation with an address scan

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The scan listing is the first debugging step on any I2C project: if the sensor's address does not appear, check power, wiring, pull-ups and voltage levels. (0x76 is also the BME280 chip's address, and its ID register 0xD0 returns 0x60.)

C++
#include <Wire.h>
void setup() {
  Serial.begin(115200);
  Wire.begin();
  for (byte addr = 1; addr < 127; addr++) {
    Wire.beginTransmission(addr);
    if (Wire.endTransmission() == 0) { Serial.print("found 0x"); Serial.println(addr, HEX); }
  }
}
void loop() {}

SPI: fast and full duplex

SPI uses four signals: SCK (clock), MOSI (controller out), MISO (controller in) and one CS (chip select, active low) per device. While the controller shifts a byte out on MOSI, the target shifts a byte back on MISO at the same time. There are no addresses or acknowledgements; CS selects the device. Four modes (0 to 3) define the clock polarity and phase, and a device's datasheet tells you which one to use.

Python SPI full-duplex byte exchange

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Wiring many SPI devices costs one extra CS pin each, but speed is excellent for displays, SD cards and flash.

Other wired buses you will meet

  • 1-Wire (DS18B20 sensors): one data wire, many devices identified by 64-bit serial numbers.
  • CAN bus: robust differential bus used in cars and industry, with message IDs and arbitration.
  • Modbus (RTU over RS-485, TCP over Ethernet): the industrial classic for PLCs and meters.
  • Ethernet and PoE: wired IP networking with power on the same cable.
  • USB: connection to computers; many boards expose a serial port over USB.

Debugging checklist

  1. Common ground connected?
  2. Voltage levels compatible (3.3 V vs 5 V)?
  3. TX to RX (crossed) for UART; SDA to SDA and SCL to SCL for I2C?
  4. I2C pull-ups present (many breakout boards include them)?
  5. Right address, baud rate and SPI mode?
  6. Wires short enough for the speed?

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How many data wires does I2C need between the controller and all its sensors?

    I2C shares SDA and SCL, and devices are told apart by their 7-bit addresses.
  2. 2. What is a UART 8N1 frame?

    8N1 means 8 data bits, no parity and 1 stop bit, plus the start bit.
  3. 3. Why does SPI need a chip-select line per device?

    SPI has no addressing; pulling a device's CS low tells it to respond.
  4. 4. Your I2C scan finds nothing. Which is NOT a likely cause?

    Power, pull-ups and wiring matter; color does not.

Summary

  • UART: asynchronous, two devices, agreed baud rate, frames of start, data and stop bits.
  • I2C: two shared wires, addressed devices, pull-ups required, moderate speed.
  • SPI: four signals plus a chip-select per device, simple and very fast.
  • Also: 1-Wire, CAN, Modbus, Ethernet. Always check ground, voltage, wiring and addresses first.

Next: Short-Range Wireless: Wi-Fi, Bluetooth, Zigbee and Thread