Serial Protocol Lab

Deep-dive theory + interactive waveform generator for I²C, SPI, and UART — the three protocols every embedded and VLSI engineer must master.

📡 What is I²C? Theory

I²C (Inter-Integrated Circuit) is a synchronous, multi-master, multi-slave, packet-switched serial communication protocol invented by Philips (now NXP) in 1982. It uses only two bidirectional wires — SDA (Serial Data) and SCL (Serial Clock) — pulled to VCC through resistors. Because both lines are open-drain, any device can pull them LOW; they go HIGH only when all devices release the line. This is what enables multi-master operation and clock stretching without bus contention.

Every slave has a unique 7-bit address (112 usable out of 128; some are reserved). The master begins a transaction with a START condition (SDA falls while SCL is HIGH), then sends the 7-bit slave address followed by a R/W̄ bit. The addressed slave responds with an ACK by pulling SDA LOW during the 9th clock. Data then flows byte by byte, each followed by an ACK, until the master issues a STOP condition (SDA rises while SCL is HIGH).


Signal Lines
  • SDA (Serial Data) — bidirectional data line. Must be stable when SCL is HIGH; can only change when SCL is LOW.
  • SCL (Serial Clock) — clock driven by the master. Slaves can hold it LOW (clock stretching) to pause the transaction.
  • Pull-up resistors — typically 4.7 kΩ (Standard/Fast) or 1–2 kΩ (Fast+/HS). Value sets max speed and bus capacitance.

Timing Rules (what makes I²C unique)
  • START (S): SDA falls while SCL is HIGH — this is the only time SDA is allowed to change while SCL = 1.
  • STOP (P): SDA rises while SCL is HIGH — similarly reserved for control signalling.
  • Data valid: SDA must be stable for the entire HIGH phase of SCL. SDA may change only during SCL LOW.
  • Repeated START (Sr): Master issues another START without a STOP — retains bus ownership for a read-after-write.
  • ACK: Receiver pulls SDA LOW on 9th clock. NACK: SDA left HIGH — no device at that address, or end of read.
  • Clock stretching: A slow slave holds SCL LOW to insert wait states. Master must check SCL before releasing it.
  • Arbitration: If two masters start simultaneously, the one that transmits a HIGH while another drives LOW loses and backs off.

I²C Frame Format

A complete write transaction: START → [ADDR 7b][R/W̄][ACK] → [DATA 8b][ACK] × N → STOP

Idle
1
S
ADDR [6:0]
7 bits
R/W̄
1 bit
ACK
DATA [7:0]
8 bits
ACK
P
Idle
1

Speed Modes
ModeMax ClockMax Bus CapTypical Use
Standard (Sm)100 kHz400 pFEEPROMs, basic sensors
Fast (Fm)400 kHz400 pFRTCs, most modern sensors
Fast-Plus (Fm+)1 MHz550 pFHigh-speed sensors, displays
High-Speed (Hs)3.4 MHz100 pFMemory, camera modules
Ultra-Fast (UFm)5 MHzWrite-only applications

Common Use Cases

Sensors (temperature, pressure, IMU), EEPROM (AT24C series), RTC (DS3231), OLED displays (SSD1306), battery management ICs (BQ series), GPIO expanders (PCF8574). I²C is preferred when pin count is critical and bus speed is not the bottleneck.

🔬 I²C Waveform Generator Interactive
Configure and click Generate to see the timing diagram.
SCL SDA S/P markers ACK NACK
⚡ What is SPI? Theory

SPI (Serial Peripheral Interface) is a synchronous, full-duplex serial communication protocol developed by Motorola in the mid-1980s. It uses four wires: SCLK (clock), MOSI (Master Out Slave In), MISO (Master In Slave Out), and CS/SS (Chip Select, active LOW). Unlike I²C, SPI has no addressing mechanism — the master selects a specific slave by asserting its CS pin LOW. Multiple slaves share the same SCLK/MOSI/MISO bus (daisy-chain or parallel CS lines).

SPI is a shift register protocol: internally, master and slave each have an N-bit shift register. On every clock edge, one bit exits the master's register via MOSI and enters the slave, while one bit exits the slave via MISO and enters the master. After N clocks, a complete N-bit word has been exchanged — simultaneously in both directions. There is no ACK; the master must know the protocol of each device to interpret responses.


Signal Lines
  • SCLK — Serial clock, always driven by master. Can be MHz to hundreds of MHz.
  • MOSI — Master Out Slave In. Data from master to slave, shifted MSB-first by convention.
  • MISO — Master In Slave Out. Data from slave to master, tri-stated when CS is HIGH.
  • CS / SS — Chip Select (active LOW). One per slave. Master asserts LOW to begin, deasserts HIGH to end.

The Four SPI Modes — CPOL × CPHA

CPOL (Clock Polarity) sets the idle state of SCLK: 0 = idle LOW, 1 = idle HIGH.
CPHA (Clock Phase) sets which edge is the capture edge: 0 = leading edge, 1 = trailing edge.
Leading edge is the first active transition after CS asserts (rising for CPOL=0, falling for CPOL=1).

