Serial communication: UART, SPI and I2C
UART, SPI and I2C compared: framing, wiring, modes, addressing and electrical layers, with baud-register, baud-error, transfer-time and I2C pull-up calculations.
Drafted with Aria, reviewed by the AiCanCode.org team. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
A mechatronic board is a set of chips that must talk: the MCU reads an IMU over I2C, writes a display or an SD card over SPI, and sends data to a PC, a GSM modem or a Modbus network over a UART. Choosing the right bus, configuring its speed and estimating how long a transfer takes are everyday design tasks — and wrong baud or clock settings are among the most common bring-up failures.
Key ideas
Serial vs parallel, synchronous vs asynchronous. Serial links send one bit at a time over few wires. Synchronous links (SPI, I2C) carry a clock line, so the receiver samples on clock edges. Asynchronous links (UART) have no clock; both ends must agree on the bit rate beforehand.
UART (Universal Asynchronous Receiver-Transmitter).
- Wires: TX, RX (crossed between devices) and a common ground. Full-duplex, point-to-point.
- Frame: idle line is high (1). One start bit (0), 5–9 data bits sent LSB first, optional parity bit (even/odd), then 1 or 2 stop bits (1). The common format 8N1 uses 10 bits per byte.
- The receiver detects the falling edge of the start bit and samples each bit near its centre, usually with 16× oversampling. Because it resynchronises on every start bit, a total clock mismatch of about 2–3 % between the two ends is tolerable; more gives framing errors.
- Baud rate = symbols per second; for a two-level UART it equals bits per second.
- Electrical layers: TTL/CMOS levels on the board; RS-232 (±3 to ±15 V, inverted) to PCs; RS-485 (differential, multi-drop, up to about 1200 m) for industrial networks such as Modbus RTU.
SPI (Serial Peripheral Interface).
- Four lines: SCLK (from master), MOSI (master out, slave in), MISO (master in, slave out), and one active-low CS/SS per slave.
- Each transfer is an exchange: master and slave shift registers swap contents, so SPI is inherently full-duplex — to read a byte the master must clock out a (dummy) byte.
- Clock polarity and phase (CPOL, CPHA) give four modes 0–3; master and slave must use the same mode.
- No addressing, no acknowledge, push-pull drivers → clock rates of tens of MHz, but one CS pin per slave (or a daisy chain) and no built-in error checking.
I2C (Inter-Integrated Circuit).
- Two lines, SDA and SCL, both open-drain with pull-up resistors, shared by many devices; half-duplex.
- Every transfer begins with a START (SDA falls while SCL is high) and ends with a STOP (SDA rises while SCL is high). The master sends a 7-bit address plus a R/W bit; the addressed slave pulls SDA low in the 9th clock to ACK. Each data byte is likewise followed by an ACK/NACK, so a byte costs 9 clocks.
- Speeds: standard 100 kHz, fast 400 kHz, fast-plus 1 MHz (high-speed 3.4 MHz on some parts).
- Open-drain wiring allows clock stretching (a slow slave holds SCL low) and multi-master arbitration (a master that sends 1 but sees 0 backs off).
- Pull-up value: too small and devices cannot pull the line low; too large and the RC rise time with bus capacitance is too slow.
Choosing. UART for point-to-point links and long cables through RS-485; SPI for fast, short, few-device links (displays, flash, ADCs); I2C for many slow devices with only two pins (sensors, EEPROMs, RTCs).
Formulas
T_bit = 1 / baud
- T_bit = bit time (s); baud = bit rate for a two-level UART (bit/s).
t_frame = n_frame / baud, with n_frame = 1 + n_data + n_parity + n_stop
- For 8N1, n_frame = 10. Time to send one character (s).
UBRR = f_osc / (16 × baud) − 1 (AVR, normal speed)
- UBRR = baud-rate register (integer, rounded); f_osc = CPU clock (Hz). Actual baud = f_osc / (16 × (UBRR + 1)).
Error % = (baud_actual − baud_desired) / baud_desired × 100
TH1 = 256 − f_osc / (384 × baud) (classic 8051, Timer 1 mode 2, SMOD = 0)
- That is why 11.0592 MHz crystals are used: they give integer reload values.
t_I2C ≈ (9 × n_bytes) / f_SCL
- Approximate I2C transfer time (s); n_bytes includes address bytes; START/STOP add a little more.
t_SPI = 8 × n_bytes / f_SCLK
R_p,min = (V_DD − V_OL,max) / I_OL and R_p,max = t_r / (0.8473 × C_b)
- I2C pull-up limits (Ω): V_OL,max = 0.4 V, I_OL = sink current (3 mA in standard/fast mode), t_r = maximum rise time (1000 ns standard, 300 ns fast), C_b = bus capacitance (F).
Worked examples
Example 1 (standard). An ATmega at 16 MHz must talk at 9600 baud, 8N1. Find UBRR, the baud error and the time to send 100 bytes.
UBRR = 16 × 10⁶ / (16 × 9600) − 1 = 104.17 − 1 = 103.17→ 103.baud_actual = 16 × 10⁶ / (16 × 104) = 9615.4 baud.Error = (9615.4 − 9600) / 9600 × 100 = 0.16 %— acceptable.- Bits = 100 × 10 = 1000;
t = 1000 / 9600 = 104.2 ms.
Answer: UBRR = 103, error 0.16 %, 104.2 ms for 100 bytes.
Example 2 (GATE level). An MCU reads 6 data bytes from an accelerometer. Over I2C at 400 kHz the sequence is: START, address+W, register address, repeated START, address+R, 6 data bytes, STOP. Over SPI at 8 MHz it is one command byte followed by 6 data bytes. Compare transfer times (ignore the START/STOP bit times). Also find I2C pull-up limits for a 3.3 V bus with C_b = 400 pF in standard mode.
- I2C bytes = 3 (addr+W, register, addr+R) + 6 = 9 bytes, each 9 clocks → 81 clocks.
t_I2C = 81 / 400 × 10³ = 202.5 µs.- SPI: 7 bytes × 8 = 56 clocks →
t_SPI = 56 / 8 × 10⁶ = 7 µs. - Ratio ≈ 202.5 / 7 ≈ 29.
R_p,min = (3.3 − 0.4) / 3 × 10⁻³ = 967 Ω.R_p,max = 1000 × 10⁻⁹ / (0.8473 × 400 × 10⁻¹²) = 2.95 kΩ.
Answers: I2C ≈ 202.5 µs, SPI = 7 µs (≈ 29× faster); pull-up between about 0.97 kΩ and 2.95 kΩ (e.g. 2.2 kΩ).
Common mistakes
- Assuming 8 bits per byte on a UART — 8N1 needs 10 bit times.
- Crossing up the wiring: UART TX must go to the other device's RX; SPI MOSI goes to MOSI.
- Ignoring baud error: at 16 MHz, 115 200 baud gives UBRR ≈ 8 with −3.5 % error, which is unreliable — use double-speed mode or a "baud" crystal.
- Mismatched SPI mode (CPOL/CPHA), giving data shifted by one bit.
- Forgetting the I2C pull-ups, or using the 8-bit "address + R/W" byte where a 7-bit address is expected.
- Connecting RS-232 (±12 V) directly to a 3.3 V MCU pin.
For GATE ME
Serial communication appears as concept MCQs (synchronous vs asynchronous, number of wires, full vs half duplex, addressing, START/STOP, start/stop bits, parity) and numericals on bit time, frame time, transfer time and baud-rate register values. Practise counting bits per frame correctly and checking the error of a rounded baud divisor.
Quick check
- How many bit times does one 8E1 character take?
- Which bus needs one chip-select line per slave?
- Why must I2C lines have pull-up resistors?
- What 8051 TH1 value gives 9600 baud at 11.0592 MHz?
- What is the bit time at 115 200 baud?
Answers: 1. 11 (start + 8 data + parity + stop) 2. SPI 3. The drivers are open-drain and can only pull low 4. 253 (0xFD) 5. ≈ 8.68 µs
Interview questions
All Microcontrollers, PLC and Industrial Automation interview questionsTry answering each one aloud before you open it.
1.What is UART and how does it work?Concept
UART stands for Universal Asynchronous Receiver-Transmitter. It is a hardware communication protocol that uses asynchronous serial communication with configurable speed. UART transmits data between devices by converting parallel data from a controlling device into serial form, transmitting it, and then converting it back to parallel form at the receiving end. It uses start and stop bits to signify the beginning and end of a data packet, and does not require a clock signal for synchronization.
2.Explain the SPI communication protocol.Concept
SPI, or Serial Peripheral Interface, is a synchronous serial communication protocol used for short-distance communication, primarily in embedded systems. It operates in full-duplex mode, allowing simultaneous data transmission and reception. SPI uses a master-slave architecture with a single master and one or more slaves. It requires four wires: MOSI (Master Out Slave In), MISO (Master In Slave Out), SCLK (Serial Clock), and SS (Slave Select). The master generates the clock signal, and data is exchanged based on this clock.
3.Describe the I2C communication protocol and its typical use cases.Concept
I2C, or Inter-Integrated Circuit, is a synchronous, multi-master, multi-slave, packet-switched, single-ended, serial communication bus. It uses two bidirectional open-drain lines, SDA (Serial Data Line) and SCL (Serial Clock Line), pulled up with resistors. I2C is commonly used for communication between microcontrollers and peripheral devices like sensors, displays, and EEPROMs. It supports multiple devices on the same bus, identified by unique addresses, and is suitable for applications requiring low-speed communication over short distances.
4.Why is SPI preferred over I2C in high-speed applications?Application
SPI is preferred over I2C in high-speed applications because it operates in full-duplex mode, allowing simultaneous data transmission and reception, which increases data throughput. Additionally, SPI does not have the overhead of start and stop conditions or addressing, as seen in I2C, which makes it faster. The clock speed in SPI can also be higher than in I2C, allowing for quicker data transfer rates. However, SPI requires more pins, which can be a limitation in some designs.
5.What happens if the baud rate is mismatched in UART communication?Application
If the baud rate is mismatched in UART communication, the data received will be incorrect or garbled. This is because UART relies on timing to determine when to sample the incoming data bits. A mismatch in baud rate means that the timing for sampling the bits will be off, leading to incorrect interpretation of the data. It is crucial for both the transmitting and receiving devices to be set to the same baud rate to ensure accurate communication.
6.How does the master device select a specific slave device in SPI communication?Application
In SPI communication, the master device selects a specific slave device using the Slave Select (SS) line. Each slave device has its own SS line connected to the master. To communicate with a particular slave, the master pulls the corresponding SS line low, which enables the slave. All other SS lines remain high, keeping the other slaves inactive. This allows the master to communicate with one slave at a time.
7.Calculate the maximum data rate for an I2C bus operating at a standard mode frequency of 100 kHz.Numerical
In standard mode, I2C operates at a frequency of 100 kHz. Since I2C is a synchronous protocol, the maximum data rate is determined by the clock frequency. In standard mode, the maximum data rate is 100 kbps (kilobits per second), as each clock cycle can transfer one bit of data. This does not account for protocol overhead such as start/stop conditions and addressing.
8.What are the advantages of using I2C over UART for connecting multiple devices?Application
A UART link is point-to-point, so each extra device needs another UART or external multiplexing. I2C is a shared two-wire bus: up to about a hundred devices with distinct 7-bit addresses sit on the same SDA and SCL lines, each byte is acknowledged, and open-drain wiring allows multiple masters and clock stretching. The trade-offs are short board-level distances, modest speeds (100 kHz to 1 MHz typically) and the need for correctly sized pull-up resistors.
9.Explain how data integrity is maintained in UART communication.Concept
Data integrity in UART communication is maintained through the use of start and stop bits, as well as optional parity bits. The start bit signals the beginning of a data packet, while the stop bit indicates its end. Parity bits, if used, provide a simple error-checking mechanism by ensuring that the number of set bits (1s) is even or odd, depending on the parity configuration. If the parity does not match, an error is detected, indicating potential data corruption.
10.If an SPI bus operates with a clock frequency of 10 MHz, what is the maximum data transfer rate?Numerical
In SPI communication, the maximum data transfer rate is equal to the clock frequency because data is transferred on each clock cycle. Therefore, if the SPI bus operates with a clock frequency of 10 MHz, the maximum data transfer rate is 10 Mbps (megabits per second). This assumes that the system is operating in full-duplex mode, allowing simultaneous transmission and reception of data.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?