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.
No Ads Available.
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
- Two Arduino boards (e.g., Uno, Nano)
- Breadboard and jumper wires
- 10kΩ pull-up resistors (optional, for SDA and SCL lines)
Circuit Setup
Follow these steps to connect the two Arduinos:
- Connect the SDA pin (A4 on Uno) of the Master to the SDA pin of the Slave.
- Connect the SCL pin (A5 on Uno) of the Master to the SCL pin of the Slave.
- Connect the GND pins of both Arduinos together.
- 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
Wire.begin()
: Initializes the I2C communication. On the Master, no arguments are required. On the Slave, the device address is specified (8 in this example).Wire.beginTransmission(address)
: Starts communication with the Slave at the specified address.Wire.write()
: Sends data from the Master to the Slave.Wire.onReceive()
: Registers a function to handle incoming data on the Slave.Wire.read()
: Reads incoming data from the I2C bus.
Troubleshooting
- If communication fails, check that the SDA and SCL connections match between the Master and Slave.
- Ensure pull-up resistors are used if communication is unstable.
- Verify the Slave address in the Master code matches the address set in the Slave code.
- Check the Serial Monitor on both Arduinos to debug communication.
Applications of I2C
I2C is widely used in embedded systems to interface sensors, displays, and other peripherals. Common applications include:
- Interfacing temperature sensors (e.g., DS3231, BME280)
- Controlling OLED or LCD displays
- Communication between multiple microcontrollers