Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Embedded communication protocols: CAN, MODBUS, UART, SPI and I2C

Clear, exam-focused notes on the embedded communication protocols CAN, Modbus (RTU/ASCII), UART, SPI and I2C: definitions, operation, frame formats, timing basics, worked examples, common mistakes and exam tips.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Embedded systems often need to exchange data between microcontrollers, sensors, actuators and industrial equipment. Protocols such as UART, SPI, I2C, CAN and Modbus are the standard ways engineers connect devices reliably in automotive, consumer and industrial systems.

Key ideas

Start with definitions and the simple physics and logic behind each bus.

  • Serial communication — sending bits one after another on one or more wires. (Contrasts with parallel communication which sends many bits simultaneously.)

  • UART (Universal Asynchronous Receiver Transmitter):

    • Definition: A hardware module that implements asynchronous serial byte-oriented communication between two devices using start, data, parity and stop bits.
    • Assumptions: Both sides must agree on baud rate (bits per second), data bits, parity and stop bits in advance. No separate clock line.
    • Frame format (typical): 1 start bit (logical 0), 5–8 data bits (LSB first), optional parity bit, 1 or 2 stop bits (logical 1). The line is idle high (logical 1). Many MCUs also support 9 data bits for multiprocessor addressing.
    • Draw to answer a diagram question: draw a time axis; label start bit (0), data bits b0..bN LSB→MSB, parity bit (if any) and stop bit(s) (1). Mark bit durations = 1/baud.
  • SPI (Serial Peripheral Interface):

    • Definition: A synchronous, full-duplex, master-slave protocol using separate Clock (SCK), Master Out Slave In (MOSI), Master In Slave Out (MISO) and a Slave Select (SS) line per slave.
    • Assumptions: One master generates SCK; slaves respond only when their SS is active (usually active low). No arbitration on bus.
    • Modes: Defined by CPOL (clock polarity) and CPHA (clock phase); four modes (0..3). CPOL=0 means idle SCK is low; CPHA=0 means sample on first edge. For a diagram: draw SCK, MOSI, MISO and SS showing edges where data is sampled and changed according to CPOL/CPHA.
  • I2C (Inter-Integrated Circuit):

    • Definition: A synchronous, multi-master, multi-slave, two-wire bus using SDA (data) and SCL (clock) with open-drain/open-collector outputs and pull-up resistors.
    • Assumptions: Devices release the line to let pull-ups pull it high; a device can pull the line low to transmit a dominant 0. Bus events: START (SDA falls while SCL high), STOP (SDA rises while SCL high). Each byte followed by an ACK or NACK bit from receiver. Slaves may hold SCL low to stretch the clock; a repeated START (Sr) is allowed without a preceding STOP; during reads, the master ACKs each received byte and sends a NACK after the final byte to terminate the transfer.
    • Addressing: 7-bit or 10-bit addressing (most use 7-bit). Each transfer: START, address+R/W, ACK, data bytes + ACKs, STOP.
  • CAN (Controller Area Network):

    • Definition: A differential, multi-master message-oriented bus widely used in automotive and industrial applications. Messages contain an identifier (which also sets priority), control bits and data payload.
    • Assumptions: Physical layer uses two wires (CAN_H, CAN_L). Logical level: dominant (logical 0, drives bus) and recessive (logical 1, released). Bitwise arbitration: lower numerical identifier has higher priority because dominant bits win over recessive.
    • Frame variants: Standard (11-bit identifier) and Extended (29-bit identifier). Frame contains start-of-frame, arbitration field (ID), control, data (0–8 bytes in classical CAN), CRC, ACK and end of frame. Classical CAN uses bit stuffing: after five consecutive bits of the same level, a stuff bit of the opposite level is inserted (receivers check and remove it; a violation causes a stuff error). The ACK field comprises an ACK slot (dominant bit driven by receivers that accepted the frame) followed by an ACK delimiter (recessive). Note: CAN FD extends the data field up to 64 bytes and allows a higher data-phase bitrate.
    • For diagrams: draw two nodes transmitting different IDs; show time axis of bits and indicate when one node detects a different level and drops out (loses arbitration).
  • Modbus (RTU and ASCII):

    • Definition: An application-layer protocol widely used in industrial control. Works over serial lines (Modbus RTU/ASCII) or TCP/IP. Master–slave model: master polls slaves.
    • RTU frame (typical): [Slave Address][Function Code][Data][CRC16 low][CRC16 high]. RTU uses binary framing; ASCII variant sends ASCII characters and LRC for error check. Valid slave addresses are 1–247 (0 = broadcast). Default serial settings are typically 8E1; if no parity is used, two stop bits (8N2) are recommended. CRC-16 uses polynomial 0xA001 and is transmitted low byte then high byte.
    • Timing: RTU frames rely on silent gaps (3.5 character times) to mark frame boundaries. Within a frame, the maximum inter-character gap is 1.5 character times.

Exact law-like statements and derivations:

  • UART bit duration: bit_time = 1 / baud. This is exact for asynchronous UART.
  • For SPI, SCK frequency = master clock divided by an integer divider (device-specific). In general, SCLK = f_master / divider.
  • I2C bus rise time for RC pull-up: the bus line is an RC circuit; per the I2C spec the 30%→70% rise time is approximately t_r ≈ 0.8473 × R_pullup × C_bus (10%→90% ≈ 2.2 × R_pullup × C_bus). For exam diagrams, show pull-up resistor to Vcc and bus capacitance to ground.
  • CAN bit timing: a CAN bit is divided into time quanta: SyncSeg (1 tq), PropSeg, PhaseSeg1, PhaseSeg2. Bit_time = (SyncSeg + PropSeg + PhaseSeg1 + PhaseSeg2) × Tq. The sample point is at the end of PhaseSeg1 (i.e., before PhaseSeg2). The resynchronization jump width (SJW) limits how much the bit time may be adjusted during hard/soft resynchronization.

