How to Use Wire Library in Arduino for I2C Communication
Use the
Wire library in Arduino to communicate with I2C devices by including #include <Wire.h>, initializing with Wire.begin(), and then sending or receiving data using Wire.beginTransmission(), Wire.write(), and Wire.endTransmission(). For reading, use Wire.requestFrom() and Wire.read() to get data from the device.Syntax
The Wire library uses these main functions for I2C communication:
Wire.begin(): Starts the I2C bus as master or slave.Wire.beginTransmission(address): Begins communication with a device at the given I2C address.Wire.write(data): Sends data bytes to the device.Wire.endTransmission(): Ends the transmission and sends the data.Wire.requestFrom(address, quantity): Requests bytes from a device.Wire.read(): Reads a byte received from the device.
arduino
#include <Wire.h> void setup() { Wire.begin(); // Initialize I2C as master } void loop() { Wire.beginTransmission(0x3C); // Start communication with device at address 0x3C Wire.write(0x00); // Send a byte Wire.endTransmission(); // End transmission Wire.requestFrom(0x3C, 1); // Request 1 byte from device if (Wire.available()) { int data = Wire.read(); // Read the byte } delay(1000); }
Example
This example shows how to send a byte to an I2C device and read a byte back. It demonstrates basic master mode communication using the Wire library.
arduino
#include <Wire.h> void setup() { Serial.begin(9600); Wire.begin(); // Start I2C as master } void loop() { Wire.beginTransmission(0x3C); // Device address Wire.write(0x01); // Send a command or data byte Wire.endTransmission(); // End transmission Wire.requestFrom(0x3C, 1); // Request 1 byte from device if (Wire.available()) { int received = Wire.read(); // Read the byte Serial.print("Received: "); Serial.println(received); } delay(1000); // Wait 1 second }
Output
Received: 0
Common Pitfalls
Common mistakes when using the Wire library include:
- Not calling
Wire.begin()insetup(), which prevents I2C from initializing. - Using the wrong I2C address for the device; always check the device datasheet.
- Not checking
Wire.available()before reading data, which can cause errors. - Forgetting to call
Wire.endTransmission()after writing data. - Trying to use
Wire.write()outside a transmission block.
Wrong way:
#include <Wire.h>
void setup() {
// Missing Wire.begin()
}
void loop() {
Wire.beginTransmission(0x3C);
Wire.write(0x01);
// Missing Wire.endTransmission()
delay(1000);
}Right way:
#include <Wire.h>
void setup() {
Wire.begin();
}
void loop() {
Wire.beginTransmission(0x3C);
Wire.write(0x01);
Wire.endTransmission();
delay(1000);
}Quick Reference
| Function | Description |
|---|---|
| Wire.begin() | Initialize I2C bus as master or slave |
| Wire.beginTransmission(address) | Start communication with device at given 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 a byte received from device |
| Wire.available() | Check if data is available to read |
Key Takeaways
Always call Wire.begin() in setup() to initialize the I2C bus.
Use Wire.beginTransmission() and Wire.endTransmission() to send data to a device.
Use Wire.requestFrom() and Wire.read() to receive data from a device.
Check Wire.available() before reading to avoid errors.
Verify the correct I2C address for your device from its datasheet.