When connecting multiple sensors, displays, and peripheral modules to a microcontroller (such as Arduino or ESP32), you quickly run out of available GPIO pins. This is where the I2C (Inter-Integrated Circuit) protocol, also known as TWI (Two-Wire Interface), becomes essential. It enables seamless communication with dozens of devices using only two signal lines.
The Two Lines of I2C
I2C operates using two bidirectional open-drain lines pulled high with pull-up resistors.
| Line | Full Name | Role in Data Transmission |
|---|---|---|
| SDA | Serial Data | Transmits data bits back and forth between connected nodes. |
| SCL | Serial Clock | Carries the clock signal generated by the master device for synchronization. |
Key Features and Device Addressing
I2C communication follows a Controller-Target (Master-Slave) topology mediated by distinct device addresses.
- 7-bit Addressing: Every target on the bus has a unique hex address (e.g.,
0x27for I2C LCDs or0x68for RTC/gyroscope modules). - Multi-Device Bus: Up to 127 unique devices can share the exact same two wires (SDA and SCL).
- Moderate Transfer Speeds: Typically runs at 100 kHz (Standard mode) or 400 kHz (Fast mode), ideal for telemetry sensors and control boards.
Arduino I2C Code Example (Wire Library)
On Arduino platforms, I2C operations are handled via the native Wire.h library.
#include <Wire.h>
void setup() {
Wire.begin(); // Initialize the I2C bus as a Controller/Master
Serial.begin(9600);
}
void loop() {
// Request 2 bytes from target peripheral at address 0x68
Wire.requestFrom(0x68, 2);
while (Wire.available()) {
byte dataByte = Wire.read();
Serial.print("Received byte: ");
Serial.println(dataByte, HEX);
}
delay(1000);
}
When to Choose the I2C Protocol
I2C is the optimal choice when board space and pin counts are constrained, allowing you to interface multiple telemetry sensors (temperature, barometric pressure, IMUs) or alphanumeric displays over short PCB traces.