How to communicate between two Arduino boards using the I2C protocol?

How to communicate between two Arduino boards using the I2C protocol

Hello and welcome back. In this tutorial, we will learn how to communicate between two Arduino boards. For that, I have used the I2C protocol for communication. In this project, we can see the two circuits. That’s master and slave. For the master circuit, I used the Arduino UNO MININA R4 board and a potentiometer. Also, for the slave circuit I used the Arduino UNO board and one servo motor. With these parts, we can control the servo using the master circuit.

What’s the I2C protocol?

It’s simply called Inter-Integrated Circuit, often abbreviated as I2C or IIC. The I2C is the serial communication protocol and communicates data bit by bit using a single wire connection. It has two main parts: the master and the slave. You can control many slaves with one master or many masters with one slave. All of this happens with just two wires. That is,

  • SCL – (Serial clock) — carries the clock signal.
  • SDA – (Serial Data) — send and receive data.

We can see this protocol in Development boards, sensors, modules, etc. Therefore, we can connect the multiple I2C sensors or modules to one Arduino board. In this case, the Arduino board is the master and the other components are slaves.

  • If you want to know more about I2C protocol — click on me

OK let’s do this project step by step. The required components are given below.  

Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.

Step 1

Firstly, identify these components.

Step 2

Secondly, let’s assemble the master circuit. For that, place the potentiometer on the breadboard. And then, connect it to the Arduino UNO R4 MINIMA board.

How to communicate between two Arduino boards using the I2C protocol

Step 3

Thirdly, let’s assemble the slave circuit. For that, connect the SG90 servo motor to the Arduino UNO R3 board.

Step 4

Now, connect the Arduino boards together using two jumper wires(I2C communication). For that, use the circuit diagram above.

Step 5

OK, now connect the master circuit to the computer and upload the program to it. For that, follow the instructions below.

How to communicate between two Arduino boards using the I2C protocol
  • Code and circuit diagram — Download
  • Then, copy and paste the following code to the Arduino IDE.
#include <Wire.h>
#define potentiometer A0
int value = 0;
int slaveAddress = 8;

void setup() {
  Wire.begin();
  Serial.begin(9600);
}

void loop() {
  value = analogRead(potentiometer);
  value = map(value, 0, 1024, 0, 255);
  Serial.println(value);

  Wire.beginTransmission( slaveAddress ); 
  Wire.write(value);
  Wire.endTransmission();
  delay(10);
}
  • Now, select the board and port. After, click the upload button.

Step 6

Next, connect the slave circuit to the computer and upload the program. For that, follow the instructions below.

How to communicate between two Arduino boards using the I2C protocol
  • Code and circuit diagram — Download
  • Then, copy and paste the following code to the Arduino IDE.
#include <Servo.h>
#include <Wire.h>
int value = 0;

Servo servo;

void setup() {
  Serial.begin(9600);
  Wire.begin(8);
  Wire.onReceive(events);
  servo.attach(3);
}

void loop() {

}

void events() {
  value = Wire.read();
  Serial.println(value);
  servo.write(value);
}
  • Next, select the board and port. After, click the upload button.

Now, power these circuits together. Then, you can control the servo motor using the potentiometer. OK, enjoy this project. The full video guide is below. We hope to see you in the next project or tutorial.

How to communicate between two Arduino boards using the I2C protocol

How to communicate between two Arduino boards using the I2C protocol?

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *