0
0
AutocadHow-ToBeginner · 4 min read

How to Connect Multiple I2C Devices to Arduino Easily

To connect multiple I2C devices to an Arduino, connect all devices' SDA pins together and all SCL pins together, then connect these lines to the Arduino's SDA and SCL pins. Each device must have a unique I2C address so the Arduino can communicate with them individually.
📐

Syntax

In Arduino, you use the Wire library to communicate with I2C devices. The basic syntax to start communication with a device is:

  • Wire.begin(): Initializes the I2C bus.
  • Wire.beginTransmission(address): Starts communication with the device at the given address.
  • Wire.write(data): Sends data to the device.
  • Wire.endTransmission(): Ends communication.
  • Wire.requestFrom(address, quantity): Requests data from the device.

All devices share the same SDA and SCL lines but must have different addresses.

arduino
#include <Wire.h>

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

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

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

Example

This example shows how to connect two I2C devices with addresses 0x3C and 0x68. It reads one byte from each device and prints it to the Serial Monitor.

arduino
#include <Wire.h>

void setup() {
  Serial.begin(9600);
  Wire.begin();
}

void loop() {
  // Read from device 1 at address 0x3C
  Wire.requestFrom(0x3C, 1);
  if (Wire.available()) {
    int data1 = Wire.read();
    Serial.print("Device 0x3C data: ");
    Serial.println(data1);
  }

  // Read from device 2 at address 0x68
  Wire.requestFrom(0x68, 1);
  if (Wire.available()) {
    int data2 = Wire.read();
    Serial.print("Device 0x68 data: ");
    Serial.println(data2);
  }

  delay(1000);
}
Output
Device 0x3C data: 0 Device 0x68 data: 0
⚠️

Common Pitfalls

  • Address Conflicts: Two devices with the same I2C address will cause communication errors. Always check device datasheets and change addresses if possible.
  • Missing Pull-up Resistors: I2C lines need pull-up resistors (typically 4.7kΩ). Some modules have them built-in; if not, add them externally.
  • Incorrect Wiring: Connect all SDA pins together and all SCL pins together, then to Arduino's A4 (SDA) and A5 (SCL) on Uno.
  • Power Issues: Ensure all devices share a common ground and proper voltage.
arduino
/* Wrong way: Connecting devices to different SDA/SCL pins */
// Device 1 SDA to Arduino A4
// Device 1 SCL to Arduino A5
// Device 2 SDA to Arduino A2 (WRONG)
// Device 2 SCL to Arduino A3 (WRONG)

/* Right way: All SDA pins connected together to A4, all SCL pins connected together to A5 */
📊

Quick Reference

  • Connect all device SDA pins together and to Arduino SDA pin.
  • Connect all device SCL pins together and to Arduino SCL pin.
  • Use pull-up resistors (4.7kΩ) on SDA and SCL lines if not included on modules.
  • Check each device's unique I2C address before connecting.
  • Use Wire.begin() to start I2C communication in Arduino code.

Key Takeaways

Connect all I2C devices' SDA pins together and all SCL pins together to Arduino's SDA and SCL pins.
Ensure each I2C device has a unique address to avoid conflicts on the bus.
Use pull-up resistors on SDA and SCL lines if your modules don't have them built-in.
Always share a common ground and proper voltage between Arduino and devices.
Use the Wire library functions to communicate with each device by its address.