UART vs USB vs Serial vs Ethernet


Understanding the differences between communication protocols is essential for hardware and embedded systems development.

Overview

Protocol Speed Distance Wires Use Case
UART 9600 - 115200 bps Short (< 15m) 2 (TX, RX) Microcontroller communication
USB 480 Mbps - 40 Gbps Short (< 5m) 4 (D+, D-, VCC, GND) Peripherals, data transfer
RS-232 (Serial) 115200 bps Medium (< 15m) 3+ (TX, RX, GND) Industrial equipment
Ethernet 10 Mbps - 100 Gbps Long (100m per segment) 8 (4 twisted pairs) Networking, internet

UART (Universal Asynchronous Receiver-Transmitter)

Definition: A hardware communication protocol that transmits data serially, one bit at a time.

Characteristics
graph LR MCU1[Microcontroller 1] -->|TX| RX1[RX] RX2[TX] -->|RX| MCU1 MCU2[Microcontroller 2] -->|TX| RX2 RX1 -->|RX| MCU2
Common Baud Rates
9600, 19200, 38400, 57600, 115200 bps

USB (Universal Serial Bus)

Definition: A standardized connection interface for computers and peripherals.

USB Versions
Version Speed Year Common Use
USB 1.1 12 Mbps 1998 Keyboards, mice
USB 2.0 480 Mbps 2000 External drives, cameras
USB 3.0 5 Gbps 2008 Fast storage
USB 3.1 10 Gbps 2013 High-speed transfers
USB 4.0 40 Gbps 2019 Thunderbolt compatible
Features
How USB Works

USB Enumeration Process:

sequenceDiagram participant Device participant Host Device->>Host: Device Connected (D+ pulled high) Host->>Device: Reset Signal Host->>Device: Get Device Descriptor Device->>Host: Device Descriptor (VID, PID, Class) Host->>Device: Set Address (assign unique address) Device->>Host: ACK Host->>Device: Get Configuration Descriptor Device->>Host: Configuration Descriptor Host->>Device: Get String Descriptors Device->>Host: Manufacturer, Product, Serial Host->>Device: Set Configuration Device->>Host: Ready for Data Transfer
USB Packet Structure
Token Packet:
┌──────┬──────┬─────────┬──────────┬─────┐
│ SYNC │ PID  │ Address │ Endpoint │ CRC │
└──────┴──────┴─────────┴──────────┴─────┘

Data Packet:
┌──────┬──────┬──────────┬─────┐
│ SYNC │ PID  │ Data     │ CRC │
└──────┴──────┴──────────┴─────┘

Handshake Packet:
┌──────┬──────┐
│ SYNC │ PID  │
└──────┴──────┘

PID Types: OUT, IN, SOF, SETUP, DATA0, DATA1, ACK, NAK, STALL
USB Transfer Types
Type Speed Error Detection Use Case
Control Slow Yes (CRC) Device configuration
Bulk Fast Yes (CRC) Large data (printers, storage)
Interrupt Medium Yes (CRC) Keyboards, mice (low latency)
Isochronous Guaranteed bandwidth No Audio, video streaming

RS-232 (Serial Communication)

Definition: A standard for serial communication with voltage levels between -15V and +15V.

Signal Levels
Logic 1 (Mark): -3V to -15V
Logic 0 (Space): +3V to +15V
Applications
How RS-232 Works

Frame Format:

RS-232 Data Frame (8N1 configuration):
┌───────┬────┬────┬────┬────┬────┬────┬────┬────┬──────┐
│ Start │ D0 │ D1 │ D2 │ D3 │ D4 │ D5 │ D6 │ D7 │ Stop │
│  Bit  │    │    │    │    │    │    │    │    │ Bit  │
└───────┴────┴────┴────┴────┴────┴────┴────┴────┴──────┘
  (0)    LSB                              MSB    (1)

