Guide For TCA9548A I2C Multiplexer: ESP32, ESP8266, Arduino

In this guide, you’ll learn how to expand the I2C bus ports (ESP32, ESP8266, Arduino) using the TCA9458A 1-to-8 I2C Multiplexer. This piece of hardware is useful if you want to control multiple I2C devices with the same I2C address. For example, multiple OLED displays, or multiple sensors like the BME280.

Guide for TCA9548A I2C Multiplexer ESP32 ESP8266 Arduino

This tutorial is compatible with ESP32, ESP8266, and Arduino boards. We’ll program the boards using Arduino IDE.

Table of Contents

In this tutorial, we’ll cover the following topics:

  • Introducing the TCA9548A I2C Multiplexer
  • TCA9548A Multiplexer Features
  • TCA9548A Multiplexer I2C address
  • TCA9548A Pinout
  • TCA9548A Multiplexer Selecting an I2C Bus
  • Example 1: Connecting multiple OLED displays
  • Example 2: Connecting multiple I2C sensors (BME280)

Introducing the TCA9548A 1-to-8 I2C Multiplexer

TCA9548A I2C Multiplexer Sensor Module

The I2C communication protocol allows you to communicate with multiple I2C devices on the same I2C bus as long as all devices have a unique I2C address. However, it will not work if you want to connect multiple I2C devices with the same address.

The TCA9548A I2C multiplexer allows you to communicate with up to 8 I2C devices with the same I2C bus. The multiplexer communicates with a microcontroller using the I2C communication protocol. Then, you can select which I2C bus on the multiplexer you want to address.

To address a specific port, you just need to send a single byte to the multiplexer with the desired output port number.

TCA9548A I2C Multiplexer How it Works

TCA9548A Multiplexer Features

Here’s a summary of its main features:

  • 1 to 8 bidireccional translating switches
  • Active-low reset input
  • Three address pins—up to 8 TCA9548A devices on the same I2C bus
  • Channel selection through an I2C bus
  • Operating power supply voltage range: 1.65V to 5.5V
  • 5V tolerant pins

For a more detailed description, consult the datasheet.

TCA9548A Multiplexer I2C Address

The TCA9548A Multiplexer communicates with a microcontroller using the I2C communication protocol. So, it needs an I2C address. The address of the multiplexer is configurable. You can select a value from 0x70 to 0x77 by adjusting the values of the A0, A1, and A2 pins, as shown in the table below.

A0A1A2I2C Address
LOWLOWLOW0x70
HIGHLOWLOW0x71
LOWHIGHLOW0x72
HIGHHIGHLOW0x73
LOWLOWHIGH0x74
HIGHLOWHIGH0x75
LOWHIGHHIGH0x76
HIGHHIGHHIGH0x77

So, you can connect up to 8 TCA9548A multiplexers to the same I2C bus, which would allow you to connect 64 devices with the same address using only one I2C bus of the microcontroller.

For example, if you connect A0, A1, and A2 to GND, it sets address 0x70 for the multiplexer.

TCA9548A Pinout

The following table describes the TCA9584A Pinout.

PinDescription
VINPowers the multiplexer
GNDConnect to GND
SDAConnect to the master microcontroller SDA pin
SCLConnect to the master microcontroller SCL pin
RSTActive low RST pin—can be used to reset the multiplexer
A0Selects multiplexer I2C address—connect to GND or VCC
A1Selects multiplexer I2C address—connect to GND or VCC
A2Selects multiplexer I2C address—connect to GND or VCC
SD0SDA for channel 0
SC0SCL for channel 0
SD1SDA for channel 1
SC1SCL for channel 1
SD2SDA for channel 2
SC2SCL for channel 2
SD3SDA for channel 3
SC3SCL for channel 3
SD4SDA for channel 4
SC4SCL for channel 4
SD5SDA for channel 5
SC5SCL for channel 5
SD6SDA for channel 6
SC6SCL for channel 6
SD7SDA for channel 7
SC7SCL for channel 7

TCA9548A I2C Multiplexer Selecting an I2C Bus

As mentioned previously, to select a specific I2C bus to read/write data, you just need to send a single byte to the multiplexer with the desired output port number (0 to 7).

To do that, you can simply use the following user-defined function:

