I2C Communication
Two-wire I2C communication using the Wire library, device scanning, and I2C sensors.
Key idea:
I2C (Inter-Integrated Circuit) uses two wires: SDA (data) and SCL (clock). Multiple devices share the same bus, each with a unique 7-bit address.
I2C Wiring
Arduino A4 (SDA) ─────── SDA
Arduino A5 (SCL) ─────── SCL
3.3V/5V ──────────────── VCC
GND ──────────────────── GND
Pull-up resistors: I2C requires pull-up resistors on both SDA and SCL (typically 4.7kΩ each).
Arduino Wire Library
#include <Wire.h>
void setup() {
Serial.begin(9600);
Wire.begin(); // Join as master
}
void loop() {
// Scan for devices
for (byte addr = 1; addr < 127; addr++) {
Wire.beginTransmission(addr);
if (Wire.endTransmission() == 0) {
Serial.print("Found device at 0x");
Serial.println(addr, HEX);
}
}
// Read from a specific device (e.g., 0x68 for MPU6050)
Wire.beginTransmission(0x68);
Wire.write(0x3B); // Register to read from
Wire.endTransmission(false);
Wire.requestFrom(0x68, 6); // Request 6 bytes
int16_t ax = (Wire.read() << 8) | Wire.read();
int16_t ay = (Wire.read() << 8) | Wire.read();
int16_t az = (Wire.read() << 8) | Wire.read();
Serial.print("Accel: "); Serial.print(ax); Serial.print(", "); Serial.print(ay); Serial.println(az);
delay(100);
}Common I2C Devices
| Device | Address | Use Case |
|---|---|---|
| MPU6050 | 0x68 | 6-axis IMU |
| BMP280 | 0x76/0x77 | Pressure/temperature |
| DS3231 | 0x68 | Real-time clock |
| SSD1306 | 0x3C/0x3D | OLED display |
| PCF8574 | 0x20-0x27 | I/O expander |
Key Takeaways
Wire.begin()andWire.requestFrom()for master communication- I2C addresses and device scanning
- SDA and SCL pins on Arduino Uno (A4/A5)
- Pull-up resistors on I2C bus
Resources
- Arduino.cc - Wire Library: https://www.arduino.cc/reference/en/language/functions/communication/wire/
- Video: Arduino I2C Tutorial - https://www.youtube.com/watch?v=norro1KiDPE