Communication Protocols: I2C, SPI, UART, and CAN
Introduction
In embedded systems and IoT applications, microcontrollers and sensors need to communicate efficiently. Various communication protocols exist, each designed for specific use cases. The four most commonly used protocols are:
- I2C (Inter-Integrated Circuit): A two-wire protocol for connecting multiple devices.
- SPI (Serial Peripheral Interface): A high-speed protocol for fast data transfer.
- UART (Universal Asynchronous Receiver-Transmitter): A simple serial communication protocol.
- CAN (Controller Area Network): A robust protocol for automotive and industrial applications.
This article explores these protocols, their working principles, advantages, disadvantages, and real-world applications.
1. I2C (Inter-Integrated Circuit)
Overview
I2C is a two-wire serial protocol used for communication between multiple devices. It consists of:
- SCL (Serial Clock Line)
- SDA (Serial Data Line)
Features
- Multi-device support (master-slave architecture)
- Uses 7-bit or 10-bit addressing
- Speeds: 100 kHz (Standard), 400 kHz (Fast), 1 MHz (Fast Mode Plus), 3.4 MHz (High-Speed Mode)
- Can connect multiple sensors with minimal wiring
Advantages
✅ Supports multiple devices with only two wires
✅ Low power consumption
✅ Well-suited for sensor data transfer
Disadvantages
❌ Slower than SPI
❌ Requires pull-up resistors
Applications
- Temperature sensors (e.g., DHT11, BME280)
- EEPROM memory chips
- OLED displays
Example (I2C Communication with Espruino)
I2C1.setup({scl: D5, sda: D4});
var sensor = require("BME280").connect(I2C1);
console.log("Temperature: " + sensor.getTemperature() + "°C");
2. SPI (Serial Peripheral Interface)
Overview
SPI is a full-duplex communication protocol used for high-speed data exchange. It consists of four main signals:
- MOSI (Master Out Slave In)
- MISO (Master In Slave Out)
- SCK (Serial Clock)
- SS (Slave Select)
Features
- Single master, multiple slaves
- High-speed data transfer (up to 100 MHz in some cases)
- Full-duplex communication
Advantages
✅ Faster than I2C
✅ Supports multiple slaves
✅ Simple protocol structure
Disadvantages
❌ Requires more pins compared to I2C
❌ No built-in device addressing
Applications
- SD card modules
- TFT & OLED displays
- High-speed ADCs and DACs
Example (SPI Communication with Espruino)
SPI1.setup({mosi: D13, miso: D12, sck: D14});
var flash = require("AT25").connect(SPI1, D15);
console.log("Flash Memory ID: " + flash.read(0, 4));
3. UART (Universal Asynchronous Receiver-Transmitter)
Overview
UART is a simple serial communication protocol that does not require a clock signal. It uses:
- TX (Transmit)
- RX (Receive)
Features
- Asynchronous communication (no clock required)
- Configurable baud rate (e.g., 9600, 115200 bps)
- Full-duplex data exchange
Advantages
✅ Requires only two wires
✅ Simple and widely used
✅ No need for a master device
Disadvantages
❌ No built-in error checking
❌ Slower than SPI
Applications
- GPS modules
- Bluetooth & Wi-Fi modules
- Debugging via serial monitors
Example (UART Communication with Espruino)
Serial1.setup(9600, {tx: D1, rx: D2});
Serial1.on('data', function(data) {
console.log("Received: " + data);
});
Serial1.write("Hello UART");
4. CAN (Controller Area Network)
Overview
CAN is a robust communication protocol designed for vehicle and industrial automation networks. It enables multiple devices to communicate without a dedicated master.
Features
- Multi-master protocol
- High noise immunity (used in harsh environments)
- Uses differential signaling (CAN_H & CAN_L)
Advantages
✅ Highly reliable and robust
✅ Works in noisy environments
✅ Multi-master support
Disadvantages
❌ More complex than UART, I2C, and SPI
❌ Requires additional transceivers
Applications
- Automotive systems (ECU communication)
- Industrial automation (robotics, PLCs)
- Medical devices
Example (CAN Communication with Espruino)
var can = require("CAN").setup({tx: D5, rx: D6, baud: 500000});
can.send([0x01, 0x02, 0x03], 0x100);
can.on("message", function(msg) {
console.log("Received CAN message: ", msg);
});
Comparison Table
Protocol | Speed | No. of Wires | Best For |
---|---|---|---|
I2C | 400 kHz – 3.4 MHz | 2 | Sensor interfacing |
SPI | Up to 100 MHz | 4+ | High-speed devices |
UART | Up to 1 Mbps | 2 | Simple serial data transfer |
CAN | 1 Mbps | 2 (differential) | Automotive & Industrial |
Conclusion
Each communication protocol serves different purposes.
- I2C is ideal for connecting multiple low-speed sensors.
- SPI is perfect for fast data transfer.
- UART is simple and widely used in debugging and wireless modules.
- CAN is robust and used in automotive and industrial applications.
Choosing the right protocol depends on speed, wiring complexity, and application requirements. Would you like more examples or projects on these protocols? Let us know!
Read This:
Raspberry Pi Interfaces
Espruino: JavaScript on Microcontrollers
Using JavaScript with NodeMCU: A Comprehensive Guide