void TCA9548A(uint8_t bus){ Wire.beginTransmission(0x70); // TCA9548A address is 0x70 Wire.write(1 << bus); // send byte to select bus Wire.endTransmission(); Serial.print(bus); }

Then, you just need to call that function and pass as an argument the port bus number you want to control before sending the I2C commands. For example, to control the device connected to bus number 3, you would call the following line before calling other I2C commands (note that it starts at 0):

TCA9548A(2);

You’ll see how this works with practical examples in the following sections.

Control Multiple OLED Displays—TCA9548A I2C Multiplexer

In this section, we’ll show you how to control multiple OLED displays. As an example, we’ll control four OLED displays, but you can connect up to 8 displays.

Parts Required

Here’s a list of the parts required for this example:

  • Microcontroller (ESP32, ESP8266, Arduino, or other);
  • TCA9548A I2C Multiplexer
  • Multiple OLED Displays (up to 8)
  • Breadboard
  • Jumper wires

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Multiple OLED Displays with I2C Multiplexer Circuit

Connect four OLED displays as shown in the following schematic diagram. We’re using buses number 2, 3, 4 and 5. You can choose any other port number.

We’re also connecting A0, A1, and A2 to GND. This selects address 0x70 for the multiplexer.

Multiple OLED Displays TCA9548A I2C Multiplexer wiring circuit diagram

Here are the default I2C pins depending on the microcontroller you’re using:

MicrocontrollerI2C Pins
ESP32GPIO 22 (SCL), GPIO 21 (SDA)
ESP8266GPIO 5 (D1) (SCL), GPIO 4 (D2) (SDA)
Arduino UnoA5 (SCL), A4 (SDA)

Multiple OLED Displays with I2C Multiplexer Code

Controlling the displays is as easy as controlling one display. You just need to consider selecting the right I2C bus before sending the commands to write to the display.

To learn how to control an I2C display, you can read the following articles:

  • ESP32 OLED Display with Arduino IDE
  • ESP8266 0.96 inch OLED Display with Arduino IDE
  • Guide for I2C OLED Display with Arduino

Installing Libraries

We’ll use the following libraries to control the OLED display. Make sure you have these libraries installed:

  • Adafruit_SSD1306 library
  • Adafruit_GFX library

You can install the libraries using the Arduino Library Manager. Go to Sketch > Include Library > Manage Libraries and search for the library name.

If you’re using VS Code with the PlatformIO extension, copy the following to the platformio.ini file to include the libraries.

lib_deps = adafruit/Adafruit SSD1306@^2.4.6 adafruit/Adafruit GFX Library@^1.10.10

After installing the libraries, you can proceed.

Copy the following code to your Arduino IDE and upload it to your board. It will work straight away.

/********* Rui Santos Complete project details at https://RandomNerdTutorials.com/tca9548a-i2c-multiplexer-esp32-esp8266-arduino/ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. *********/ #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // Select I2C BUS void TCA9548A(uint8_t bus){ Wire.beginTransmission(0x70); // TCA9548A address Wire.write(1 << bus); // send byte to select bus Wire.endTransmission(); Serial.print(bus); } void setup() { Serial.begin(115200); // Start I2C communication with the Multiplexer Wire.begin(); // Init OLED display on bus number 2 TCA9548A(2); if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); for(;;); } // Clear the buffer display.clearDisplay(); // Init OLED display on bus number 3 TCA9548A(3); if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); for(;;); } // Clear the buffer display.clearDisplay(); // Init OLED display on bus number 4 TCA9548A(4); if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); for(;;); } // Clear the buffer display.clearDisplay(); // Init OLED display on bus number 5 TCA9548A(5); if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); for(;;); } // Clear the buffer display.clearDisplay(); // Write to OLED on bus number 2 TCA9548A(2); display.clearDisplay(); display.setTextSize(8); display.setTextColor(WHITE); display.setCursor(45, 10); // Display static text display.println("1"); display.display(); // Write to OLED on bus number 3 TCA9548A(3); display.clearDisplay(); display.setTextSize(8); display.setTextColor(WHITE); display.setCursor(45, 10); // Display static text display.println("2"); display.display(); // Write to OLED on bus number 4 TCA9548A(4); display.clearDisplay(); display.setTextSize(8); display.setTextColor(WHITE); display.setCursor(45, 10); // Display static text display.println("3"); display.display(); // Write to OLED on bus number 5 TCA9548A(5); display.clearDisplay(); display.setTextSize(8); display.setTextColor(WHITE); display.setCursor(45, 10); // Display static text display.println("4"); display.display(); } void loop() { }

View raw code

How the Code Works

Continue reading to learn how the code works or skip to the Demonstration section.

First, import the required libraries to control the OLED display: Adafruit_GFX and Adafruit_SSD1306. The Wire library is needed to use the I2C communication protocol.

#include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h>

Define the OLED width and height.

#define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64

Create an Adafruit_SSD1306 instance to communicate with the OLED display.

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

You can use the same instance to communicate with all displays. In that case, you need to clear the display buffer (display.clearDisplay()) before writing to another OLED.

Alternatively, you can create multiple Adafruit_SSD1306 instances, one for each OLED. In that case, you don’t need to clear the buffer. We’ll show you an example with multiple instances at the end of this section.

Select the I2C Channel

The TCA9548A() function can be called to select the bus that you want to communicate with. It sends a byte to the multiplexer with the port number.

// Select I2C BUS void TCA9548A(uint8_t bus){ Wire.beginTransmission(0x70); // TCA9548A address Wire.write(1 << bus); // send byte to select bus Wire.endTransmission(); Serial.print(bus); }

You must call this function whenever you want to select the I2C port.

setup()

In the setup(), initialize a serial communication for debugging purposes.

Serial.begin(115200);

Start I2C communication on the default I2C pins with the I2C multiplexer.

Wire.begin();

Then, initialize each display. The following lines show an example for the first OLED display (it is connected to bus number 2).

//Init OLED display on bus number 2 TCA9548A(2); if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); for(;;); } // Clear the buffer display.clearDisplay();

Initializing the other displays is similar, but you need to call the TCA9548A() function with the corresponding I2C bus number.

Then, write something to the displays. Don’t forget you need to call the TCA9548A() function every time you want to switch between OLEDs. You also need to clear the display buffer before writing anything to the OLED.

// Write to OLED on bus number 2 TCA9548A(2); display.clearDisplay(); display.setTextSize(8); display.setTextColor(WHITE); display.setCursor(45, 10); // Display static text display.println("1"); display.display();

In this case, we’re just printing a different number on each display. Here’s an example for OLED number 4 (it is connected to bus number 5).

// Write to OLED on bus number 5 TCA9548A(5); display.clearDisplay(); display.setTextSize(8); display.setTextColor(WHITE); display.setCursor(45, 10); // Display static text display.println("4"); display.display();

And that’s pretty much how the code works.

The following code shows a similar example but using multiple Adafruit_SSD1306 instances. Notice that you don’t need to clear the buffer before writing to the display.

/********* Rui Santos Complete project details at https://RandomNerdTutorials.com/tca9548a-i2c-multiplexer-esp32-esp8266-arduino/ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. *********/ #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 Adafruit_SSD1306 display1(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); Adafruit_SSD1306 display2(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); Adafruit_SSD1306 display3(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); Adafruit_SSD1306 display4(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // Select I2C BUS void TCA9548A(uint8_t bus){ Wire.beginTransmission(0x70); // TCA9548A address Wire.write(1 << bus); // send byte to select bus Wire.endTransmission(); Serial.print(bus); } void setup() { Serial.begin(115200); // Start I2C communication with the Multiplexer Wire.begin(); // Init OLED display on bus number 2 (display 1) TCA9548A(2); if(!display1.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); for(;;); } // Clear the buffer display1.clearDisplay(); // Init OLED display on bus number 3 TCA9548A(3); if(!display2.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); for(;;); } // Clear the buffer display2.clearDisplay(); // Init OLED display on bus number 4 TCA9548A(4); if(!display3.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); for(;;); } // Clear the buffer display3.clearDisplay(); // Init OLED display on bus number 5 TCA9548A(5); if(!display4.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); for(;;); } // Clear the buffer display4.clearDisplay(); // Write to OLED on bus number 2 TCA9548A(2); display1.setTextSize(8); display1.setTextColor(WHITE); display1.setCursor(45, 10); // Display static text display1.println("1"); display1.display(); // Write to OLED on bus number 3 TCA9548A(3); display2.setTextSize(8); display2.setTextColor(WHITE); display2.setCursor(45, 10); // Display static text display2.println("2"); display2.display(); // Write to OLED on bus number 4 TCA9548A(4); display3.setTextSize(8); display3.setTextColor(WHITE); display3.setCursor(45, 10); // Display static text display3.println("3"); display3.display(); // Write to OLED on bus number 5 TCA9548A(5); display4.setTextSize(8); display4.setTextColor(WHITE); display4.setCursor(45, 10); // Display static text display4.println("4"); display4.display(); } void loop() { }

View raw code

Demonstration

Upload the code to your board. Here’s what you should get.

OLED SSD1306 Demonstration TCA9548A I2C Multiplexer Sensor Module

As you can see, it is pretty easy to control multiple OLED displays showing different graphics using an I2C multiplexer.

Read Multiple BME280 Sensors —TCA9548A I2C Multiplexer

In this section, you’ll learn how to read data from multiple BME280 sensors using the TCA9548A I2C multiplexer. We’ll read data from four sensors, but you can hook up to 8 sensors.

BME280 Demonstration TCA9548A I2C Multiplexer Sensor Module

Parts Required

Here’s a list of the parts required for this example:

  • Microcontroller (ESP32, ESP8266, Arduino, or other);
  • TCA9548A I2C Multiplexer
  • Multiple BME280 sensors (up to 8)
  • Breadboard
  • Jumper wires

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Multiple BME280 Sensors with I2C Multiplexer Circuit

Connect four BME280 sensors as shown in the following schematic diagram. We’re using buses number 2, 3, 4 and 5. You can choose any other port numbers.

We’re also connecting A0, A1, and A2 to GND. This selects address 0x70 for the multiplexer.

Multiple BME280 Sensors with TCA9548A I2C Multiplexer wiring circuit diagram

Here are the default I2C pins depending on the microcontroller you’re using:

MicrocontrollerI2C Pins
ESP32GPIO 22 (SCL), GPIO 21 (SDA)
ESP8266GPIO 5 (D1) (SCL), GPIO 4 (D2) (SDA)
Arduino UnoA5 (SCL), A4 (SDA)

Multiple BME280 Sensors with I2C Multiplexer Code

Similar to the OLED display, reading data from multiple sensors is as easy as controlling one single sensor. You just need to take into account selecting the right I2C bus before communicating with the sensor.

To learn how to read data from a BME280 sensor:

  • ESP32 with BME280 Sensor using Arduino IDE (Pressure, Temperature, Humidity)
  • ESP8266 with BME280 using Arduino IDE (Pressure, Temperature, Humidity)
  • Guide for BME280 Sensor with Arduino (Pressure, Temperature, Humidity)

Installing Libraries

We’ll use the following libraries to read from the BME280 sensor. Make sure you have these libraries installed:

  • Adafruit_BME280 library
  • Adafruit_Sensor library

You can install the libraries using the Arduino Library Manager. Go to Sketch > Include Library > Manage Libraries and search for the library name.

If you’re using VS Code with the PlatformIO extension, copy the following to the platformio.ini file to include the libraries.

lib_deps = adafruit/Adafruit Unified Sensor @ ^1.1.4 adafruit/Adafruit BME280 Library @ ^2.1.2

After installing the libraries, you can proceed.

Copy the following code to your Arduino IDE and upload it to your board. It will work straight away.

/********* Rui Santos Complete project details at https://RandomNerdTutorials.com/tca9548a-i2c-multiplexer-esp32-esp8266-arduino/ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. *********/ #include <Wire.h> #include <Adafruit_Sensor.h> #include <Adafruit_BME280.h> #define SEALEVELPRESSURE_HPA (1022) Adafruit_BME280 bme1; // I2C Adafruit_BME280 bme2; // I2C Adafruit_BME280 bme3; // I2C Adafruit_BME280 bme4; // I2C // Select I2C BUS void TCA9548A(uint8_t bus){ Wire.beginTransmission(0x70); // TCA9548A address Wire.write(1 << bus); // send byte to select bus Wire.endTransmission(); } void printValues(Adafruit_BME280 bme, int bus) { TCA9548A (bus); Serial.print("Sensor number on bus"); Serial.println(bus); Serial.print("Temperature = "); Serial.print(bme.readTemperature()); Serial.println(" *C"); Serial.print("Pressure = "); Serial.print(bme.readPressure() / 100.0F); Serial.println(" hPa"); Serial.print("Approx. Altitude = "); Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA)); Serial.println(" m"); Serial.print("Humidity = "); Serial.print(bme.readHumidity()); Serial.println(" %"); Serial.println(); } void setup() { Serial.begin(115200); // Start I2C communication with the Multiplexer Wire.begin(); // Init sensor on bus number 2 TCA9548A(2); if (!bme1.begin(0x76)) { Serial.println("Could not find a valid BME280 sensor on bus 2, check wiring!"); while (1); } Serial.println(); // Init sensor on bus number 3 TCA9548A(3); if (!bme2.begin(0x76)) { Serial.println("Could not find a valid BME280 sensor on bus 2, check wiring!"); while (1); } Serial.println(); // Init sensor on bus number 4 TCA9548A(4); if (!bme3.begin(0x76)) { Serial.println("Could not find a valid BME280 sensor on bus 2, check wiring!"); while (1); } Serial.println(); // Init sensor on bus number 5 TCA9548A(5); if (!bme4.begin(0x76)) { Serial.println("Could not find a valid BME280 sensor on bus 2, check wiring!"); while (1); } Serial.println(); } void loop() { //Print values for sensor 1 printValues(bme1, 2); printValues(bme2, 3); printValues(bme3, 4); printValues(bme4, 5); delay(5000); }

View raw code

How the Code Works

Continue reading to learn how the code works or skip to the Demonstration section.

First, import the required libraries to control the BME280 display: Adafruit_BME280 and Adafruit_Sensor. The Wire library is needed to use the I2C communication protocol.

#include <Wire.h> #include <Adafruit_Sensor.h> #include <Adafruit_BME280.h>

Then, you need to create several instances of Adafruit_BME280, one for each sensor: bme1, bme2, bme3, and bme4.

Adafruit_BME280 bme1; // I2C Adafruit_BME280 bme2; // I2C Adafruit_BME280 bme3; // I2C Adafruit_BME280 bme4; // I2C

Select the I2C Channel

The TCA9548A() function can be called to select the bus that you want to communicate with. It sends a byte to the multiplexer with the port number.

// Select I2C BUS void TCA9548A(uint8_t bus){ Wire.beginTransmission(0x70); // TCA9548A address Wire.write(1 << bus); // send byte to select bus Wire.endTransmission(); Serial.print(bus); }

You must call this function whenever you want to select the I2C port.

printValues() function

Then, we create a function printValues() that allows us to print in the Serial Monitor the values for each sensor. This function allows us to pass the Adafruit_BME280 instance and its bus.

Inside the function, we select the I2C bus we want to talk to by calling the TCA9548A() function and passing the bus as an argument.

TCA9548A (bus);

Then, we use the usual functions to get readings from the sensor.

Serial.print("Sensor number on bus"); Serial.println(bus); Serial.print("Temperature = "); Serial.print(bme.readTemperature()); Serial.println(" *C"); Serial.print("Pressure = "); Serial.print(bme.readPressure() / 100.0F); Serial.println(" hPa"); Serial.print("Approx. Altitude = "); Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA)); Serial.println(" m"); Serial.print("Humidity = "); Serial.print(bme.readHumidity()); Serial.println(" %");

