What are the differences between I2C and SPI protocols in Arduino?


Here’s a clear, technical comparison of I2C and SPI protocols for Arduino projects, including pros/cons and typical use cases:
1. Key Differences
Feature | I2C (Inter-Integrated Circuit) | SPI (Serial Peripheral Interface) |
Communication | 2-wire (SDA + SCL) | 4-wire (MOSI, MISO, SCK, SS) |
Speed | Up to 3.4 MHz (Fast Mode+) | 10+ MHz (depends on Arduino) |
Topology | Multi-master/multi-slave (addressable) | Single-master, daisy-chain possible |
Synchronization | Clock (SCL) required | Clock (SCK) controls data flow |
Complexity | Simpler (fewer pins) | Faster but more wiring |
2. Protocol Details
I2C
Pins:
SDA (data) + SCL (clock) (+ pull-up resistors!)
Example: BMP280 (temperature sensor), OLED displays (SSD1306).
Addressing:
- 7-bit or 10-bit slave addresses (e.g.,
0x68
for MPU6050).
- 7-bit or 10-bit slave addresses (e.g.,
Data format:
- Start bit → Slave address + R/W bit → Data → Stop bit.
SPI
Pins:
MOSI (Master Out Slave In)
MISO (Master In Slave Out)
SCK (clock)
SS/CS (Slave Select, one pin per slave)
Example: SD cards, RF24L01 (radio modules).
Data format:
Full-duplex (simultaneous send/receive).
No addressing – SS pin selects the slave.
3. Pros and Cons
Criterion | I2C | SPI |
Speed | Slower (kHz to MHz) | Very fast (MHz range) |
Pin Usage | 2 pins (shared bus) | 4+ pins (extra SS per slave) |
Scalability | Good for many slaves (addresses) | Limited by SS pins |
Noise Immunity | Sensitive to long wires | Robust (synchronous) |
Power Use | Lower (no CS pin) | Higher (active SS lines) |
4. Code Examples (Arduino)
I2C (Wire Library)
cpp
#include <Wire.h>
void setup() {
Wire.begin(); // Master
Wire.beginTransmission(0x68); // Slave address (e.g., MPU6050)
Wire.write(0x6B); // Register
Wire.write(0); // Data
Wire.endTransmission();
}
SPI (SPI Library)
cpp
#include <SPI.h>
void setup() {
SPI.begin();
digitalWrite(SS, LOW); // Activate slave
SPI.transfer(0x55); // Send data
digitalWrite(SS, HIGH);
}
5. When to Use Which?
Use I2C when:
Pin count is limited.
Many sensors (e.g., temperature, pressure) are connected.
Speed is secondary (e.g., environmental sensors).
Use SPI when:
High speed is needed (e.g., SD cards, displays).
Full-duplex communication is required.
Pin count isn’t an issue.
6. Typical Applications
I2C | SPI |
- Temp sensors (BME280) | - SPI displays (TFT) |
- EEPROMs (24LC256) | - Flash memory (W25Q128) |
- RTC modules (DS3231) | - Radio modules (nRF24L01) |
Summary
I2C: Simple, pin-efficient, but slower – ideal for sensors.
SPI: Fast, robust, but pin-heavy – best for high-speed data.
Tip: I2C is often better for multi-sensor projects, while SPI excels in speed-critical applications (e.g., graphics).
Subscribe to my newsletter
Read articles from ampheo directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
