In the previous article, we saw how a microcontroller reads the physical world through GPIO and ADC, and controls it with DAC and PWM. However, as soon as your project grows, the “one pin per signal” approach hits a wall quickly.
Imagine building an environmental monitoring station equipped with a temperature-humidity-pressure sensor (BME280), a GPS module, a real-time clock (RTC), a color TFT display, and an SD card for data logging. If every bit of data were sent in parallel over separate wires, you would run out of microcontroller pins before even half the components were connected.
This is where serial communication comes in: transmitting data bit by bit sequentially over just one to four signal wires. In the embedded world, nearly 90% of chip-to-chip and sensor interactions rely on three primary protocols: UART, I2C, and SPI. Understanding how all three work—along with their strengths and pitfalls—is an essential skill so your circuit doesn’t just light up on the bench, but stays reliable and easy to debug.
Core Concept: Synchronous vs. Asynchronous #
Before dissecting each protocol, there is one fundamental distinction that defines how they operate: is there a separate clock line or not?
- Asynchronous Communication: Has no shared clock wire. The transmitter and receiver must agree in advance on the bit transmission speed (baud rate). Example: UART.
- Synchronous Communication: Uses a dedicated wire for a clock signal generated by the primary device (controller). The receiver simply samples data whenever the clock edge rises or falls, making speeds far more flexible and tolerant of oscillator drift. Examples: I2C and SPI.
1. UART (Universal Asynchronous Receiver-Transmitter) #
UART is the oldest communication protocol yet still the one you encounter most often every day—from the Serial Monitor window used for debugging on your computer, to GPS modules, 4G/NB-IoT cellular modems, smart HMI displays (like Nextion), and direct communication between two microcontrollers.
How It Works and Wiring #
UART is point-to-point (directly connecting only two devices) and full-duplex (both sides can transmit and receive simultaneously). It requires only two main signal wires plus a ground reference:
- TX (Transmit): Sends data out.
- RX (Receive): Receives incoming data.
- GND: Shared voltage reference.
The golden wiring rule is always crossed: TX on device A connects to RX on device B, and RX on device A connects to TX on device B.
Because there is no clock line, UART wraps each chunk of data (typically 8 bits / 1 byte) into a frame packet, commonly written in standard notation such as 8N1:
- Start Bit (1 LOW bit): Signals to the receiver that data transmission is beginning.
- Data Bits (8 bits): The actual binary payload sent one bit at a time.
- Parity Bit (Optional —
Nmeans None): A simple extra bit for basic transmission error detection. - Stop Bit (1 HIGH bit): Marks the end of a frame and returns the line to its idle state (HIGH).
For the receiver to know exactly when to sample each bit, both devices must use the exact same Baud Rate (bits per second)—for example 9600, 115200, or 921600 bps. If they drift apart by more than roughly 2–3%, received data turns into unreadable garbage characters.
Important: Don’t Confuse UART with RS-232 and RS-485! A microcontroller’s TX/RX pins output UART TTL signals (0V and 3.3V/5V), which are only suitable for short distances (between ICs on a PCB or cables under 1–2 meters). For tens to thousands of meters in noisy industrial environments, the same UART protocol is simply routed through a transceiver IC (such as the MAX485) to convert it into differential RS-485 signaling.
2. I2C (Inter-Integrated Circuit / $\text{I}^2\text{C}$) #
Developed by Philips (now NXP), I2C was designed to solve one specific problem: how to connect many sensors and ICs on a single PCB using the fewest possible pins.
How It Works and Wiring #
I2C is a synchronous, half-duplex (transmit and receive take turns) bus that needs only 2 wires to talk to dozens of devices at once:
- SDA (Serial Data): Bidirectional line for sending and receiving data.
- SCL (Serial Clock): Clock line driven by the Controller (Master).
How does the microcontroller know which sensor it is talking to when all of them share the same two wires? The answer is a unique address.
Every Target (Slave) device on an I2C bus has a 7-bit address (for example, 0x3C for an SSD1306 OLED display, 0x76 for a BME280 sensor, or 0x68 for an MPU6050 IMU). When communication begins:
- The Controller sends a START condition, then broadcasts the 7-bit target address followed by 1 direction bit (Read or Write).
- Every device on the bus listens, but only the device whose address matches replies with an ACK (Acknowledge) signal.
- Data is exchanged byte by byte—and every single byte is confirmed with an ACK/NACK bit, so the sender immediately knows if the target device is dead or unresponsive.
- The transaction ends with a STOP condition.
I2C’s Hardware Secret: Open-Drain Architecture and Pull-Up Resistors #
Remember the Open-Drain mode from Article 3? I2C is its quintessential real-world application. The SDA and SCL pins on all I2C devices are open-drain—meaning they can only pull the line down to GND (LOW), never actively drive it to $V_{CC}$ (HIGH). This prevents short circuits even if two devices accidentally access the bus at the same time.
The consequence: an I2C bus strictly requires two external pull-up resistors (one from SDA to $V_{CC}$, one from SCL to $V_{CC}$) so the lines can return to logic HIGH when released. The most common value for 3.3V systems is $4.7\text{ k}\Omega$ (or $2.2\text{ k}\Omega$ for higher speeds or slightly longer traces/cables).
- Standard I2C Speeds: Standard Mode (100 kHz), Fast Mode (400 kHz), up to Fast Mode Plus (1 MHz).
- Limitations: Although a 7-bit address space theoretically supports over 100 devices, physically the number of devices is limited by total bus capacitance (maximum 400 pF). The more devices and the longer the wires, the more the square wave rounds off (like a shark fin) until communication fails.
3. SPI (Serial Peripheral Interface) #
If UART wins on simplicity and I2C wins on pin efficiency, then SPI (developed by Motorola) is the king of speed. It is used whenever data must move very fast without pauses—such as pushing frames to a color TFT screen, reading/writing an SD card, accessing external Flash memory, or sampling a high-speed ADC.
How It Works and Wiring #
SPI is synchronous, full-duplex (transmit and receive happen simultaneously on every clock pulse), and uses push-pull pins so it does not require pull-up resistors. By standard, it uses 4 signal lines:
- SCLK / SCK (Serial Clock): Clock signal from the Controller.
- MOSI / COPI (Master Out Slave In / Controller Out Peripheral In): Data line from the microcontroller to the peripheral.
- MISO / CIPO (Master In Slave Out / Controller In Peripheral Out): Data line from the peripheral back to the microcontroller.
- CS / SS (Chip Select / Slave Select): Device selection pin (typically Active-Low).
Unlike I2C, which uses digital addresses inside the data stream, SPI selects devices physically via the CS pin. If you connect 3 SPI peripherals to one bus (sharing SCLK, MOSI, and MISO), you need 3 separate CS pins on the microcontroller. When the microcontroller wants to talk to device B, it pulls CS_B LOW while keeping CS_A and CS_C HIGH.
Internally, SPI works like two shift registers connected in a ring: on each clock pulse, 1 bit shifts out via MOSI while 1 bit shifts in via MISO. Because protocol overhead is practically zero (no start bits, addresses, or ACKs), clock speeds easily reach 10 MHz to 40–80 MHz depending on the chip.
Understanding SPI Modes (CPOL and CPHA) #
One SPI detail that often causes headaches is the lack of a single universal standard for when data is sampled relative to the clock. This is controlled by two parameters that define 4 SPI Modes (Mode 0, 1, 2, and 3):
- CPOL (Clock Polarity): Determines whether the idle state of SCLK is LOW (
0) or HIGH (1). - CPHA (Clock Phase): Determines whether data is sampled on the first clock edge (
0) or the second clock edge (1).
If your firmware uses Mode 0 while the sensor datasheet calls for Mode 3, the received bits will be shifted or garbled. Always check the SPI Mode table in the component’s datasheet before writing code.
Comparison Summary: UART vs. I2C vs. SPI #
| Aspect | UART | I2C ($\text{I}^2\text{C}$) | SPI |
|---|---|---|---|
| Signal Wires | 2 (TX, RX) | 2 (SDA, SCL) | 4 (SCLK, MOSI, MISO, CS) + 1 CS per additional device |
| Synchronization | Asynchronous (no clock, requires matching baud rate) | Synchronous (SCL line) | Synchronous (SCLK line) |
| Direction | Full-Duplex | Half-Duplex | Full-Duplex |
| Typical Speed | Slow – Medium (9.6 kbps – ~3 Mbps) | Medium (100 kHz, 400 kHz, 1 MHz) | Very Fast (1 MHz – 40+ MHz) |
| Number of Devices | 1-to-1 (point-to-point) | Many (multi-device via 7-bit address) | Many (limited by available CS pins) |
| Data Confirmation (ACK) | None built-in | Yes, on every byte (ACK/NACK) | None built-in |
| External Resistors | Not required | Required pull-up resistors on SDA & SCL | Not required (push-pull) |
| Popular Examples | GPS, 4G/GSM modems, debug console, Nextion displays, HC-05 Bluetooth | BME280, MPU6050, BH1750, small OLEDs, DS3231 RTC, ADS1115 | TFT/LCD displays, SD cards, external NOR Flash, LoRa modules, fast ADCs/DACs |
Practical Guide: When to Choose Which? #
- Choose UART if you are directly connecting just two devices, need simple bidirectional communication, want a PC debugging console, or need to extend communication to industrial distances via RS-485.
- Choose I2C if your microcontroller pins are limited, you need to read many low-to-medium-speed sensors (temperature, light, accelerometer, RTC) inside one board or enclosure, and you want built-in ACK confirmation that a device actually responded.
- Choose SPI if data transfer speed is your top priority (such as refreshing a graphics display, high-speed SD card logging, or high-sample-rate signal acquisition) and you have enough spare GPIO pins for CS lines.
6 Common Mistakes & Field Troubleshooting Tips #
Serial communication is the most classic source of bugs when assembling prototypes. If your data isn’t showing up or constantly reads 0xFF / 0x00, check these six things first:
- Forgetting a Common Ground (GND) Two boards with separate power supplies (for example, one powered by a laptop USB port and another by a 5V adapter) cannot communicate over UART, I2C, or SPI unless their GND lines are tied together. Without a shared 0V reference, 3.3V from device A is meaningless to device B.
- Swapped TX/RX Wires or Mismatched Baud Rate (UART)
The simplest mistake, yet even senior engineers make it: wiring TX to TX and RX to RX. If your serial console prints strange symbols like
⸮x⸮⸮or boxes, 99% of the time it is a baud rate mismatch between the firmware and the receiver. - I2C Pull-Up Resistor Issues (Too Few or Too Many!)
- Missing pull-ups: When designing your own PCB with bare sensor ICs, forgetting pull-up resistors leaves SDA/SCL unable to ever rise HIGH, locking up the bus immediately.
- Too many pull-ups: Conversely, inexpensive breakout modules usually include $10\text{ k}\Omega$ or $4.7\text{ k}\Omega$ pull-up resistors on every board. If you wire 6 modules in parallel, the combined parallel resistance drops so low that the chips’ open-drain pins can no longer pull the line down to 0V!
- I2C Address Collision
Connecting two identical sensors (for instance, two temperature sensors fixed at
0x40) to the same I2C bus causes both to reply at once and corrupt the data. Solutions: check if the sensor has an address selector pin (ADDR/AD0), use a second I2C peripheral on your microcontroller, or add an I2C Multiplexer IC (such as the TCA9548A). - SPI CS Pin Not Returned to HIGH or Wrong SPI Mode When using multiple SPI devices, ensure every idle device’s CS pin stays HIGH (even safer: add a $10\text{ k}\Omega$ pull-up resistor on each CS line so devices don’t activate simultaneously during MCU boot). If communication with a single device is still garbled, double-check CPOL/CPHA settings and temporarily lower the SPI clock frequency.
- Forcing I2C or SPI Over Long Cables I2C and SPI are designed for short distances (chip-to-chip on a single PCB or tens of centimeters of wire). Running an I2C cable 5 meters outdoors will let capacitance and electrical interference destroy the signal immediately. For long distances, switch to RS-485 or CAN bus.
Pro Troubleshooting Tips:
- For I2C, get in the habit of running an I2C Scanner script every time you wire up a new sensor—within 2 seconds you will know whether the wiring and address are detected by the microcontroller.
- If you are serious about embedded development, pick up an 8-channel USB Logic Analyzer (very affordable, often under $10) and pair it with free software like PulseView (sigrok). Seeing the actual SDA/SCL or MOSI/MISO waveforms alongside protocol decoding cuts debugging time from hours down to minutes.
Your microcontroller can now talk to the physical world via GPIO/ADC/PWM and exchange rich data with modern peripherals over UART, I2C, and SPI. However, once all those sensors, buttons, and displays come together in a single program, a new firmware challenge emerges: how do you read a sensor every 100 ms, update the screen, and respond immediately to an emergency button without tasks blocking each other and making the system feel sluggish? The key is breaking the bad habit of using delay() and mastering Timers and Interrupts—which we will dive into in Article 5.