How to Connect Two Arduino Boards Using I2C

The popular I2C protocol enables two or more Arduino boards to communicate Discover how to connect and code them

Jul 16, 2023 - 17:40
 33
How to Connect Two Arduino Boards Using I2C
How to Connect Two Arduino Boards Using I2C

o connect two Arduino boards using I2C (Inter-Integrated Circuit) communication, you will need the following:

  1. Two Arduino boards: Each board should have an ATmega microcontroller (such as Arduino Uno, Arduino Nano, etc.) or any other compatible board.

  2. I2C-compatible pins: Most Arduino boards have dedicated I2C pins, labeled as SDA (data line) and SCL (clock line). Ensure that your chosen Arduino boards have these pins available.

  3. 4.7kΩ pull-up resistors: You will need two 4.7kΩ resistors to connect to the SDA and SCL lines. These resistors are required to pull the I2C bus lines high.

  4. Jumper wires: Use jumper wires to connect the components together.

Once you have gathered the necessary components, follow these steps to connect the Arduino boards using I2C:

Step 1: Connect the first Arduino board

  • Connect the SDA pin of the first Arduino to the SDA pin of the second Arduino.
  • Connect the SCL pin of the first Arduino to the SCL pin of the second Arduino.
  • Connect the 5V and GND pins of the first Arduino to the respective 5V and GND pins of the second Arduino.
  • Connect a 4.7kΩ pull-up resistor between the SDA pin and the 5V pin of the first Arduino.
  • Connect a 4.7kΩ pull-up resistor between the SCL pin and the 5V pin of the first Arduino.

Step 2: Upload the I2C Master Sketch

  • Choose one of the Arduino boards to act as the I2C master.
  • Connect the master Arduino to your computer and upload the following I2C master sketch:

#include <Wire.h>

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

void loop() {
  Wire.beginTransmission(9); // address of the slave Arduino
  Wire.write("Hello from master!"); // send data to slave
  Wire.endTransmission(); // stop transmitting
  delay(500); // wait for a moment
}

Step 3: Upload the I2C Slave Sketch

  • Choose the other Arduino board to act as the I2C slave.
  • Connect the slave Arduino to your computer and upload the following I2C slave sketch:

#include <Wire.h>

void setup() {
  Wire.begin(9); // join I2C bus as slave with address 9
  Wire.onReceive(receiveEvent); // register receive event handler
  Serial.begin(9600); // initialize serial communication for debugging
}

void loop() {
  delay(100); // wait for any incoming data
}

void receiveEvent(int bytes) {
  while (Wire.available()) {
    char c = Wire.read(); // read the received byte
    Serial.print(c); // print received data to serial monitor
  }
}

ep 4: Testing the Connection

  • Open the serial monitor for the slave Arduino (baud rate: 9600).
  • Once both sketches are uploaded, you should see "Hello from master!" messages appearing in the serial monitor of the slave Arduino.

Congratulations! You have successfully connected two Arduino boards using I2C communication. You can now extend this basic setup to exchange more complex data or control signals between the two boards. Remember to adapt the code according to your specific project requirements.