Post Top Ad

Sunday, December 24, 2023

How I2C Communication Works and How To Use It with Arduino


 How I2C Communication Works and How To Use It with Arduino

In this tutorial we will learn how the I2C communication protocol works and also we will make a practical example of it with the Arduino Board and a sensor which uses this protocol. You can watch the following video or read the written tutorial below.

Here we would like to show you how to connect multiple Arduinos through I2C communication. We will start with two Arduinos, one Master and one Slave, than we will add to this another Slave and adjust the Master code, than we will add yet another Slave to this and adjust the Master code again. That we show how to connect multiple Arduinos via I2C where one Arduino is the Master and has multiple Slaves. At the end we will show how to have multiple Masters and multiple Slaves with four Arduinos.


Before all of that we would like to mentioned the reason why we would need so many Arduinos. In robotics for instance we prefer to have a single microcontroller controlling one set of sensors or actuators. One example would be to have one Arduino as a Master, two slave Arduinos each responsible for controlling two motors via Arduino bridge motor drivers, and a third Arduino connected with object avoidance ultrasonic sensors. Each Arduino would have different internal operations and their actions would be determined by the master.


Now let's start with the simple two Arduino wiring.

4x Arduino Uno (or Arduino Nano)

5V power supply for powering our Arduinos (you can choose to use different ways to power up an Arduino, just remember to connect the grounds)

3x LEDs (blue, red, yellow)

Jumper wires

Breadboard

Here we are going to show how to connect two Arduinos together with I2C connection. You can simply slide the images above or read the steps below.


Connect the grounds of both Arduinos together. (Image 1)

Now we are going to connect both A5 analog pins together, these pins are SCL, or clock pins explained later in the tutorial. (Image 2)

Connect both A4 analog pins together, these pins are SDA, or data transfer pins. (Image 3)

To upload the code we need to connect to both Arduinos a USB cable, recognize which port is for which Arduino. Through the USB we power up both Arduinos and we are ready to start uploading the code.

Master and One Slave Arduino

Now we will write a program which will enable a master to control an LED on the Slave Arduino. User will write in the serial monitor of the Master BlueOn or BlueOff to toggle the LED on the Slave. Simple enough, so let's write the code.


Here you can copy the master code or simply download it from the attachment down below. The attached code has an explanation for each line of the code, but in the next step of the tutorial we will explain the code in detail.


 How I2C Communication Works and How To Use It with Arduino

In this tutorial we will learn how the I2C communication protocol works and also we will make a practical example of it with the Arduino Board and a sensor which uses this protocol. You can watch the following video or read the written tutorial below.

Here we would like to show you how to connect multiple Arduinos through I2C communication. We will start with two Arduinos, one Master and one Slave, than we will add to this another Slave and adjust the Master code, than we will add yet another Slave to this and adjust the Master code again. That we show how to connect multiple Arduinos via I2C where one Arduino is the Master and has multiple Slaves. At the end we will show how to have multiple Masters and multiple Slaves with four Arduinos.


Before all of that we would like to mentioned the reason why we would need so many Arduinos. In robotics for instance we prefer to have a single microcontroller controlling one set of sensors or actuators. One example would be to have one Arduino as a Master, two slave Arduinos each responsible for controlling two motors via Arduino bridge motor drivers, and a third Arduino connected with object avoidance ultrasonic sensors. Each Arduino would have different internal operations and their actions would be determined by the master.


Now let's start with the simple two Arduino wiring.

4x Arduino Uno (or Arduino Nano)

5V power supply for powering our Arduinos (you can choose to use different ways to power up an Arduino, just remember to connect the grounds)

3x LEDs (blue, red, yellow)

Jumper wires

Breadboard

Here we are going to show how to connect two Arduinos together with I2C connection. You can simply slide the images above or read the steps below.


Connect the grounds of both Arduinos together. (Image 1)

Now we are going to connect both A5 analog pins together, these pins are SCL, or clock pins explained later in the tutorial. (Image 2)

Connect both A4 analog pins together, these pins are SDA, or data transfer pins. (Image 3)

To upload the code we need to connect to both Arduinos a USB cable, recognize which port is for which Arduino. Through the USB we power up both Arduinos and we are ready to start uploading the code.

Master and One Slave Arduino

Now we will write a program which will enable a master to control an LED on the Slave Arduino. User will write in the serial monitor of the Master BlueOn or BlueOff to toggle the LED on the Slave. Simple enough, so let's write the code.


Here you can copy the master code or simply download it from the attachment down below. The attached code has an explanation for each line of the code, but in the next step of the tutorial we will explain the code in detail.

No comments:

Post a Comment

Post Top Ad

Pages