The term I2C stands for ‘Inter-Integrated Circuits’. It’s normally denoted as I2C or I squared C or even as 2-wire protocol (TWI) in some instances — all indicative of the same. I2C is a synchronous communication protocol, unlike the UART; synchronous implies that both the devices that are sharing the information share a common clock signal. It consists of only two wires to share information, out of which one is used for the clock signal and the other one is used for sending and receiving data.
How does I2C work?
I2C communication protocol was first introduced by Philips. As mentioned earlier, it uses two wires which are connected across both the devices, wherein one device is called the master and the other device is called a slave. Communication always occurs between a Master and a Slave. One of the advantages of I2C communication is that it allows more than one slave to be connected to the Master.
The communication takes place through two wires namely Serial Clock (SCL) and Serial Data (SDA).
Serial Clock (SCL): Shares the clock signal generated by the master with the slave
Serial Data (SDA): Sends the data to and fro between the master and slave.
Note: At any given time only the master will be able to initiate the communication.
Where is I2C communication used?
I2C communication is used only for short-distance communication. It is mainly used to communicate with a sensor or other devices that need to send information to a master device and comes in handy when a microcontroller has to communicate with other slave modules using a minimum set of wires.
I2C in Arduino
The following image shows the I2C pins in the Arduino UNO.
The Arduino Analog pins that are used for I2C communication:
SDA — A4
SCL — A5
Now that we have an idea about the I2C communication, let’s get familiarised with the Wire library with the help of a simple example. Using the following sketch, we’ll learn how to read the Device ID of a BMP280 Pressure Sensor with the help of the I2C communication protocol in Arduino.
Make the following connections:
Note: Connecting SDO to the Ground, will result in slave address (0x76).
Once you’re done with these connections, upload the following sketch in the Arduino IDE.
Let’s go through a few of the functions that we’ve used from the Wire library:
The program once compiled, displays the device ID of the slave i.e the sensor on the serial monitor, using the I2C communication protocol. You can also explore reading the temperature values from the sensor.
In this blog, we went through I2C communication, how it works, where it's used, and saw a simple sketch to implement the protocol.