0
0
AutocadHow-ToBeginner · 3 min read

How to Use I2C in Arduino: Simple Guide with Examples

To use I2C in Arduino, include the Wire.h library, then use Wire.begin() to start the bus. Use Wire.beginTransmission(address) and Wire.endTransmission() to send data, and Wire.requestFrom(address, quantity) to receive data from devices.
📐

Syntax

The Wire library handles I2C communication in Arduino. Key functions include:

  • Wire.begin(): Initializes the I2C bus as master or slave.
  • Wire.beginTransmission(address): Starts communication with a device at the given 7-bit address.
  • Wire.write(data): Sends data bytes to the device.
  • Wire.endTransmission(): Ends the transmission and sends the data.
  • Wire.requestFrom(address, quantity): Requests quantity bytes from the device.
  • Wire.read(): Reads received bytes one by one.
arduino
#include <Wire.h>

void setup() {
  Wire.begin(); // Start I2C as master
}

void loop() {
  Wire.beginTransmission(0x3C); // Device address
  Wire.write(0x00); // Data to send
  Wire.endTransmission();

  Wire.requestFrom(0x3C, 1); // Request 1 byte
  if (Wire.available()) {
    int data = Wire.read();
  }
  delay(1000);
}
💻

Example

This example shows how to send a byte to an I2C device at address 0x3C and read one byte back. It prints the received byte to the Serial Monitor.

arduino
#include <Wire.h>

void setup() {
  Serial.begin(9600);
  Wire.begin(); // Initialize I2C as master
}

void loop() {
  Wire.beginTransmission(0x3C); // Start communication with device
  Wire.write(0x01); // Send a command or data byte
  Wire.endTransmission();

  Wire.requestFrom(0x3C, 1); // Request 1 byte from device
  if (Wire.available()) {
    int received = Wire.read();
    Serial.print("Received byte: ");
    Serial.println(received);
  }
  delay(1000); // Wait 1 second
}
Output
Received byte: 0
⚠️

Common Pitfalls

Common mistakes when using I2C on Arduino include:

  • Not calling Wire.begin() before using other Wire functions.
  • Using the wrong 7-bit device address (some datasheets show 8-bit addresses).
  • Not checking if data is available before reading with Wire.read().
  • Forgetting to call Wire.endTransmission() after Wire.write().
  • Not connecting the SDA and SCL lines with pull-up resistors (usually 4.7kΩ).

Example of a wrong and right way:

arduino
// Wrong way: missing Wire.endTransmission()
Wire.beginTransmission(0x3C);
Wire.write(0x01);
// Wire.endTransmission(); // This line is missing

// Right way:
Wire.beginTransmission(0x3C);
Wire.write(0x01);
Wire.endTransmission();
📊

Quick Reference

FunctionDescription
Wire.begin()Initialize I2C bus as master or slave
Wire.beginTransmission(address)Start communication with device at 7-bit address
Wire.write(data)Send data byte(s) to device
Wire.endTransmission()End transmission and send data
Wire.requestFrom(address, quantity)Request bytes from device
Wire.read()Read received byte

Key Takeaways

Always include Wire.h and call Wire.begin() before using I2C functions.
Use 7-bit device addresses and confirm them from your device datasheet.
Call Wire.endTransmission() after sending data with Wire.write().
Check Wire.available() before reading data to avoid errors.
Connect SDA and SCL lines with pull-up resistors for reliable communication.