Communication Protocols in Hardware: I2C, UART, and SPI

My primary job is as an automotive electronics engineer. Previously, I have been working on the software side. Recently, I started learning about hardware. Here, I will document my learning content.

Starting with the MCU collecting sensor data. After studying the pin configuration of the MCU, I decided to begin with communication methods.

I²C, UART, and SPI are the three most commonly used serial communication protocols in embedded systems, each with its advantages and disadvantages, suitable for completely different scenarios.

UART is an asynchronous communication method that requires two hardwired connections, TX and RX. As indicated by the name asynchronous communication, it does not require synchronization, hence there is no clock requirement. In terms of topology, it is point-to-point communication, with a communication speed typically < 115.2 kbps, classifying it as low-speed communication. The software configuration only requires setting the baud rate.

I2C and SPI are both synchronous communications, thus requiring a clock line. The software is relatively complex.

I2C connects master and slave devices with two hardwired lines (data line SDA and clock line SCL), and multiple slave devices can be connected to these two lines simultaneously. The communication speed is relatively high, with a standard mode of 100 kbps and a fast mode of 400 kbps. However, if multiple slave devices are connected, the communication speed for a single slave device is not high.

SPI communication uses four hardwired lines (chip select line CS, master out slave in MOSI, master in slave out MISO, and a clock line SCLK) to connect master and slave devices. Multiple slave devices can share MOSI, MISO, and SCLK, with the chip select line determining which slave device is currently connected. The communication rate is high, reaching 10+ Mbps.

From the hardware configuration perspective, I2C has only one data line, indicating that I2C is a half-duplex communication method. At any given time, only one device occupies the data line.

Features

UART

I²C

SPI

Communication Method

Asynchronous

Synchronous

Synchronous

Hardware Lines

TX, RX (2 lines, full duplex)

SDA, SCL (2 lines, half duplex)

MOSI, MISO, SCLK, CS (3+N lines, full duplex)

Topology

Point-to-point

Multi-master multi-slave (Bus type)

One master multiple slaves (Bus type)

Speed

Low(typically < 115.2 kbps)

Medium(Standard mode 100 kbps, Fast mode 400 kbps)

High (up to 10+ Mbps)

Software Complexity

Simple(only need to configure baud rate)

Complex(needs to handle addresses, protocols)

Medium(needs to manage chip select)

Hardware Overhead

Low(2 lines)

Very low (2 lines + pull-up resistors)

High(3 lines + one chip select per slave)

Key Advantages

Simple, versatile, full duplex

Pin-efficient, multi-device, acknowledgment

Very fast, full duplex, simple protocol

Main Disadvantages

Slow, no addressing, short distance

Medium speed, complex software

Uses many pins, no flow control, no acknowledgment

Detailed Application Scenarios

1. UART (Universal Asynchronous Receiver/Transmitter)

Point-to-point communication.

Typical scenarios:

oDebugging and logging output: The MCU connects to a USB-to-serial chip (like CH340, CP2102) via UART to communicate with a PC and print debugging information. This is the most common use case.

oGPS modules, Bluetooth modules, Wi-Fi modules: Many of these modules use UART as a configuration and data transmission interface, controlled by sending AT commands.

oIndustrial controllers and PC/HMI: Connecting legacy industrial control devices, scanners, printers, etc.

oPoint-to-point communication between microcontrollers: When two MCUs need to exchange data simply and independently.

When to choose:

o When point-to-point communication is needed.

o When speed requirements are not high, but simplicity and versatility are required.

o When human-machine interaction is needed (like connecting to a computer terminal).

oNote: In long-distance or noisy environments, RS-485 (based on UART but with differential signaling) is usually used instead.

2. I²C (Inter-Integrated Circuit)

The master device addresses multiple slave devices and receives responses..

Typical scenarios:

oConnecting onboard sensors: This is the main battlefield for I²C. For example, temperature sensors (BMP280), humidity sensors, accelerometers/gyroscopes (MPU6050), light intensity sensors, etc. Because they usually have small data volumes and aim to occupy as few MCU pins as possible.

oAccessing small capacity memory: Such as EEPROM (24C02), used to store system configuration parameters.

oIO port expansion chips: When MCU pins are insufficient, chips with I²C interfaces (like PCA9538) can expand multiple IO ports.

oReal-time clock (RTC): Such as DS3231, providing accurate date and time.

When to choose:

o When your MCU pin resources are very tight but need to connect multiple low-speed peripherals.

o When the device is typically a “sensor/small memory,” check if it provides an I²C interface.

o When board space is tight and routing needs to be minimized.

3. SPI (Serial Peripheral Interface)

The master device simultaneously distributes commands to multiple slave devices or receives responses..

Typical scenarios:

oHigh-speed data storage: Connecting SD cards, Flash memory. This is a classic application of SPI, requiring high-speed data block transfer.

oHigh-resolution sensors: Such as ADC/DAC conversion chips, requiring high-speed, continuous transmission of collected data streams.

oDisplay interfaces: Driving OLED screens, TFT screens, requiring high-speed refresh of display data.

oDigital signal processors (DSP), audio codecs: Requiring high-speed full-duplex data transmission.

oRFID modules, Ethernet controllers etc.

When to choose:

o When the application has extremely high data transfer speed requirements.

o When full duplex (simultaneous send and receive) communication is needed.

o When you don’t mind using more MCU pins for performance.

In practical projects, it is very common to use all three protocols on a single circuit board:

· Using UART to connect to a computer for debugging.

· Using I²C to connect three or four sensors and EEPROM.

· Using SPI to connect a high-speed Flash chip and display.

The technical information in this article was partially obtained through Deepseek queries.

Leave a Comment