Arduino - Inter Integrated Circuit - Tutorialspoint
Arduino - Inter Integrated Circuit - Tutorialspoint
Inter-integrated circuit (I2C) is a system for serial data exchange between the microcontrollers
and specialized integrated circuits of a new generation. It is used when the distance between
them is short (receiver and transmitter are usually on the same printed board). Connection is
established via two conductors. One is used for data transfer and the other is used for
synchronization (clock signal).
As seen in the following figure, one device is always a master. It performs addressing of one
slave chip before the communication starts. In this way, one microcontroller can communicate
with 112 different devices. Baud rate is usually 100 Kb/sec (standard mode) or 10 Kb/sec (slow
baud rate mode). Systems with the baud rate of 3.4 Mb/sec have recently appeared. The
distance between devices, which communicate over an I2C bus is limited to several meters.
Arduino I2C
We have two modes - master code and slave code - to connect two Arduino boards using I2C.
They are −
Master Transmitter / Slave Receiver
Master Receiver / Slave Transmitter
https://www.tutorialspoint.com/arduino/arduino_inter_integrated_circuit.htm 1/4
16/11/2020 Arduino - Inter Integrated Circuit - Tutorialspoint
Master Transmitter
The following functions are used to initialize the Wire library and join the I2C bus as a master or
slave. This is normally called only once.
Wire.begin(address) − Address is the 7-bit slave address in our case as the master is
not specified and it will join the bus as a master.
Wire.beginTransmission(address) − Begin a transmission to the I2C slave device
with the given address.
Wire.write(value) − Queues bytes for transmission from a master to slave device (in-
between calls to beginTransmission() and endTransmission()).
Wire.endTransmission() − Ends a transmission to a slave device that was begun by
beginTransmission() and transmits the bytes that were queued by wire.write().
Example
short age = 0;
void loop() {
Wire.beginTransmission(2);
// transmit to device #2
Wire.write("age is = ");
Wire.write(age); // sends one byte
Wire.endTransmission(); // stop transmitting
delay(1000);
}
Slave Receiver
id () { // hi ill l
https://www.tutorialspoint.com/arduino/arduino_inter_integrated_circuit.htm 2/4
16/11/2020 Arduino - Inter Integrated Circuit - Tutorialspoint
void setup() { //this will run only once
Wire.begin(2); // join i2c bus with address #2
Wire.onReceive(receiveEvent); // call receiveEvent when the master send any th
void loop() {
delay(250);
}
Master Receiver
The Master, is programmed to request, and then read bytes of data that are sent from the
uniquely addressed Slave Arduino.
The following function is used −
Wire.requestFrom(address,number of bytes) − Used by the master to request bytes from a
slave device. The bytes may then be retrieved with the functions wire.available() and wire.read()
functions.
Example
void loop() {
Wire.requestFrom(2, 1); // request 1 bytes from slave device #2
while (Wire.available()) // slave may send less than requested {
char c = Wire.read(); // receive a byte as character
Serial.print(c); // print the character
}
delay(500);
}
https://www.tutorialspoint.com/arduino/arduino_inter_integrated_circuit.htm 3/4
16/11/2020 Arduino - Inter Integrated Circuit - Tutorialspoint
Slave Transmitter
#include <Wire.h>
void setup() {
Wire.begin(2); // join i2c bus with address #2
Wire.onRequest(requestEvent); // register event
}
Byte x = 0;
void loop() {
delay(100);
}
void requestEvent() {
Wire.write(x); // respond with message of 1 bytes as expected by master
x++;
}
https://www.tutorialspoint.com/arduino/arduino_inter_integrated_circuit.htm 4/4