Timing: Each bit lasts 1/baud_rate seconds
Example at 9600 baud: 1 bit = 104 microseconds
RS-232 Signal Lines
Signal Pin Direction Purpose
TXD 2 DTE → DCE Transmit Data
RXD 3 DCE → DTE Receive Data
RTS 7 DTE → DCE Request To Send (flow control)
CTS 8 DCE → DTE Clear To Send (flow control)
GND 5 - Signal Ground

Hardware Flow Control (RTS/CTS):

sequenceDiagram participant DTE as Computer (DTE) participant DCE as Modem (DCE) DTE->>DCE: RTS High (Ready to send) DCE->>DTE: CTS High (Ready to receive) DTE->>DCE: Data transmission DCE->>DTE: CTS Low (Buffer full, pause) Note over DTE: Stops sending DCE->>DTE: CTS High (Buffer cleared) DTE->>DCE: Resume data transmission

Ethernet

Definition: A family of networking technologies for local area networks (LANs).

graph TB Router[Router/Switch] PC1[Computer 1] PC2[Computer 2] Server[Server] Printer[Network Printer] Router --- PC1 Router --- PC2 Router --- Server Router --- Printer
Ethernet Standards
Standard Speed Cable Type Max Distance
10BASE-T 10 Mbps Cat 3 100m
100BASE-TX 100 Mbps Cat 5 100m
1000BASE-T 1 Gbps Cat 5e/6 100m
10GBASE-T 10 Gbps Cat 6a/7 100m
OSI Model Layers
How Ethernet Works

Ethernet Frame Structure (IEEE 802.3):

Ethernet II Frame:
┌──────────┬─────────┬──────────┬─────────┬──────────┬─────┬─────┐
│ Preamble │   SFD   │   Dest   │  Source │   Type   │Data │ FCS │
│  (7 B)   │  (1 B)  │ MAC (6B) │ MAC(6B) │  (2 B)   │     │(4B) │
└──────────┴─────────┴──────────┴─────────┴──────────┴─────┴─────┘

Preamble: 10101010... (synchronization)
SFD: 10101011 (Start Frame Delimiter)
Dest MAC: Destination hardware address
Source MAC: Source hardware address
Type: Protocol type (0x0800 = IPv4, 0x0806 = ARP)
Data: 46-1500 bytes
FCS: Frame Check Sequence (CRC-32)
CSMA/CD (Carrier Sense Multiple Access with Collision Detection)
graph TD Start([Want to Send Data]) --> Listen{Is line idle?} Listen -->|No| Wait[Wait random time] Wait --> Listen Listen -->|Yes| Send[Start Transmission] Send --> Detect{Collision
Detected?} Detect -->|Yes| Jam[Send Jam Signal] Jam --> Backoff[Random Backoff] Backoff --> Listen Detect -->|No| Complete[Transmission Complete] Complete --> End([Done])
MAC Address Format
MAC Address: 48 bits (6 bytes)
Format: XX:XX:XX:XX:XX:XX (hexadecimal)

Example: 00:1A:2B:3C:4D:5E

┌─────────────────┬─────────────────┐
│ OUI (24 bits)   │ NIC (24 bits)   │
│ Manufacturer ID │ Device Specific │
└─────────────────┴─────────────────┘

Special Addresses:
FF:FF:FF:FF:FF:FF = Broadcast (all devices)
01:00:5E:XX:XX:XX = IPv4 Multicast
33:33:XX:XX:XX:XX = IPv6 Multicast
Ethernet Communication Flow
sequenceDiagram participant PC1 as Computer A participant Switch participant PC2 as Computer B PC1->>Switch: Frame (Dest MAC: PC2) Note over Switch: Checks MAC table Switch->>PC2: Forward frame to port PC2->>PC2: Verify destination MAC PC2->>Switch: Response frame Switch->>PC1: Forward response

HTTP (HyperText Transfer Protocol)

