30 Days Lost in Space → Help Center

Libraries

Wire Library (I2C Communication)

The built-in Wire library lets your Arduino talk to I2C devices like OLED displays and sensors using just two wires: SDA and SCL.

What is the Wire Library?

The Wire library is built into Arduino and provides I2C (Inter-Integrated Circuit) communication. I2C lets multiple devices share just two wires:

  • SDA (data) — Arduino pin A4
  • SCL (clock) — Arduino pin A5

Why I2C?

  • Only 2 wires for any number of devices
  • Each device has a unique address (like a phone number)
  • Built into many sensors and displays (OLED, temperature sensors, etc.)

Setup

#include <Wire.h>

void setup() {
  Wire.begin();  // Join I2C bus as controller
}

Key Functions

Sending Data

Wire.beginTransmission(0x3C);  // Start talking to device at address 0x3C
Wire.write(0x00);              // Send a byte
Wire.write(data, length);      // Send multiple bytes
Wire.endTransmission();        // Stop talking

Receiving Data

Wire.requestFrom(0x3C, 2);    // Ask device for 2 bytes
while (Wire.available()) {
  byte b = Wire.read();        // Read each byte
}

Finding Device Addresses

If you don't know a device's address, run an I2C scanner:

#include <Wire.h>

void setup() {
  Wire.begin();
  Serial.begin(9600);
  Serial.println("I2C Scanner...");

  for (byte addr = 1; addr < 127; addr++) {
    Wire.beginTransmission(addr);
    if (Wire.endTransmission() == 0) {
      Serial.print("Found device at 0x");
      Serial.println(addr, HEX);
    }
  }
}

void loop() {}

Common I2C Addresses

| Device | Address | |--------|---------| | SSD1306 OLED | 0x3C or 0x3D | | AHT10 Temp Sensor | 0x38 | | PCF8574 IO Expander | 0x20-0x27 |

Tips

  • I2C is slower than SPI but uses fewer wires
  • Keep I2C wires short (under 1 meter)
  • Most libraries (U8g2, etc.) call Wire internally — you just need to #include <Wire.h>
  • If a device doesn't respond, check the address and wiring