什么是 I2C
I2C (Inter-Integrated Circuit) is a serial protocol using just two wires — SDA (data) + SCL (clock). It is the most common way to connect sensors and displays to Arduino/ESP32.
Why I2C
- Only 2 wires regardless of device count
- Up to 127 devices on one bus (each with unique 7-bit address)
- Built into virtually every MCU
How It Works
- Master sends START + 7-bit device address + R/W bit
- Target acknowledges
- Data exchanged byte-by-byte, each byte ACKed
- Master sends STOP
Quick Example (Arduino)
#include <Wire.h>
void setup() { Wire.begin(); Serial.begin(9600); }
void loop() {
Wire.beginTransmission(0x68);
Wire.write(0x00);
Wire.endTransmission();
Wire.requestFrom(0x68, 2);
while (Wire.available()) {
Serial.println(Wire.read());
}
delay(1000);
}
Common Pitfalls
- Device not found — wrong address; run I2C scanner sketch
- Garbage data — missing pull-up resistors; add 4.7k on SDA/SCL
- Bus hang — device holding SDA low; power-cycle