interview_spi_i2c_uart
interview_spi_i2c_uart
UART (Universal
I2C (Inter-Integrated SPI (Serial Peripheral
Feature Asynchronous
Circuit) Interface)
Receiver/Transmitter)
Synchronous, Multi- Synchronous, Master-
Type Asynchronous, Point-to-point
master Slave
2: SDA (Data), SCL 2: TX (Transmit), RX 4 (minimum): SCLK,
Number of Lines
(Clock) (Receive) MOSI, MISO, SS
Up to 3.4 Mbps (Fast Up to 100 Mbps or
Speed Typically up to 1 Mbps
Mode Plus) higher
Data Transfer Half-Duplex Full-Duplex Full-Duplex
Number of Multiple (up to 127 Point-to-point (2 devices Multiple (requires
Devices addresses) only) separate SS line)
Clock Required (provided by Required (provided by
Not required (uses baud rate)
Requirement master) master)
Protocol
Moderate Simple Simple
Complexity
Yes (multi-master No (peer-to-peer Yes (single master,
Master-Slave
supported) communication) multiple slaves)
No built-in
Data Integrity Uses ACK/NACK Parity bit (optional)
acknowledgment
Hardware Pull-up resistors for None (requires UART Requires more GPIO
Requirements SDA and SCL module) pins
Power Low (idle state Moderate (depends on
Low
Consumption consumes little) clock speed)
High-speed
Sensors, EEPROM, Debugging, Serial
Use Cases communication,
RTC Communication
ADC/DAC
Detailed Comparison
1. Data Transfer Mechanism
I2C: Uses two lines (SDA and SCL) and allows multiple devices to communicate on the
same bus. Each device is addressed uniquely.
UART: A point-to-point communication protocol where data is sent asynchronously without
a clock signal.
SPI: Relies on a master device generating a clock signal, with separate lines for sending and
receiving data. Each slave requires a unique chip select (SS) line.
2. Speed
I2C: Slower compared to SPI, especially in standard and fast modes (100 kbps to 400 kbps).
The Fast Mode Plus and High-Speed modes reach up to 3.4 Mbps.
UART: Limited by the baud rate, typically up to 1 Mbps.
SPI: Faster than both I2C and UART, reaching up to 100 Mbps or more.
3. Complexity
I2C: Moderately complex due to its addressing, ACK/NACK mechanism, and multi-master
arbitration.
UART: The simplest protocol, as it only requires matching baud rates for communication.
SPI: Simple but requires more GPIO pins for multiple slaves.
4. Number of Devices
I2C: Supports multiple devices on the same bus, identified by their unique 7-bit or 10-bit
addresses. A single master can communicate with up to 127 devices.
UART: Only supports two devices (point-to-point communication).
SPI: Supports multiple devices, but each slave requires a separate SS line, which increases
the number of GPIOs needed.
5. Application Scenarios
I2C: Ideal for connecting low-speed devices like sensors, EEPROMs, and RTCs where a
shared bus is advantageous.
UART: Commonly used for debugging, serial communication between microcontrollers and
computers, or GPS modules.
SPI: Suited for high-speed data transfers like ADCs, DACs, SD cards, and displays.
6. Example in Arduino
I2C Example:
cpp
Copy code
#include <Wire.h>
void setup() {
Wire.begin(); // Initialize as master
Wire.beginTransmission(0x50); // Start communication with slave
Wire.write(0x01); // Send data
Wire.endTransmission(); // Stop condition
}
void loop() {}
UART Example:
cpp
Copy code
void setup() {
Serial.begin(9600); // Initialize UART
Serial.println("Hello, UART!"); // Send data
}
void loop() {}
SPI Example:
cpp
Copy code
#include <SPI.h>
void setup() {
SPI.begin(); // Initialize SPI
digitalWrite(SS, LOW); // Select slave
SPI.transfer(0x01); // Send data
digitalWrite(SS, HIGH); // Deselect slave
}
void loop() {}
Summary
I2C: Best for low-speed, multi-device setups with minimal wiring.
UART: Simple and efficient for point-to-point communication.
SPI: Optimal for high-speed, low-latency applications, though it requires more pins for
multi-slave setups.