Inter-Arduino communication opens up a world of possibilities for embedded systems and IoT projects. One powerful method for achieving this is using the I2C (Inter-Integrated Circuit) protocol. I2C enables seamless communication between microcontrollers, allowing for data exchange and control commands. In this article, we’ll delve into the process of controlling one Arduino board from another using I2C, exploring its principles, setup, and practical implementation.
Here is Arduino’s official documentation:
1 | https://docs.arduino.cc/learn/communication/wire/ |
Understanding I2C Communication
I2C is a synchronous serial communication protocol that facilitates communication between integrated circuits using only two wires: a data line (SDA) and a clock line (SCL). It operates in a master-slave architecture, where one device acts as the master and initiates communication, while the other devices act as slaves and respond to the master’s commands.
---
For example, the master device can generate clock pulses to synchronize data transmission and reception. Each slave device on the bus has a unique address, allowing the master to select the desired slave for communication. This address space enables multiple devices to coexist on the same bus.
Setting Up the Hardware
To control one Arduino from another via I2C, you’ll need two Arduino boards (one acting as the master and the other as the slave), along with some basic components:
- Two Arduino boards (e.g., Arduino Uno, Nano, or Mega)
- Connecting wires
- Breadboard (optional, for prototyping)
- One LED
- One resistor – 220Ohm
- One potentiometer
- Power source (USB or external power supply)
Potentiometer works because of PWM. Read What is Pulse Width Modulation (PWM)?.
Connect the boards as follows:

Implementing Master-Slave Communication
Now, let’s implement the master-slave communication between the two Arduinos:
Setting up the Slave Arduino (Arduino 2):
- Define a unique I2C address for the slave Arduino using the
Wire.begin(address)function. It isWire.begin(2)in our case. - Implement callback functions to handle incoming data from the master.
- In the callback functions, process the received data and perform the desired actions.
In this case, we will read to the master Arduino and instruct the LED.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #include <Wire.h> const int ledPin = 10; // Digital pin for LED int brightness = 0; // LED brightness void setup() { Wire.begin(2); // Initialize I2C communication with address 2 Wire.onReceive(receiveEvent); // Set up function to receive I2C data pinMode(ledPin, OUTPUT); // Set LED pin as output } void loop() { // No need for additional code in the loop since we're using I2C interrupts } void receiveEvent(int byteCount) { if (byteCount == 1) { brightness = Wire.read(); // Read the value sent from Arduino 1 analogWrite(ledPin, brightness); // Set LED brightness based on received value } } |
Setting up the Master Arduino (Arduino 1):
Basically it is easy to read from a potentiometer. The mapped value will be sent over I2C.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #include <Wire.h> const int potentiometerPin = A0; // Analog pin for potentiometer int potValue = 0; // Potentiometer value void setup() { Wire.begin(); // Initialize I2C communication } void loop() { potValue = analogRead(potentiometerPin); // Read potentiometer value int mappedValue = map(potValue, 0, 1023, 0, 255); // Map to PWM range (0-255) // Send mapped value over I2C to Arduino 2 Wire.beginTransmission(2); // Arduino 2's I2C address is 2 Wire.write(mappedValue); Wire.endTransmission(); delay(10); // Add a small delay to avoid excessive I2C communication } |
Here is the simulation:
Conclusion
Inter-Arduino communication via I2C provides a flexible and efficient way to control multiple Arduino boards in a networked environment. By understanding the principles of I2C communication and following the setup procedures, you can seamlessly integrate multiple Arduino devices into your projects, unlocking endless possibilities for automation, robotics, sensor networks, and more. Experiment with different scenarios and expand your knowledge of embedded systems and IoT applications with this powerful communication protocol.