Arduino Experiment: I2C Communication Between Microcontrollers

Difficulty Level: Intermediate

I2C (Inter-Integrated Circuit) is a protocol used to allow communication between multiple microcontrollers and peripherals using just two wires: SDA (data line) and SCL (clock line). In this experiment, two Arduinos will be connected using I2C, with one acting as the master and the other as the slave.

Required Components

Wiring Instructions

Connect the two Arduinos together using the I2C pins:

Note: Some Arduino boards may require external pull-up resistors on the SDA and SCL lines. The Arduino Uno and similar boards have these built in, but other microcontrollers might not.

Code Explanation

In this experiment, the master Arduino will send a value to the slave Arduino, which will then print the received data to the serial monitor.

Master Code

The master device initiates communication by sending data to the slave device using the Wire.beginTransmission() and Wire.write() functions. It then ends the transmission using Wire.endTransmission().

Master Arduino Code


// Include Wire library for I2C
#include 

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

void loop() {
  Wire.beginTransmission(8);  // Start communication with slave device (address 8)
  Wire.write("Hello, Slave!");  // Send string to slave
  Wire.endTransmission();  // Stop transmission

  delay(1000);  // Wait 1 second before next transmission
}
            

Slave Code

The slave device waits for the master to send data. It uses the Wire.onReceive() function to specify what action to take when data is received. The Wire.read() function retrieves the received bytes, which are then printed to the serial monitor.

Slave Arduino Code


// Include Wire library for I2C
#include 

void setup() {
  Wire.begin(8);  // Join I2C bus with address #8
  Wire.onReceive(receiveEvent);  // Register event for receiving data
  Serial.begin(9600);  // Initialize serial communication
}

void loop() {
  delay(100);  // Delay for stability
}

// Function that executes whenever data is received from master
void receiveEvent(int howMany) {
  while (Wire.available()) {  // Loop while data is available
    char c = Wire.read();  // Receive byte as a character
    Serial.print(c);  // Print the character to serial monitor
  }
  Serial.println();  // Print new line after each message
}
            

Upload and Test

  1. Connect your two Arduino boards as described in the wiring instructions.
  2. Upload the master code to one Arduino and the slave code to the other.
  3. Open the serial monitor on the slave Arduino.
  4. You should see "Hello, Slave!" printed in the serial monitor every second.

How It Works

This experiment demonstrates how to use the I2C protocol for communication between two Arduinos. The master device sends a string to the slave device, which prints the received message to the serial monitor. This basic setup can be expanded for more complex communications between multiple devices.

Conclusion

I2C communication allows for simple, efficient data transfer between microcontrollers and peripheral devices. By understanding the basics of master-slave communication, you can easily implement I2C in your projects for sensor reading, data logging, or even creating a network of devices.