Definition: Application-layer protocol for transmitting hypermedia documents and powering the World Wide Web.

Request-Response Model
sequenceDiagram participant Client participant Server Client->>Server: HTTP Request (GET /api/users) Server->>Server: Process Request Server->>Client: HTTP Response (200 OK + Data)
HTTP Methods
Method Purpose Idempotent Safe
GET Retrieve data Yes Yes
POST Create resource No No
PUT Update/Replace resource Yes No
PATCH Partial update No No
DELETE Remove resource Yes No
HTTP Request Structure
GET /api/products/123 HTTP/1.1
Host: api.example.com
User-Agent: Mozilla/5.0
Accept: application/json
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...

[Optional Request Body]
HTTP Response Structure
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 145
Cache-Control: max-age=3600

{
  "id": 123,
  "name": "Laptop",
  "price": 999.99,
  "stock": 42
}
Status Code Categories
HTTP Versions Comparison
Version Key Feature Performance
HTTP/1.1 Persistent connections, pipelining Sequential requests
HTTP/2 Multiplexing, server push, header compression Parallel requests over single connection
HTTP/3 QUIC protocol (UDP-based) Faster, better for lossy networks

MQTT (Message Queuing Telemetry Transport)

Definition: Lightweight publish-subscribe messaging protocol designed for IoT devices with limited bandwidth.

Publish-Subscribe Model
graph LR Publisher1[Temperature Sensor] -->|Publish| Broker[MQTT Broker] Publisher2[Humidity Sensor] -->|Publish| Broker Broker -->|Subscribe| Sub1[Dashboard] Broker -->|Subscribe| Sub2[Alert System] Broker -->|Subscribe| Sub3[Data Logger]
MQTT Topics
// Topic hierarchy
home/livingroom/temperature
home/livingroom/humidity
home/bedroom/temperature
home/bedroom/light/status

// Wildcards
home/+/temperature        // + matches single level
home/#                    // # matches multiple levels

// Example publish
mosquitto_pub -h broker.example.com -t "home/livingroom/temp" -m "22.5"

// Example subscribe
mosquitto_sub -h broker.example.com -t "home/#"
Quality of Service (QoS) Levels
QoS Guarantee Use Case
0 At most once (fire and forget) Non-critical sensor data
1 At least once (acknowledged) Important messages, duplicates OK
2 Exactly once (guaranteed) Critical commands, billing data
MQTT Features
MQTT Connection Flow
sequenceDiagram participant Client participant Broker Client->>Broker: CONNECT (with credentials) Broker->>Client: CONNACK (connection accepted) Client->>Broker: SUBSCRIBE (topic: sensors/temp) Broker->>Client: SUBACK (subscription confirmed) Client->>Broker: PUBLISH (topic: sensors/temp, payload: 23.5) Broker->>Client: PUBACK (QoS 1) Note over Client: Disconnect Client->>Broker: DISCONNECT

Modbus

Definition: Industrial communication protocol for connecting PLCs, sensors, and other automation devices.

Modbus Variants
Variant Transport Use Case
Modbus RTU Serial (RS-232/RS-485) Direct device connection
Modbus ASCII Serial (human-readable) Debugging, legacy systems
Modbus TCP Ethernet/IP Networked industrial systems
Master-Slave Architecture
graph TD Master[Modbus Master
PLC/SCADA] --> Slave1[Slave 1
Temperature Sensor] Master --> Slave2[Slave 2
Motor Controller] Master --> Slave3[Slave 3
Flow Meter] Master --> Slave4[Slave 4
Valve Actuator]
Common Function Codes
Code Function Description
01 Read Coils Read 1-2000 digital outputs
02 Read Discrete Inputs Read 1-2000 digital inputs
03 Read Holding Registers Read 1-125 analog outputs
04 Read Input Registers Read 1-125 analog inputs
05 Write Single Coil Write single digital output
06 Write Single Register Write single analog output
15 Write Multiple Coils Write multiple digital outputs
16 Write Multiple Registers Write multiple analog outputs
Modbus RTU Frame Structure
Modbus RTU Frame:
┌──────────┬──────────┬─────────┬──────┬─────┐
│ Slave ID │ Function │ Data    │ CRC  │     │
│ (1 byte) │ (1 byte) │ (N bytes)│(2 B) │     │
└──────────┴──────────┴─────────┴──────┴─────┘

