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
- Asynchronous: No shared clock signal; uses start/stop bits
- Point-to-point: Direct connection between two devices
- Simple: Easy to implement, low overhead
- Configurable: Baud rate, parity, stop bits
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
- Hot-pluggable: Connect/disconnect without rebooting
- Power delivery: Can provide up to 100W (USB-C PD)
- Device classes: HID, Mass Storage, Audio, Video
- Hub support: Connect up to 127 devices
How USB Works
USB Enumeration Process:
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
- Industrial automation (PLCs, sensors)
- Networking equipment (routers, switches)
- Scientific instruments
- Legacy systems
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):
Ethernet
Definition: A family of networking technologies for local area networks (LANs).
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
- Physical Layer: Cables, connectors, signaling
- Data Link Layer: MAC addresses, frame formatting
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)
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
HTTP (HyperText Transfer Protocol)
Definition: Application-layer protocol for transmitting hypermedia documents and powering the World Wide Web.
Request-Response Model
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
- 1xx (Informational): Request received, continuing process
- 2xx (Success): 200 OK, 201 Created, 204 No Content
- 3xx (Redirection): 301 Moved Permanently, 304 Not Modified
- 4xx (Client Error): 400 Bad Request, 401 Unauthorized, 404 Not Found
- 5xx (Server Error): 500 Internal Server Error, 503 Service Unavailable
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
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
- Retained Messages: Broker stores last message for new subscribers
- Last Will and Testament (LWT): Automatic message on unexpected disconnect
- Clean Session: Control whether broker remembers subscriptions
- Keep Alive: Periodic ping to maintain connection
MQTT Connection Flow
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
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
I2C (Inter-Integrated Circuit)
Definition: Two-wire serial communication protocol for short-distance communication between microcontrollers and peripherals.
I2C Bus Architecture
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
- SDA (Serial Data): Bidirectional data line
- SCL (Serial Clock): Clock signal from master
- Pull-up Resistors: Required on both lines (typically 4.7kΩ)
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
- Multi-Master: Multiple masters can control the bus (with arbitration)
- Clock Stretching: Slave can hold SCL low to slow down master
- Hot-Swapping: Some devices support connection/disconnection while powered
- Simple Wiring: Only 2 wires + ground needed
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 |