Arduino — I2C

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.

Image for post

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.

Components required:

  1. Arduino Uno
  2. BMP 280 Temperature & Pressure sensor
  3. Jumper wires

Make the following connections:

Image for post

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.

Image for post

Let’s go through a few of the functions that we’ve used from the Wire library:

  1. Wire.begin() — This function initiates the Wire library and joins the bus as a master or slave. In this case, the sensor is the slave and the Uno board is the master.
  2. Wire.beginTransmission (intAddress) — This function begins transmission to the I2C slave device with the given address. It also queues the bytes for transmission with the write function and transmits them by calling endTransmission ().
  3. Wire.write() — This writes the data from a slave device in response to a request from the master.
  4. Wire.endTransmission() — This ends a transmission to a slave device that was started by beginTransmission() and transmits the bytes that were queued by write().
  5. Wire.requestFrom(address, quantity, stop) — It is used by the master to request bytes from a slave device and then the bytes may be retrieved with the available() and read() functions.
  6. — Reads a byte that was transmitted from a slave device to a master after a call to requestFrom() was transmitted from a master to a slave.
  7. Wire.available() -Returns the number of bytes available for retrieval with read().

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.