Introduction
In this tutorial, you will learn how to improve your programming with Arduino: I2C communication, Previously we worked with the SPI communication protocol for peripherals, and with it, we were able to send data from our Arduino to the shift register. There is also the IIC or I2C protocol, which stands for Inter-Integrated Circuit. It is very useful to communicate a microcontroller with external devices. and It combines the advantages of UART and SPI, since it only uses 2 communication cables and allows handling different slave devices, however, its transmission speed is lower.
It is somewhat more complicated than the SPI protocol since it has more steps to follow to transmit the information. now It is also necessary for the microcontroller to have an I2C port, unlike the UART protocol which can be implemented by software.
Hardware Required
Components | # | Buy From Amazon |
---|---|---|
Arduino UNO | 2 | Buy Now |
Led 5mm | 1 | Buy Now |
Resistor 1K | 1 | Buy Now |
37 in 1 Sensors kit | 1 | Buy Now |
Jumper Wires | few | Buy Now |
Breadboard | 1 | Buy Now |
9v DC Adapter (Optional) | 1 | Buy Now |
Summary
In this tutorial, we are going to communicate two Arduino with each other, which will allow us to understand how data packets are handled with this protocol.
As we mentioned, there are only two cables to transmit the data, SCL and SDA, corresponding to the clock signal and the data signal, respectively.
To start the communication it is required to send the data packets with a specific format. First a START bit , a 7 to 10 bit ADDRESS , a READ/WRITE bit, and an ACK bit . Following this, 8-bit data can be sent with its respective confirmation bit, to end with a stop bit.
The program for the master Arduino essentially establishes serial communication with the PC and I2C communication. Then, it asks the Arduino slave to send 6 bytes, and each byte is saved as a character, which it then prints on the serial monitor.
Circuit Diagram
Below is the connection of the Arduino, if you plan to use the same power supply (USB) you can use the 5V pin to power both cards.
Installing Arduino IDE Software
First, you will require to Download the updated version of Arduino IDE Software and Install it on your PC or laptop. if you Learn How to install the Arduino step-by-step guide then click on how to install Arduino Button given Blow
Code for Master
//For more Projects: www.arduinocircuit.com
#include <Wire.h>
// This code initiates communication, receives slave data and prints
void setup() {
Wire.begin(); // This function starts the protocol
Serial.begin(9600); // Commence serial communication with PC
}
void loop() {
Wire.requestFrom(8, 6);
while (Wire.available()) {
char c = Wire.read();
Serial.println(c); // Print the character on the serial monitor
}
delay(500);
}
For the slave Arduino, we tell it to start I2C communication with address 8 and we create an event that depends on the serial protocol. When the event is registered, the function that sends the 6 bytes is executed.
Code For Slave
//For more Projects: www.arduinocircuit.com
#include <Wire.h>
void setup() {
Wire.begin(8); // unirse a la communication con la dirección 8
Wire.onRequest(requestEvent); // registrar event
}
void loop() {
delay(100);
}
// This function is executed when the teacher asks for data
// this function is registered as an event, described in setup()
void requestEvent() {
Wire.write("Hello"); // responder with a message of 6 bytes
// como espera el dispositivo maestro
}
Serial Monitor
The serial monitor should show the string that we programmed into the slave Arduino.
With this tutorial, we saw how to establish I2C communication through the Wire.h library and the associated functions. To use this protocol with other peripherals, simply select the correct address and read the appropriate registers. This tutorial for Improve your programming with Arduino: I2C communication