Introduction
Inter-Integrated Circuit (I2C) communication is a powerful protocol used in many microcontroller projects for interfacing with various peripherals like sensors and other microcontrollers. In this post, we’ll explore how to set up I2C communication between two Arduinos: one as a master and another as a slave. We’ll dive into the technical details, provide working code examples, and discuss common debugging challenges.
Design
I2C communication utilizes two lines: SDA (data line) and SCL (clock line), enabling multiple master and slave devices to communicate over the same bus. Each device on the bus has a unique address which the master uses to direct communication to the correct slave.
Components
- 2x Arduino Uno
- Breadboard
- Jumper wires
- Resistors (for pull-ups on SDA and SCL)
Connection Scheme
- Connect the SDA and SCL pins of both Arduinos.
- Attach pull-up resistors (typically 4.7 kΩ) to both the SDA and SCL lines to VCC.
- Ensure common ground between both Arduinos.
Implementation
Below are the implementation steps and the necessary code snippets for both the master and the slave Arduino.
Master Arduino Code
#include <Wire.h>
void setup() {
Wire.begin(); // join i2c bus as master
Serial.begin(9600);
}
void loop() {
Wire.beginTransmission(8); // transmit to device #8 (slave)
Wire.write("Hello, Slave!"); // sends hello string
Wire.endTransmission(); // stop transmitting
delay(1000); // wait for 1 second
}
Slave Arduino Code
#include <Wire.h>
void setup() {
Wire.begin(8); // join i2c bus with address #8
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600);
}
void receiveEvent(int howMany) {
while (1 < Wire.available()) { // loop through all but the last
char c = Wire.read(); // receive byte as a character
Serial.print(c); // print the character
}
int x = Wire.read(); // receive byte as an integer
Serial.println(x); // print as integer
}
void loop() {
delay(100);
}
Debugging
During the setup, you might encounter common issues such as:
- No Data Received: Ensure both Arduinos are powered and share a common ground.
- Corrupt Data: Check if pull-up resistors are properly connected. Incorrect resistor values can lead to signal integrity issues.
Results
After correctly setting up the connection and uploading the code, the Master should send “Hello, Slave!” to the Slave Arduino, which then prints the received message to its Serial Monitor.
Conclusion
Setting up I2C communication between two Arduinos allows for robust data transmission in embedded systems. By understanding the underlying principles and addressing common pitfalls, developers can implement effective communication strategies in their projects.
This practical guide demonstrates the setup and troubleshooting of I2C communication, providing a foundational skill set for more complex multi-device interactions in future projects.