Monday, February 14, 2022

Multiple I2C Devices Arduino


Overview

Have you ever wanted to use 2 sensors with the same I2C address at the same time, not knowing what to do? Faced some serious challenges in doing that?

In a lot of projects, we have used different modules and sensors that support I2C communication protocol. Sometimes, we have interfaced some I2C modules with a microcontroller in our projects. If they all have different I2C addresses, we could easily interface them, facing no serious trouble. But, if 2 or more of the modules had the same I2C address, we would face some apparently unsolvable problems in using them all. This problem is a serious one which we all have definitely come across at least once. If you’re having the same problem, this tutorial can be helpful for you.

Generally speaking, this problem can be solved in both software and hardware. In this tutorial, we are going to present a hardware solution for this problem. In a nutshell, we are going to add the TCA9548A I2C Multiplexer to the project and expand the I2C addresses of the modules with the same I2C address.

What Is I2C Communication Protocol, and How Does It Work?

I2C, short for Inter-Integrated Circuit, is a communication protocol which can also be referred to with the short term IIC. This communication protocol is widely used in microcontrollers. In this communication, masters and slaves (Masters are usually the main components like microcontrollers and Slaves are sensors, modules and other components used in the circuit.) communicate through 2 lines:

  • SDA (Serial Data): The line to transmit and receive data between the Master and Slave
  • SCL (Serial Clock): The line to send the clock
  • The most important features of the I2C communication protocol are the following:

    • It’s not so fast (But fast enough for most applications)
    • Suitable for short distances (e.g. maximum distance in 100KHz is 1 meter)
    • Synchronous communication
    • Data is transmitted serially
    • Logic level can be both 3.3V and 5V
    • How I2C Communication Protocol Works

      The data being transmitted between 2 devices on the SDA line consists of the following parts:

      • Start: At first, the SDA line voltage drops from High to Low level. Then, the same thing goes for the SCL line, too.
      • Address: Including 7 bits (10 bit in some cases) is the I2C address of the module that the master intends to communicate with. These 7 bits (the address) is always sent from the master toward the slaves. After the address is sent, each slave compares it to its own address and in case it matches, the module will send a single bit (ACK Bit) in Low level to the master.
      • R/W Bit: This bit, which is sent alongside the 7-bit address (it’s the LSB) determines whether the master is the transmitter or the receiver. If this bit is in High level, the master is the receiver, and vice versa.
      • ACK/NACK Bit: Once each 8-bit data is received by the Master or Slave, this bit specifies whether the data has been properly received. If the receiver properly receives the data, it’s called the “Acknowledge” state and a bit with Low level will be sent through the SDA line.
      • Date: Once the ACK Bit sent from the slave is received by the master, the first byte of data is prepared to be sent. Pay attention that at the end of each data transition, the receiver must put the ACK bit in Low level to indicate full proper transition.
      • Stop: After the completion of all data, at first, the SCL line changes from Low to High and then the SDA line will change from High to Low.


Circuits Diagram: Click here

Circuits Diagram 1: Click here





Arduino Code: Click here
 

No comments:

Post a Comment

dc motor with encoder arduino

How to control dc motor with encoder: Arduino DC Motor Speed Control with Encoder-  I have been using different types of stepper motors, Ser...