setup()

In the setup(), initialize a serial communication for debugging purposes.

Serial.begin(115200);

Start I2C communication on the default I2C pins with the I2C multiplexer.

Wire.begin();

Then, initialize each sensor. The following lines show an example for the first BME280 sensor (it is connected to bus number 2, and it refers to the bme1 instance).

//Init sensor on bus number 2 TCA9548A(2); if (!bme1.begin(0x76)) { Serial.println("Could not find a valid BME280 sensor on bus 2, check wiring!"); while (1); } Serial.println();

Initializing the other sensors is similar, but you need to call the TCA9548A() function with the corresponding I2C bus number. Also, don’t forget that each sensor has its own instance.

loop()

In the loop(), we call the printValues() function for each sensor.

printValues(bme1, 2); printValues(bme2, 3); printValues(bme3, 4); printValues(bme4, 5);

And that’s pretty much how the code works.

Demonstration

Upload the code to your board. Open the Serial Monitor at a baud rate of 115200. The readings for each sensor will be displayed on the Serial Monitor.

Multiple BME280 Sensors I2C Multiplexer Demonstration

Wrapping Up

This tutorial taught you how to add more I2C ports to your microcontroller with the TCA9548A I2C multiplexer. This is especially useful if you want to connect multiple devices with the same I2C address. Furthermore, the I2C address of the multiplexer itself can be changed from 0x70 to 0x77. This allows us to connect up to 8 multiplexers simultaneously, which allows you to control 64 devices.

The examples shown throughout this tutorial are compatible with the ESP32, ESP8266, and Arduino boards.

We have an extensive tutorial about I2C functions with the ESP32:

  • ESP32 I2C Communication: Set Pins, Multiple Bus Interfaces and Peripherals (Arduino IDE)

Learn more about ESP32, ESP8266, and Arduino with our resources:

Free ProjectsCourse/eBook
ESP32ESP32 TutorialsLearn ESP32 with Arduino IDE
ESP8266ESP8266 TutorialsHome Automation using ESP8266
ArduinoArduino TutorialsArduino Step-by-step Projects

We hope you find this tutorial useful.

Thanks for reading.

Tag » Arduino I2c Read Write Example