Formulas

bit_time = 1 / baud — bit_time: duration of one serial bit (s); baud: bits per second (s⁻¹). Applies to UART. SCLK = f_master / divider — SCLK: SPI clock frequency (Hz); f_master: MCU clock feeding SPI peripheral (Hz); divider: integer divisor. Applies to SPI peripherals. bit_time_CAN = (Sync + Prop + Ph1 + Ph2) × Tq — all in units of time quanta (Tq). Sync, Prop, Ph1, Ph2 are integer counts. Applies to CAN bit-time configuration. I2C_rise_approx (30–70%) ≈ 0.8473 × R_pullup × C_bus — R_pullup in ohms, C_bus in farads, rise time in seconds (I2C spec measures 30–70%; for 10–90% use ≈ 2.2 × R_pullup × C_bus). Applies when RC model dominates bus behaviour. Baud_divisor ≈ f_uartclk / (oversampling × desired_baud) — oversampling commonly 16 or 8. Applies to UART peripherals that use oversampling. Modbus_RTU_frame = [Addr][Func][Data][CRC16_low][CRC16_high] — symbolic layout; CRC16 is two bytes (apply little-endian order). Applies to Modbus RTU.

For programming syntax to remember (pseudocode):

  • UART init (generic): uart_init(baud, data_bits, parity, stop_bits)
  • SPI transfer (master): spi_select(slave); spi_write(byte[]); read = spi_transfer(byte); spi_deselect(slave)
  • I2C single write (master): i2c_start(); i2c_write(addr<<1 | 0); i2c_write(reg); i2c_write(data); i2c_stop();
  • CAN send (pseudo): can_tx_prepare(id, data[], dlc); can_tx_request();
  • Modbus RTU send (master): send([slave, func, data...]); append_crc16(); wait_for_reply_or_timeout();

Worked examples

  1. UART baud divisor (numeric, typical MCU model using oversampling):

    • Given: f_uartclk = 16.000 MHz, desired baud = 115200, oversampling = 16.
    • Formula: divisor = f_uartclk / (oversampling × baud).
    • Numbers: divisor = 16e6 / (16 × 115200) = 16e6 / 1,843,200 = 8.680555....
    • The peripheral usually takes an integer divisor. Choose 9 → actual baud = 16e6 / (16 × 9) = 111,111.11 bps.
    • Error = (111111.11 − 115200) / 115200 × 100% = −3.55% (too low). If divisor 8 → actual baud = 125,000 → +8.51% error. So divisor=9 is better but still outside many UART tolerances (commonly ±2–3%). Answer: choose divisor=9, actual baud ≈ 111,111 bps (error −3.55%).
  2. SPI clock example (simple numeric):

    • Given: f_master = 48 MHz, peripheral supports divider = 16.
    • SCLK = 48e6 / 16 = 3e6 Hz. Answer: SCLK = 3 MHz.
  3. CAN arbitration step-by-step (conceptual trace):

    • Two nodes attempt to send simultaneously. Node A sends ID = 0x300 (binary starts with 0...); Node B sends ID = 0x400.
    • Both transmit MSB first. On the first bit where Node A drives a dominant 0 and Node B transmits a recessive 1, the bus is dominant (0).
    • Node B samples bus and sees a level different from what it sent (it sent 1 but sees 0) → Node B loses arbitration and stops transmitting, leaving Node A to continue.
    • Because identifiers are part of arbitration, lower numeric ID wins. Draw: show bit-by-bit levels and mark where one node drops out.

Common mistakes

  • In SPI, mixing up CPOL/CPHA meaning — always draw clock and data edges to check mode.
  • Thinking I2C lines are driven high — they are pulled high by resistors; devices only pull low.
  • For CAN, assuming higher numeric ID has higher priority — opposite: lower ID wins because dominant bits (0) override recessive (1).
  • In Modbus RTU, ignoring the 3.5 character silent interval used to detect frame boundaries.

For your exam

Question types you will see: definitions, short notes, frame/diagram drawing, timing derivations, small numerical calculations (baud/divider, SPI frequency, I2C rise-time estimate), protocol traces (CAN arbitration, UART framing), simple code/pseudocode to configure a peripheral, and error-detection calculation descriptions.

What full-mark answers include:

  • Clear definitions of the protocol and assumptions.
  • Labeled diagram where required (show wires, signals and sample points, or frame field byte order).
  • Exact formulas used and units.
  • Stepwise arithmetic with units carried through and final numerical answer with correct units.
  • Mention error margins or practical considerations (e.g., pull-up resistor value trade-offs for I2C).

Quick check

  1. In UART 8N1 at 9600 baud, how many bits are transmitted for each byte and what is the time to transmit one byte?
  2. In I2C, what sequence of SDA/SCL transitions indicates a START condition?
  3. In CAN arbitration who wins between ID 0x1A and ID 0x3F and why? Answers: 1) 10 bits per byte (1 start + 8 data + 1 stop); time = 10 / 9600 ≈ 1.0417 ms. 2) START: SDA falls while SCL is high. 3) ID 0x1A wins because lower numeric identifier has higher priority (dominant bits = 0 win).

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?