I2C and SPI Protocols
Two-wire vs four-wire protocols — connecting sensors, displays, and EEPROMs
Skills taught: embedded.i2c-spi
I2C: Two-Wire Interface
I2C (Inter-Integrated Circuit) uses just two wires: SDA (data) and SCL (clock). Every device on the bus has a unique 7-bit address (0x00–0x7F), enabling up to 128 devices on a single pair of wires. On Arduino Uno: SDA = A4, SCL = A5. The Wire.h library handles all I2C communication.
#include <Wire.h>
void setup() {
Wire.begin();
Serial.begin(115200);
Serial.println(F("Scanning I2C bus..."));
int found = 0;
for (byte addr = 1; addr < 127; addr++) {
Wire.beginTransmission(addr);
byte error = Wire.endTransmission();
if (error == 0) {
Serial.print(F("Found device at 0x"));
if (addr < 16) Serial.print('0');
Serial.print(addr, HEX);
// Identify common devices
if (addr == 0x3C || addr == 0x3D) Serial.print(F(" (SSD1306 OLED)"));
if (addr == 0x76 || addr == 0x77) Serial.print(F(" (BME280 pressure)"));
if (addr == 0x48) Serial.print(F(" (ADS1115 ADC or TMP75)"));
if (addr == 0x68 || addr == 0x69) Serial.print(F(" (MPU-6050 IMU)"));
Serial.println();
found++;
}
}
Serial.print(found); Serial.println(F(" device(s) found."));
}
Using an I2C Sensor: BME280
The BME280 measures temperature, humidity, and barometric pressure — all via I2C. It is one of the most popular environmental sensors and ships at address 0x76 or 0x77.
// Library: Adafruit BME280 Library
// Dependency: Adafruit Unified Sensor
#include <Adafruit_BME280.h>
#include <Wire.h>
Adafruit_BME280 bme; // uses I2C address 0x76
void setup() {
Serial.begin(115200);
Wire.begin();
if (!bme.begin(0x76)) {
Serial.println(F("Error: BME280 not found at 0x76!"));
while (1) delay(100);
}
Serial.println(F("BME280 ready"));
}
void loop() {
Serial.print(F("Temp: ")); Serial.print(bme.readTemperature(), 1); Serial.print(F("°C "));
Serial.print(F("Humidity: ")); Serial.print(bme.readHumidity(), 1); Serial.print(F("% "));
Serial.print(F("Pressure: ")); Serial.print(bme.readPressure() / 100.0F, 1); Serial.println(F(" hPa"));
delay(1000);
}
Tip: Run the I2C scanner sketch whenever you get a new I2C device — it confirms the device is wired correctly and shows its actual address. Many modules have address conflicts (two BME280s both at 0x76): change the address jumper or use a multiplexer (TCA9548A).
SPI: Four-Wire High-Speed Interface
SPI (Serial Peripheral Interface) uses four wires: MOSI (Master Out Slave In), MISO (Master In Slave Out), SCK (clock), and SS/CS (Slave Select / Chip Select). It's faster than I2C (up to 20 MHz) and full-duplex. Each device needs its own SS pin — 4 devices need 4 extra GPIO pins.
// SD card via SPI using the SD library
// Arduino Uno SPI pins: 11=MOSI, 12=MISO, 13=SCK, 10=CS
#include <SPI.h>
#include <SD.h>
const int SD_CS = 10;
void setup() {
Serial.begin(115200);
if (!SD.begin(SD_CS)) {
Serial.println(F("SD card init failed!"));
while (1);
}
Serial.println(F("SD card ready"));
// Create/append a file
File logFile = SD.open("log.csv", FILE_WRITE);
if (logFile) {
logFile.println(F("time_ms,temperature,humidity"));
logFile.close();
Serial.println(F("Header written"));
}
}
void loop() {
File logFile = SD.open("log.csv", FILE_WRITE);
if (logFile) {
logFile.print(millis());
logFile.print(',');
logFile.print(25.3); // replace with actual sensor read
logFile.print(',');
logFile.println(60.1);
logFile.close();
}
delay(5000);
}
I2C vs SPI
- I2C: Two-wire: SDA + SCL shared by all devices
- SPI: Four wires: MOSI + MISO + SCK + 1 SS pin per device
Warning: I2C requires pull-up resistors on both SDA and SCL (typically 4.7 kΩ to Vcc). Most breakout boards include them, but wiring multiple I2C boards together may add too many pull-ups in parallel, dropping resistance below 1 kΩ and causing signal integrity issues. Remove pull-ups from extra boards if you see corrupted I2C data.
Frequently Asked Questions
When should I choose SPI over I2C?
Use SPI for high-speed devices (SD cards ≥4 MHz, TFT displays ≥10 MHz). Use I2C when you have many slow sensors and few spare GPIO pins. I2C is simpler to wire but slower. For most sensors (BME280, MPU-6050, OLED), I2C is fine.
Two of my I2C devices have the same address. How do I use both?
Option 1: check the datasheet — many devices have address pins to change to a second address. Option 2: use a TCA9548A I2C multiplexer that provides 8 isolated I2C buses, letting you have 8 devices with identical addresses.
What pull-up resistor value should I use for I2C?
4.7 kΩ at 100 kHz (standard mode), 2.2 kΩ at 400 kHz (fast mode), 1 kΩ at 1 MHz (fast-plus). Most breakout boards include 4.7 kΩ pull-ups. Too low a value wastes power; too high causes slow edges at high speed.