I2C Communication Between Arduinos

I2C Communication Between Two Arduinos

Introduction

This tutorial demonstrates how to use the I2C protocol to enable communication between two Arduino boards.

Wiring Diagram

Connect the SDA and SCL pins between the two boards. Add pull-up resistors for stable communication.

Code Examples

Use the provided Arduino sketches for both Master and Slave devices.

Applications

Implement this setup in projects requiring multiple microcontrollers, like robotics or IoT systems.

Troubleshooting

Common issues include incorrect wiring or addressing. Ensure pull-up resistors are used and SDA/SCL connections are correct.

Conclusion

With I2C communication, you can expand the capabilities of your Arduino projects by enabling inter-device communication.

Introduction

I2C (Inter-Integrated Circuit) is a communication protocol that allows multiple devices to communicate using just two wires: SDA (data line) and SCL (clock line). This experiment demonstrates how to establish I2C communication between two Arduino boards, where one acts as the Master and the other as the Slave.

Components Needed

Circuit Setup

Follow these steps to connect the two Arduinos:

  1. Connect the SDA pin (A4 on Uno) of the Master to the SDA pin of the Slave.
  2. Connect the SCL pin (A5 on Uno) of the Master to the SCL pin of the Slave.
  3. Connect the GND pins of both Arduinos together.
  4. Optional: Add 10kΩ pull-up resistors between SDA and 5V, and between SCL and 5V.

Code for I2C Communication

Use the following code for the Master Arduino:


// Master Arduino Code

#include  // Include the Wire library

void setup() {
    Wire.begin(); // Start the I2C bus as Master
    Serial.begin(9600); // Start Serial communication for debugging
}

void loop() {
    Wire.beginTransmission(8); // Address of the Slave device
    Wire.write("Hello, Slave!"); // Send data to the Slave
    Wire.endTransmission(); // End the I2C transmission
    Serial.println("Message sent to Slave.");
    delay(1000); // Wait for 1 second
}
            

Use the following code for the Slave Arduino:


// Slave Arduino Code

#include  // Include the Wire library

void setup() {
    Wire.begin(8); // Start the I2C bus as Slave with address 8
    Wire.onReceive(receiveEvent); // Register a function to handle data reception
    Serial.begin(9600); // Start Serial communication for debugging
}

void loop() {
    delay(100); // Keep the loop alive
}

// Function to handle received data
void receiveEvent(int bytes) {
    while (Wire.available()) {
        char c = Wire.read(); // Read one byte
        Serial.print(c); // Print received data to Serial Monitor
    }
    Serial.println(); // Add a newline after the message
}
            

Explanation

Troubleshooting

Applications of I2C

I2C is widely used in embedded systems to interface sensors, displays, and other peripherals. Common applications include: