I2C Multiplexer – How to use two i2c modules with the same address?!

//I2C Multiplexer – How to use two i2c modules with the same address?!
TUTORIAL

i2c_multiplexer-featured


– Using the TCA9548A I2C Multiplexer –

To connect multiple I2C module with the same address

HELP OUT

All my content is free.

If you feel that my videos are helping and you would like to contribute, you can toss some coins in the Tip Jar via PayPal.

Select amount then click the “Donate” button.

Brainy-Bits Tip Jar Amount
OVERVIEW

In the last tutorial we looked at how the I2C protocol works and saw that one thing it needs is that each of the connected i2c module have a unique address.

This is important for the Arduino to select which of the connected i2c devices it want to talk to.

Now most of the time this isn’t a problem since most module have some way to select the i2c address by way of dip switches, like the I2C Expander module we looked at, or by soldering some pads on the PCB.

But some module have a Fixed i2c address and don’t have any way to change it.

This is when the I2C Multiplexer comes into play!

In this tutorial we will use two Oled Displays which have a Fixed i2c address of 0x3C.

Quick overview of the TCA9548A I2C Multiplexer

I2C-Multiplexer-TCA9548A

The i2c multiplexer enables you to use up to eight i2c module with the same address.

It connect to the Arduino using the SDA and SCL pins and itself has the i2c address of 0x70, which can be changed by soldering some pads on the PCB (A0-A1-A3), or by sending HIGH or LOW signal to the corresponding pins.  Check out the tutorial video for more information on that function.

You then connect the i2c modules you want to use to the I2C Multiplexer using any of the eight available I2C channels:

SD0-SC0  – i2c Channel #0
SD1-SC1   – i2c Channel #1
…….
SD7-SC7   – i2c Channel #7

When you want the Arduino to talk to a specific module you switch to the channel the module is connected to.

One thing to take note of is that even though the I2C Multiplexer uses the i2c address of 0x70, went you switch to a channel to communicate with a module, you will still need to know that module’s i2c address in the Arduino Code.

If you’re using multiple i2c modules that are all set at the same i2c address, then you use that address when communicating with any of those modules after changing the channel.

Technically you can use i2c modules that don’t share the same address if you wanted to as well, although you can do that without using this I2C Multiplexer,  but if you wanted to connect all the module (those sharing i2c address + some more that don’t) you could do that to simplify the wiring I guess.

Connection Diagram

I2C-Multiplexer-Connections

As you can see we are only using 2 Pins on the Arduino to connect the I2C Multiplexer and both OLED displays.

I2C Multiplexer module connections:
5V and Ground from the Arduino and A4 – A5 to SDA and SCL on the module

OLED Connections:
5v and Ground from the Arduino and SD2-SC2 to SDA and SCL of OLED #1
5v and Ground from the Arduino and SD7-SC7 to SDA and SCL of OLED #2

Switches Connections:
Ground from the Arduino on one leg of the switches

Pin 10 of the Arduino to switch #1 other leg
Pin 11 of the Arduino to switch #2 other leg

THE CODE

In this tutorial we are using two OLED display which have a fixed I2C address of 0x3c and using the I2C Multiplexer which has a default address of 0x70.

We are using a library to communicate and switch channels on the I2C Multiplexer.

Also using the U8Glib library for the OLED displays.

By default the U8Glib library is configured to use the I2C address 0x3c for the displays, but if the display you’re using have a different address, you will need to edit this file:   arduino library folder/u8glib/csrc/u8g_com_i2c.c  to change it.  Make sure to check out the tutorial video for more information on this.

As always to get more information on the code check out the tutorial video as well.

/* Arduino I2C Multiplexer TCA9548A with two I2C Oled

Created by Yvan / https://Brainy-Bits.com

This code is in the public domain...
You can: copy it, use it, modify it, share it or just plain ignore it!
Thx!

*/

#include <U8glib.h>   // Library for OlED display https://github.com/olikraus/u8glib/

#include <I2CMux.h>    // Library for TCA9548A by Alvaro Ferran https://github.com/alvaroferran/I2C-multiplexer
#include <Wire.h>

#define I2CMulti_Addr 0x70  // Address of the I2C Multiplexer board

#define change_switch 10    // Tact switch used to select which OLED to use
#define increase_count_switch 11  // Tact switch used to increase the counter

int selected_oled=2;  // Use to keep track of which I2C channel is selected on the Multiplexer

I2CMux I2CMulti(I2CMulti_Addr);  // Init of the multiplexer library

int Oled1c=0;   // Counter variable for OLED #1
int Oled2c=0;   // Counter variable for OLED #2


U8GLIB_SSD1306_128X32 First_Oled(U8G_I2C_OPT_FAST);  // Init of the OLED #1 using I2C
U8GLIB_SSD1306_128X32 Second_Oled(U8G_I2C_OPT_FAST);  // Init of the OLED #2 using I2C

void setup() {

  Wire.begin();
  
// Using Input Pullups for the tact switches
  pinMode(change_switch, INPUT_PULLUP);
  pinMode(increase_count_switch, INPUT_PULLUP);

// Rotate screen 180 degrees on OLED, uncomment if required
  //u8gDHT.setRot180();

// Select font to use
  First_Oled.setFont(u8g_font_unifont);
  Second_Oled.setFont(u8g_font_unifont);
  
  I2CMulti.switchToBus(2);  // Switch to multiplexer i2c channel #2
  First_Oled.setContrast(100);  // Set oled contrast at startup

// Put stuff on OLED at startup    
  First_Oled.firstPage(); 
    do {
        First_Oled.drawStr( 88, 10, "|");  
        First_Oled.drawStr( 0, 32, "Oled #");
        First_Oled.setPrintPos(50, 32);
        First_Oled.print(Oled1c, 1);
        First_Oled.drawStr( 88, 32, "|");
      } 
    while( First_Oled.nextPage() );

  I2CMulti.switchToBus(7);
  Second_Oled.setContrast(0);
  
  Second_Oled.firstPage(); 
    do {
        Second_Oled.drawStr( 88, 10, "|");  
        Second_Oled.drawStr( 0, 32, "Oled #");
        Second_Oled.setPrintPos(50, 32);
        Second_Oled.print(Oled2c, 1);
        Second_Oled.drawStr( 88, 32, "|");
      }
    while( Second_Oled.nextPage() );

  I2CMulti.switchToBus(2); // Select i2c multiplexer channel to #2            
  
}


void loop() {

// if tact switch clicked change i2c channel
  if (digitalRead(change_switch) == LOW) {
    delay(200);
    
//change contrast and channel
  if (selected_oled == 2) {
      First_Oled.setContrast(0);
      I2CMulti.switchToBus(7);
      Second_Oled.setContrast(100);
      selected_oled=7;
    } else {
        Second_Oled.setContrast(0);
        I2CMulti.switchToBus(2);
        First_Oled.setContrast(100);
        selected_oled=2;       
    }
  }

// if tact switch clicked increase counter on selected OLED
  if (digitalRead(increase_count_switch) == LOW) {
    delay(50);
    if (selected_oled == 2) {
      Oled1c++;
      First_Oled.firstPage(); 
      do {
          First_Oled.drawStr( 88, 10, "|");  
          First_Oled.drawStr( 0, 32, "Oled #");
          First_Oled.setPrintPos(50, 32);
          First_Oled.print(Oled1c, 1);
          First_Oled.drawStr( 88, 32, "|");
        } while( First_Oled.nextPage() );
    } else {
      Oled2c++;
      Second_Oled.firstPage(); 
      do {
          Second_Oled.drawStr( 88, 10, "|");  
          Second_Oled.drawStr( 0, 32, "Oled #");
          Second_Oled.setPrintPos(50, 32);
          Second_Oled.print(Oled2c, 1);
          Second_Oled.drawStr( 88, 32, "|");
        } while( Second_Oled.nextPage() );
    }
  }
}
CONCLUSION

So if you have some I2C modules that have fixed address and you want to use more than one of them, this I2C Multiplexer board will enable you to do that pretty easily!

Like I mentionned in the tutorial video, you still need to know the address of the modules even if they are connected to the I2C Multiplexer, since changing the channel only connects the SDA and SCL lines and does not transfer the I2C address.

Thank you for stopping by!

TUTORIAL VIDEO
DOWNLOAD

Copy and Paste the above code/sketch in your Arduino IDE software.

Link to the libraries used in this tutorial:


By | 2019-04-23T19:28:42-04:00 April 22nd, 2019|Tutorials|

2 Comments

  1. Gary August 5, 2019 at 8:42 am - Reply

    Does it pull a lot of amperage when you’re running a loaded circuit

  2. Bob June 15, 2019 at 9:08 am - Reply

    Hi, an excellent range of tutorials, very well written. Specifically on the TCA9548 I2C multiplexer would this also help with the issue of pull up resistors in parallel when multiple break out boards with their own PU resistors are connected on the same I2C bus

Leave A Comment