Example - Read 10 holding registers starting at address 100:
Slave ID: 01
Function: 03 (Read Holding Registers)
Start Address: 00 64 (100 in hex)
Quantity: 00 0A (10 registers)
CRC: XX XX (calculated)

Request: 01 03 00 64 00 0A [CRC]
Response: 01 03 14 [20 bytes of data] [CRC]
Modbus Communication Example
sequenceDiagram participant Master as Master (PLC) participant Slave as Slave (Sensor) Master->>Slave: Read Holding Register (Addr: 100) Slave->>Master: Response (Value: 2350 = 23.5°C) Master->>Slave: Write Coil (Addr: 50, Value: ON) Slave->>Master: ACK (Coil set to ON)

I2C (Inter-Integrated Circuit)

Definition: Two-wire serial communication protocol for short-distance communication between microcontrollers and peripherals.

I2C Bus Architecture
graph LR Master[Microcontroller
Master] -->|SDA| Bus[I2C Bus] Master -->|SCL| Bus Bus --> Slave1[EEPROM
0x50] Bus --> Slave2[RTC
0x68] Bus --> Slave3[Temp Sensor
0x48] Bus --> Slave4[LCD Display
0x27]
I2C Signals
I2C Addressing
Mode Address Bits Max Devices
7-bit 7 bits (0x00-0x7F) 128 devices
10-bit 10 bits (0x000-0x3FF) 1024 devices
I2C Communication Protocol
I2C Transaction:
┌───────┬─────────┬────┬──────┬────┬──────┬────┬──────┐
│ START │ Address │ R/W│ ACK  │Data│ ACK  │... │ STOP │
└───────┴─────────┴────┴──────┴────┴──────┴────┴──────┘

START: SDA falls while SCL is high
STOP: SDA rises while SCL is high
ACK: Receiver pulls SDA low
NACK: Receiver leaves SDA high

Example - Write to EEPROM (Address 0x50):
START | 0x50 (Write) | ACK | 0x00 (Mem Addr) | ACK | 0xFF (Data) | ACK | STOP
I2C Speed Modes
Mode Speed Use Case
Standard 100 kbit/s Basic sensors, EEPROMs
Fast 400 kbit/s Most common mode
Fast Plus 1 Mbit/s High-speed sensors
High Speed 3.4 Mbit/s Specialized applications
Arduino I2C Example
#include <Wire.h>

void setup() {
  Wire.begin();  // Join I2C bus as master
}

void loop() {
  // Write to device at address 0x48
  Wire.beginTransmission(0x48);
  Wire.write(0x01);  // Register address
  Wire.write(0xA0);  // Data to write
  Wire.endTransmission();
  
  // Read from device at address 0x48
  Wire.beginTransmission(0x48);
  Wire.write(0x00);  // Register to read
  Wire.endTransmission();
  
  Wire.requestFrom(0x48, 2);  // Request 2 bytes
  if (Wire.available() >= 2) {
    byte msb = Wire.read();
    byte lsb = Wire.read();
    int value = (msb << 8) | lsb;
  }
  
  delay(1000);
}
I2C Features

When to Use Each Protocol

Scenario Best Choice Reason
Arduino to sensor UART Simple, low power, short distance
Connecting peripherals USB Fast, standardized, power delivery
Industrial equipment RS-232 Reliable, noise-resistant, legacy support
Network infrastructure Ethernet High speed, long distance, scalable