Mode 0 — CPOL=0, CPHA=0
Idle LOW · Sample on rising edge · Shift on falling. Most common (microcontrollers, flash, ADCs).
Mode 1 — CPOL=0, CPHA=1
Idle LOW · Sample on falling edge · Shift on rising. Used by some sensors (e.g. MAX31855).
Mode 2 — CPOL=1, CPHA=0
Idle HIGH · Sample on falling edge · Shift on rising. Less common; some ADCs and DACs.
Mode 3 — CPOL=1, CPHA=1
Idle HIGH · Sample on rising edge · Shift on falling. Used by SD cards, some Ethernet PHYs.

Timing Rules
  • CS must assert before SCLK starts. Some devices require t_CS_setup > 5 ns.
  • MOSI is valid before the capture edge. t_setup must be met.
  • MISO is released when CS deasserts. The slave tri-states MISO to allow bus sharing.
  • MSB-first is conventional but some devices (e.g. certain ADCs) use LSB-first. Always check the datasheet.
  • Word size varies: 8-bit is most common, but 16- and 32-bit transfers exist.
  • No ACK. The master must implement protocol-level validation (e.g. read a status register after write).

I²C vs SPI Quick Comparison
FeatureI²CSPI
Wires2 (SDA + SCL)4 (SCLK, MOSI, MISO, CS)
DuplexHalf-duplexFull-duplex
SpeedUp to 5 MHzUp to 100+ MHz
Addressing7-bit in packetCS pin per slave
ACKYes (hardware)No
Multi-slaveUp to 112 on same busOne CS pin per slave
Typical useSensors, EEPROMs, RTCsFlash, ADC/DAC, SD, displays
🔬 SPI Waveform Generator Interactive
Configure and click Generate.
CS SCLK MOSI MISO
📟 What is UART? Theory

UART (Universal Asynchronous Receiver-Transmitter) is a hardware communication protocol that uses asynchronous serial communication with configurable speed. Unlike I²C and SPI, UART has no clock line — both sender and receiver independently generate their own bit-rate clocks that must be pre-configured to the same baud rate (bits per second). The idle line state is logic HIGH (mark). Transmission begins with a mandatory START bit (logic LOW), which lets the receiver detect the start of a frame and synchronise its sampling clock.

Data bits are transmitted LSB first by convention (bit 0 before bit 7). An optional parity bit follows the data for error detection. One or two STOP bits (logic HIGH) end the frame, giving the receiver time to process before the next frame. Because there is no clock recovery mechanism, baud rates must match within ±2–3% — any mismatch causes the sampling point to drift across the frame.


Frame Format

Example: 8N1 (8 data bits, No parity, 1 stop bit) transmitting 0x41 ('A' = 0b01000001)

IDLE
1
START
0
D0
1
D1
0
D2
0
D3
0
D4
0
D5
0
D6
1
D7
0
STOP
1
IDLE
1

Note: 0x41 = 0b01000001. LSB (bit 0 = 1) is sent first → D0=1, then D1..D6=0, D7=0.


Key Concepts
  • Baud rate: Bits per second. At 115200 baud, each bit = 1/115200 ≈ 8.68 µs.
  • LSB first: Bit 0 (LSB) is always transmitted first; receiver reverses the order.
  • START bit edge: The falling edge of the START bit is the sync event — receiver's sampling clock starts from this edge + 0.5 bit periods (samples at mid-bit for maximum noise margin).
  • Parity — Even: Parity bit is chosen so the total number of 1s (data + parity) is even.
  • Parity — Odd: Total number of 1s is odd. Neither detects 2-bit errors.
  • Stop bit(s): Line returns to HIGH. The receiver checks this; a LOW stop bit is a framing error.
  • Oversampling: UARTs typically sample each bit at 8× or 16× the baud rate and take a majority vote for noise rejection.
  • RS-232 vs TTL: RS-232 uses ±12 V logic (HIGH = -12 V, LOW = +12 V — inverted!). TTL UART uses 3.3 V or 5 V logic. Always match levels with a level-shifter or MAX232.
  • Flow control — RTS/CTS: Hardware flow control where receiver asserts CTS LOW to allow transmission. Prevents buffer overrun on slow receivers.

Common Baud Rates
Baud RateBit PeriodTypical Use
9600104.2 µsGPS modules, modems, legacy RS-232
1920052.1 µsIndustrial instruments
3840026.0 µsOlder Bluetooth UART
5760017.4 µsSome microcontroller debug ports
1152008.68 µsStandard debug/console (most common)
9216001.08 µsHigh-speed UART, FTDI chips
4,000,000250 nsMaximum for many UARTs

Common Use Cases

Debug console (printf over USB-UART), GPS receivers (NMEA sentences), Bluetooth UART (HC-05, HM-10), GSM/LTE modems (AT commands), bootloader flashing, data logging. UART is the universal "wire" for quick embedded communication.

🔬 UART Waveform Generator Interactive
Configure and click Generate.
TX line START bit Data bits (LSB first) Parity bit STOP bit(s)
❓ Quick Reference — Protocol Comparison
FeatureI²CSPIUART
TypeSynchronousSynchronousAsynchronous
Wires24 (+1 per slave)2 (TX + RX)
DuplexHalfFullFull
Max Speed5 MHz100+ MHz~4 Mbps
Addressing7-bit (in packet)CS pinNone
Error checkACK/NACKNoneParity / Framing
Multi-slaveYes (same bus)Yes (separate CS)No (point-to-point)
Multi-masterYes (with arbitration)NoNo
ComplexityMediumLowLowest