{"id":105159,"date":"2021-09-08T14:56:08","date_gmt":"2021-09-08T14:56:08","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=105159"},"modified":"2021-09-08T15:27:14","modified_gmt":"2021-09-08T15:27:14","slug":"tca9548a-i2c-multiplexer-esp32-esp8266-arduino","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/tca9548a-i2c-multiplexer-esp32-esp8266-arduino\/","title":{"rendered":"Guide for TCA9548A I2C Multiplexer: ESP32, ESP8266, Arduino"},"content":{"rendered":"\n<p>In this guide, you&#8217;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.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/Guide-TCA9548A-I2C-Multiplexer-ESP32-EP8266-Arduino.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Guide for TCA9548A I2C Multiplexer ESP32 ESP8266 Arduino\" class=\"wp-image-105208\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/Guide-TCA9548A-I2C-Multiplexer-ESP32-EP8266-Arduino.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/Guide-TCA9548A-I2C-Multiplexer-ESP32-EP8266-Arduino.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/Guide-TCA9548A-I2C-Multiplexer-ESP32-EP8266-Arduino.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/Guide-TCA9548A-I2C-Multiplexer-ESP32-EP8266-Arduino.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n\n<p>This tutorial is compatible with ESP32, ESP8266, and Arduino boards. We&#8217;ll program the boards using  Arduino IDE.<\/p>\n\n\n\n<p><strong>Table of Contents<\/strong><\/p>\n\n\n\n<p>In this tutorial, we&#8217;ll cover the following topics:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"#TCA9548A-Intro\">Introducing the TCA9548A I2C Multiplexer<\/a><\/li><li><a href=\"#features\" title=\"#features\">TCA9548A Multiplexer Features<\/a><\/li><li><a href=\"#select-address\">TCA9548A Multiplexer I2C address<\/a><\/li><li><a href=\"#pinout\">TCA9548A Pinout<\/a><\/li><li><a href=\"#select-bus\">TCA9548A Multiplexer Selecting an I2C Bus<\/a><\/li><li><a href=\"#multiple-displays\">Example 1: Connecting multiple OLED displays<\/a><\/li><li><a href=\"#multiple-sensors\">Example 2: Connecting multiple I2C sensors (BME280)<\/a><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"TCA9548A-Intro\">Introducing the TCA9548A 1-to-8 I2C Multiplexer<\/h2>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"419\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/TCA9548A-I2C-Multiplexer-Sensor-Module.jpg?resize=750%2C419&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"TCA9548A I2C Multiplexer Sensor Module\" class=\"wp-image-105179\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/TCA9548A-I2C-Multiplexer-Sensor-Module.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/TCA9548A-I2C-Multiplexer-Sensor-Module.jpg?resize=300%2C168&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>The <a href=\"https:\/\/makeradvisor.com\/tools\/tca9548a-i2c-multiplexer\/\" target=\"_blank\" rel=\"noreferrer noopener\">TCA9548A I2C multiplexer<\/a> 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.<\/p>\n\n\n\n<p>To address a specific port, you just need to send a single byte to the multiplexer with the desired output port number.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"739\" height=\"363\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/TCA9548A-How-it-Works.png?resize=739%2C363&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"TCA9548A I2C Multiplexer How it Works\" class=\"wp-image-105182\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/TCA9548A-How-it-Works.png?w=739&amp;quality=100&amp;strip=all&amp;ssl=1 739w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/TCA9548A-How-it-Works.png?resize=300%2C147&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 739px) 100vw, 739px\" \/><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"features\">TCA9548A Multiplexer Features<\/h3>\n\n\n\n<p>Here&#8217;s a summary of its main features:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>1 to 8 bidireccional translating switches<\/li><li>Active-low reset input<\/li><li>Three address pins\u2014up to 8 TCA9548A devices on the same I2C bus<\/li><li>Channel selection through an I2C bus<\/li><li>Operating power supply voltage range: 1.65V to 5.5V<\/li><li>5V tolerant pins<\/li><\/ul>\n\n\n\n<p>For a more detailed description, consult the datasheet.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"select-address\">TCA9548A Multiplexer I2C Address<\/h3>\n\n\n\n<p>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.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\"><strong>A0<\/strong><\/td><td class=\"has-text-align-center\" data-align=\"center\"><strong>A1<\/strong><\/td><td class=\"has-text-align-center\" data-align=\"center\"><strong>A2<\/strong><\/td><td class=\"has-text-align-center\" data-align=\"center\"><strong>I2C Address<\/strong><\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">LOW<\/td><td class=\"has-text-align-center\" data-align=\"center\">LOW<\/td><td class=\"has-text-align-center\" data-align=\"center\">LOW<\/td><td class=\"has-text-align-center\" data-align=\"center\">0x70<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">HIGH<\/td><td class=\"has-text-align-center\" data-align=\"center\">LOW<\/td><td class=\"has-text-align-center\" data-align=\"center\">LOW<\/td><td class=\"has-text-align-center\" data-align=\"center\">0x71<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">LOW<\/td><td class=\"has-text-align-center\" data-align=\"center\">HIGH<\/td><td class=\"has-text-align-center\" data-align=\"center\">LOW<\/td><td class=\"has-text-align-center\" data-align=\"center\">0x72<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">HIGH<\/td><td class=\"has-text-align-center\" data-align=\"center\">HIGH<\/td><td class=\"has-text-align-center\" data-align=\"center\">LOW<\/td><td class=\"has-text-align-center\" data-align=\"center\">0x73<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">LOW<\/td><td class=\"has-text-align-center\" data-align=\"center\">LOW<\/td><td class=\"has-text-align-center\" data-align=\"center\">HIGH<\/td><td class=\"has-text-align-center\" data-align=\"center\">0x74<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">HIGH<\/td><td class=\"has-text-align-center\" data-align=\"center\">LOW<\/td><td class=\"has-text-align-center\" data-align=\"center\">HIGH<\/td><td class=\"has-text-align-center\" data-align=\"center\">0x75<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">LOW<\/td><td class=\"has-text-align-center\" data-align=\"center\">HIGH<\/td><td class=\"has-text-align-center\" data-align=\"center\">HIGH<\/td><td class=\"has-text-align-center\" data-align=\"center\">0x76<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">HIGH<\/td><td class=\"has-text-align-center\" data-align=\"center\">HIGH<\/td><td class=\"has-text-align-center\" data-align=\"center\">HIGH<\/td><td class=\"has-text-align-center\" data-align=\"center\">0x77<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>For example, if you connect <span class=\"rnthl rntclgray\">A0<\/span>, <span class=\"rnthl rntclgray\">A1<\/span>, and <span class=\"rnthl rntclgray\">A2<\/span> to GND, it sets address 0x70 for the multiplexer.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"pinout\">TCA9548A Pinout<\/h3>\n\n\n\n<p>The following table describes the TCA9584A Pinout.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Pin<\/strong><\/td><td><strong>Description<\/strong><\/td><\/tr><tr><td><strong>VIN<\/strong><\/td><td>Powers the multiplexer<\/td><\/tr><tr><td><strong>GND<\/strong><\/td><td>Connect to GND<\/td><\/tr><tr><td><strong>SDA<\/strong><\/td><td>Connect to the master microcontroller SDA pin<\/td><\/tr><tr><td><strong>SCL<\/strong><\/td><td>Connect to the master microcontroller SCL pin<\/td><\/tr><tr><td><strong>RST<\/strong><\/td><td>Active low RST pin\u2014can be used to reset the multiplexer<\/td><\/tr><tr><td><strong>A0<\/strong><\/td><td>Selects multiplexer I2C address\u2014connect to GND or VCC<\/td><\/tr><tr><td><strong>A1<\/strong><\/td><td>Selects multiplexer I2C address\u2014connect to GND or VCC<\/td><\/tr><tr><td><strong>A2<\/strong><\/td><td>Selects multiplexer I2C address\u2014connect to GND or VCC<\/td><\/tr><tr><td><strong>SD0<\/strong><\/td><td>SDA for channel 0<\/td><\/tr><tr><td><strong>SC0<\/strong><\/td><td>SCL for channel 0<\/td><\/tr><tr><td><strong>SD1<\/strong><\/td><td>SDA for channel 1<\/td><\/tr><tr><td><strong>SC1<\/strong><\/td><td>SCL for channel 1<\/td><\/tr><tr><td><strong>SD2<\/strong><\/td><td>SDA for channel 2<\/td><\/tr><tr><td><strong>SC2<\/strong><\/td><td>SCL for channel 2<\/td><\/tr><tr><td><strong>SD3<\/strong><\/td><td>SDA for channel 3<\/td><\/tr><tr><td><strong>SC3<\/strong><\/td><td>SCL for channel 3<\/td><\/tr><tr><td><strong>SD4<\/strong><\/td><td>SDA for channel 4<\/td><\/tr><tr><td><strong>SC4<\/strong><\/td><td>SCL for channel 4<\/td><\/tr><tr><td><strong>SD5<\/strong><\/td><td>SDA for channel 5<\/td><\/tr><tr><td><strong>SC5<\/strong><\/td><td>SCL for channel 5<\/td><\/tr><tr><td><strong>SD6<\/strong><\/td><td>SDA for channel 6<\/td><\/tr><tr><td><strong>SC6<\/strong><\/td><td>SCL for channel 6<\/td><\/tr><tr><td><strong>SD7<\/strong><\/td><td>SDA for channel 7<\/td><\/tr><tr><td><strong>SC7<\/strong><\/td><td>SCL for channel 7<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"select-bus\">TCA9548A I2C Multiplexer Selecting an I2C Bus<\/h2>\n\n\n\n<p>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).<\/p>\n\n\n\n<p>To do that, you can simply use the following user-defined function:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void TCA9548A(uint8_t bus){\n  Wire.beginTransmission(0x70);  \/\/ TCA9548A address is 0x70\n  Wire.write(1 &lt;&lt; bus);          \/\/ send byte to select bus\n  Wire.endTransmission();\n  Serial.print(bus);\n}<\/code><\/pre>\n\n\n\n<p>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):<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>TCA9548A(2);<\/code><\/pre>\n\n\n\n<p>You&#8217;ll see how this works with practical examples in the following sections.<\/p>\n\n\n\n<hr class=\"wp-block-separator is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"multiple-displays\">Control Multiple OLED Displays\u2014TCA9548A I2C Multiplexer<\/h2>\n\n\n\n<p>In this section, we&#8217;ll show you how to control multiple OLED displays. As an example, we&#8217;ll control four OLED displays, but you can connect up to 8 displays.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Parts Required<\/h4>\n\n\n\n<p>Here&#8217;s a list of the parts required for this example:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Microcontroller (<a href=\"https:\/\/makeradvisor.com\/tools\/esp32-dev-board-wi-fi-bluetooth\/\" target=\"_blank\" rel=\"noreferrer noopener\">ESP32<\/a>, <a href=\"https:\/\/makeradvisor.com\/tools\/esp8266-esp-12e-nodemcu-wi-fi-development-board\/\" target=\"_blank\" rel=\"noreferrer noopener\">ESP8266<\/a>, <a href=\"https:\/\/makeradvisor.com\/tools\/compatible-arduino-uno-r3-board\/\" target=\"_blank\" rel=\"noreferrer noopener\">Arduino<\/a>, or other);<\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/tca9548a-i2c-multiplexer\/\" target=\"_blank\" rel=\"noreferrer noopener\">TCA9548A I2C Multiplexer<\/a><\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/oled-display-128x64-0-96-inch\/\" target=\"_blank\" rel=\"noreferrer noopener\">Multiple OLED Displays<\/a> (up to 8)<\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\" rel=\"noreferrer noopener\">Breadboard<\/a><\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\" rel=\"noreferrer noopener\">Jumper wires<\/a><\/li><\/ul>\n\n\n<p>You can use the preceding links or go directly to <a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\">MakerAdvisor.com\/tools<\/a> to find all the parts for your projects at the best price!<\/p><p style=\"text-align:center;\"><a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/header-200.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\"><\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Multiple OLED Displays with I2C Multiplexer Circuit<\/h3>\n\n\n\n<p>Connect four OLED displays as shown in the following schematic diagram. We&#8217;re using buses number 2, 3, 4 and 5. You can choose any other port number.<\/p>\n\n\n\n<p>We&#8217;re also connecting <span class=\"rnthl rntclgray\">A0<\/span>, <span class=\"rnthl rntclgray\">A1<\/span>, and <span class=\"rnthl rntclgray\">A2<\/span> to GND. This selects address 0x70 for the multiplexer.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"1052\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/multiple-OLED-displays-TCA9548A-I2C-Multiplexer.jpg?resize=1200%2C1052&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Multiple OLED Displays TCA9548A I2C Multiplexer wiring circuit diagram\" class=\"wp-image-105193\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/multiple-OLED-displays-TCA9548A-I2C-Multiplexer.jpg?w=1380&amp;quality=100&amp;strip=all&amp;ssl=1 1380w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/multiple-OLED-displays-TCA9548A-I2C-Multiplexer.jpg?resize=300%2C263&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/multiple-OLED-displays-TCA9548A-I2C-Multiplexer.jpg?resize=1024%2C898&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/multiple-OLED-displays-TCA9548A-I2C-Multiplexer.jpg?resize=768%2C673&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n\n<p>Here are the default I2C pins depending on the microcontroller you&#8217;re using:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Microcontroller<\/strong><\/td><td><strong>I2C Pins<\/strong><\/td><\/tr><tr><td><strong>ESP32<\/strong><\/td><td>GPIO 22 (SCL), GPIO 21 (SDA)<\/td><\/tr><tr><td><strong>ESP8266<\/strong><\/td><td>GPIO 5 (D1) (SCL), GPIO 4 (D2) (SDA)<\/td><\/tr><tr><td><strong>Arduino Uno<\/strong><\/td><td>A5 (SCL), A4 (SDA)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Multiple OLED Displays with I2C Multiplexer Code<\/h3>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>To learn how to control an I2C display, you can read the following articles:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-ssd1306-oled-display-arduino-ide\/\">ESP32 OLED Display with Arduino IDE<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-0-96-inch-oled-display-with-arduino-ide\/\">ESP8266 0.96 inch OLED Display with Arduino IDE<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/guide-for-oled-display-with-arduino\/\">Guide for I2C OLED Display with Arduino<\/a><\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">Installing Libraries<\/h4>\n\n\n\n<p>We&#8217;ll use the following libraries to control the OLED display. Make sure you have these libraries installed:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/adafruit\/Adafruit_SSD1306\" target=\"_blank\">Adafruit_SSD1306 library<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/adafruit\/Adafruit-GFX-Library\" target=\"_blank\">Adafruit_GFX library<\/a><\/li><\/ul>\n\n\n\n<p>You can install the libraries using the Arduino Library Manager. Go to&nbsp;<strong>Sketch&nbsp;<\/strong>&gt;&nbsp;<strong>Include Library<\/strong>&nbsp;&gt;&nbsp;<strong>Manage Libraries<\/strong>&nbsp;and search for the library name.<\/p>\n\n\n\n<p>If you&#8217;re using VS Code with the PlatformIO extension, copy the following to the <span class=\"rnthl rntliteral\">platformio.ini<\/span> file to include the libraries.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>lib_deps = adafruit\/Adafruit SSD1306@^2.4.6\n\tadafruit\/Adafruit GFX Library@^1.10.10<\/code><\/pre>\n\n\n\n<p>After installing the libraries, you can proceed.<\/p>\n\n\n\n<p>Copy the following code to your Arduino IDE and upload it to your board. It will work straight away.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*********\r\n  Rui Santos\r\n  Complete project details at https:\/\/RandomNerdTutorials.com\/tca9548a-i2c-multiplexer-esp32-esp8266-arduino\/\r\n  \r\n  Permission is hereby granted, free of charge, to any person obtaining a copy\r\n  of this software and associated documentation files.\r\n  \r\n  The above copyright notice and this permission notice shall be included in all\r\n  copies or substantial portions of the Software.\r\n*********\/\r\n\r\n#include &lt;Wire.h&gt;\r\n#include &lt;Adafruit_GFX.h&gt;\r\n#include &lt;Adafruit_SSD1306.h&gt;\r\n\r\n#define SCREEN_WIDTH 128\r\n#define SCREEN_HEIGHT 64\r\n\r\nAdafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &amp;Wire, -1);\r\n\r\n\/\/ Select I2C BUS\r\nvoid TCA9548A(uint8_t bus){\r\n  Wire.beginTransmission(0x70);  \/\/ TCA9548A address\r\n  Wire.write(1 &lt;&lt; bus);          \/\/ send byte to select bus\r\n  Wire.endTransmission();\r\n  Serial.print(bus);\r\n}\r\n \r\nvoid setup() {\r\n  Serial.begin(115200);\r\n\r\n  \/\/ Start I2C communication with the Multiplexer\r\n  Wire.begin();\r\n\r\n  \/\/ Init OLED display on bus number 2\r\n  TCA9548A(2);\r\n  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {\r\n    Serial.println(F(&quot;SSD1306 allocation failed&quot;));\r\n    for(;;);\r\n  } \r\n  \/\/ Clear the buffer\r\n  display.clearDisplay();\r\n\r\n  \/\/ Init OLED display on bus number 3\r\n  TCA9548A(3);\r\n  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {\r\n    Serial.println(F(&quot;SSD1306 allocation failed&quot;));\r\n    for(;;);\r\n  } \r\n  \/\/ Clear the buffer\r\n  display.clearDisplay();\r\n\r\n  \/\/ Init OLED display on bus number 4\r\n  TCA9548A(4);\r\n  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {\r\n    Serial.println(F(&quot;SSD1306 allocation failed&quot;));\r\n    for(;;);\r\n  } \r\n  \/\/ Clear the buffer\r\n  display.clearDisplay();\r\n\r\n  \/\/ Init OLED display on bus number 5\r\n  TCA9548A(5);\r\n  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {\r\n    Serial.println(F(&quot;SSD1306 allocation failed&quot;));\r\n    for(;;);\r\n  } \r\n  \/\/ Clear the buffer\r\n  display.clearDisplay();\r\n\r\n  \/\/ Write to OLED on bus number 2\r\n  TCA9548A(2);\r\n  display.clearDisplay();\r\n  display.setTextSize(8);\r\n  display.setTextColor(WHITE);\r\n  display.setCursor(45, 10);\r\n  \/\/ Display static text\r\n  display.println(&quot;1&quot;);\r\n  display.display(); \r\n\r\n  \/\/ Write to OLED on bus number 3\r\n  TCA9548A(3);\r\n  display.clearDisplay();\r\n  display.setTextSize(8);\r\n  display.setTextColor(WHITE);\r\n  display.setCursor(45, 10);\r\n  \/\/ Display static text\r\n  display.println(&quot;2&quot;);\r\n  display.display(); \r\n  \r\n  \/\/ Write to OLED on bus number 4\r\n  TCA9548A(4);\r\n  display.clearDisplay();\r\n  display.setTextSize(8);\r\n  display.setTextColor(WHITE);\r\n  display.setCursor(45, 10);\r\n  \/\/ Display static text\r\n  display.println(&quot;3&quot;);\r\n  display.display(); \r\n  \r\n  \/\/ Write to OLED on bus number 5\r\n  TCA9548A(5);\r\n  display.clearDisplay();\r\n  display.setTextSize(8);\r\n  display.setTextColor(WHITE);\r\n  display.setCursor(45, 10);\r\n  \/\/ Display static text\r\n  display.println(&quot;4&quot;);\r\n  display.display(); \r\n}\r\n \r\nvoid loop() {\r\n  \r\n}\r\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/TCA9548A\/I2C_Multiplexer_Multiple_OLED.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How the Code Works<\/h3>\n\n\n\n<p>Continue reading to learn how the code works or skip to the <a href=\"#demonstration-oled\" title=\"#demonstration-oled\">Demonstration <\/a>section.<\/p>\n\n\n\n<p>First, import the required libraries to control the OLED display: <span class=\"rnthl rntliteral\">Adafruit_GFX<\/span> and <span class=\"rnthl rntliteral\">Adafruit_SSD1306<\/span>. The <span class=\"rnthl rntliteral\">Wire<\/span> library is needed to use the I2C communication protocol.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include &lt;Wire.h&gt;\n#include &lt;Adafruit_GFX.h&gt;\n#include &lt;Adafruit_SSD1306.h&gt;<\/code><\/pre>\n\n\n\n<p>Define the OLED width and height.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define SCREEN_WIDTH 128\n#define SCREEN_HEIGHT 64<\/code><\/pre>\n\n\n\n<p>Create an <span class=\"rnthl rntliteral\">Adafruit_SSD1306<\/span> instance to communicate with the OLED display.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &amp;Wire, -1);<\/code><\/pre>\n\n\n\n<p>You can use the same instance to communicate with all displays. In that case, you need to clear the display buffer (<span class=\"rnthl rntliteral\">display.clearDisplay()<\/span>) before writing to another OLED. <\/p>\n\n\n\n<p>Alternatively, you can create multiple <span class=\"rnthl rntliteral\">Adafruit_SSD1306<\/span> instances, one for each OLED. In that case, you don&#8217;t need to clear the buffer. We&#8217;ll show you an <a href=\"#multiple-instances\">example with multiple instances<\/a> at the end of this section.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Select the I2C Channel<\/h4>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">TCA9548A()<\/span> 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.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Select I2C BUS\nvoid TCA9548A(uint8_t bus){\n  Wire.beginTransmission(0x70);  \/\/ TCA9548A address\n  Wire.write(1 &lt;&lt; bus);          \/\/ send byte to select bus\n  Wire.endTransmission();\n  Serial.print(bus);\n}<\/code><\/pre>\n\n\n\n<p>You must call this function whenever you want to select the I2C port.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">setup()<\/h4>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, initialize a serial communication for debugging purposes.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.begin(115200);<\/code><\/pre>\n\n\n\n<p>Start I2C communication on the default I2C pins with the I2C multiplexer.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Wire.begin();<\/code><\/pre>\n\n\n\n<p>Then, initialize each display. The following lines show an example for the first OLED display (it is connected to bus number 2).<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/Init OLED display on bus number 2\nTCA9548A(2);\nif(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {\n  Serial.println(F(\"SSD1306 allocation failed\"));\n  for(;;);\n} \n\/\/ Clear the buffer\ndisplay.clearDisplay();<\/code><\/pre>\n\n\n\n<p>Initializing the other displays is similar, but you need to call the <span class=\"rnthl rntliteral\">TCA9548A()<\/span> function with the corresponding I2C bus number.<\/p>\n\n\n\n<p>Then, write something to the displays. Don&#8217;t forget you need to call the <span class=\"rnthl rntliteral\">TCA9548A()<\/span> function every time you want to switch between OLEDs. You also need to clear the display buffer before writing anything to the OLED.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Write to OLED on bus number 2\nTCA9548A(2);\ndisplay.clearDisplay();\ndisplay.setTextSize(8);\ndisplay.setTextColor(WHITE);\ndisplay.setCursor(45, 10);\n\/\/ Display static text\ndisplay.println(\"1\");\ndisplay.display(); <\/code><\/pre>\n\n\n\n<p>In this case, we&#8217;re just printing a different number on each display. Here&#8217;s an example for OLED number 4 (it is connected to bus number 5).<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Write to OLED on bus number 5\nTCA9548A(5);\ndisplay.clearDisplay();\ndisplay.setTextSize(8);\ndisplay.setTextColor(WHITE);\ndisplay.setCursor(45, 10);\n\/\/ Display static text\ndisplay.println(\"4\");\ndisplay.display();<\/code><\/pre>\n\n\n\n<p>And that&#8217;s pretty much how the code works.<\/p>\n\n\n\n<p id=\"multiple-instances\">The following code shows a similar example but using multiple <span class=\"rnthl rntliteral\">Adafruit_SSD1306<\/span> instances. Notice that you don&#8217;t need to clear the buffer before writing to the display.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*********\r\n  Rui Santos\r\n  Complete project details at https:\/\/RandomNerdTutorials.com\/tca9548a-i2c-multiplexer-esp32-esp8266-arduino\/\r\n  \r\n  Permission is hereby granted, free of charge, to any person obtaining a copy\r\n  of this software and associated documentation files.\r\n  \r\n  The above copyright notice and this permission notice shall be included in all\r\n  copies or substantial portions of the Software.\r\n*********\/\r\n\r\n#include &lt;Wire.h&gt;\r\n#include &lt;Adafruit_GFX.h&gt;\r\n#include &lt;Adafruit_SSD1306.h&gt;\r\n\r\n#define SCREEN_WIDTH 128\r\n#define SCREEN_HEIGHT 64\r\n\r\nAdafruit_SSD1306 display1(SCREEN_WIDTH, SCREEN_HEIGHT, &amp;Wire, -1);\r\nAdafruit_SSD1306 display2(SCREEN_WIDTH, SCREEN_HEIGHT, &amp;Wire, -1);\r\nAdafruit_SSD1306 display3(SCREEN_WIDTH, SCREEN_HEIGHT, &amp;Wire, -1);\r\nAdafruit_SSD1306 display4(SCREEN_WIDTH, SCREEN_HEIGHT, &amp;Wire, -1);\r\n\r\n\/\/ Select I2C BUS\r\nvoid TCA9548A(uint8_t bus){\r\n  Wire.beginTransmission(0x70);  \/\/ TCA9548A address\r\n  Wire.write(1 &lt;&lt; bus);          \/\/ send byte to select bus\r\n  Wire.endTransmission();\r\n  Serial.print(bus);\r\n}\r\n\r\nvoid setup() {\r\n  Serial.begin(115200);\r\n\r\n  \/\/ Start I2C communication with the Multiplexer\r\n  Wire.begin();\r\n\r\n  \/\/ Init OLED display on bus number 2 (display 1)\r\n  TCA9548A(2);\r\n  if(!display1.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {\r\n    Serial.println(F(&quot;SSD1306 allocation failed&quot;));\r\n    for(;;);\r\n  } \r\n  \/\/ Clear the buffer\r\n  display1.clearDisplay();\r\n\r\n  \/\/ Init OLED display on bus number 3\r\n  TCA9548A(3);\r\n  if(!display2.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {\r\n    Serial.println(F(&quot;SSD1306 allocation failed&quot;));\r\n    for(;;);\r\n  } \r\n  \/\/ Clear the buffer\r\n  display2.clearDisplay();\r\n\r\n  \/\/ Init OLED display on bus number 4\r\n  TCA9548A(4);\r\n  if(!display3.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {\r\n    Serial.println(F(&quot;SSD1306 allocation failed&quot;));\r\n    for(;;);\r\n  } \r\n  \/\/ Clear the buffer\r\n  display3.clearDisplay();\r\n\r\n  \/\/ Init OLED display on bus number 5\r\n  TCA9548A(5);\r\n  if(!display4.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {\r\n    Serial.println(F(&quot;SSD1306 allocation failed&quot;));\r\n    for(;;);\r\n  } \r\n  \/\/ Clear the buffer\r\n  display4.clearDisplay();\r\n\r\n  \/\/ Write to OLED on bus number 2\r\n  TCA9548A(2);\r\n  display1.setTextSize(8);\r\n  display1.setTextColor(WHITE);\r\n  display1.setCursor(45, 10);\r\n  \/\/ Display static text\r\n  display1.println(&quot;1&quot;);\r\n  display1.display(); \r\n  \r\n  \/\/ Write to OLED on bus number 3\r\n  TCA9548A(3);\r\n  display2.setTextSize(8);\r\n  display2.setTextColor(WHITE);\r\n  display2.setCursor(45, 10);\r\n  \/\/ Display static text\r\n  display2.println(&quot;2&quot;);\r\n  display2.display(); \r\n  \r\n  \/\/ Write to OLED on bus number 4\r\n  TCA9548A(4);\r\n  display3.setTextSize(8);\r\n  display3.setTextColor(WHITE);\r\n  display3.setCursor(45, 10);\r\n  \/\/ Display static text\r\n  display3.println(&quot;3&quot;);\r\n  display3.display(); \r\n  \r\n  \/\/ Write to OLED on bus number 5\r\n  TCA9548A(5);\r\n  display4.setTextSize(8);\r\n  display4.setTextColor(WHITE);\r\n  display4.setCursor(45, 10);\r\n  \/\/ Display static text\r\n  display4.println(&quot;4&quot;);\r\n  display4.display();\r\n}\r\n \r\nvoid loop() {\r\n  \r\n}\r\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/TCA9548A\/I2C_Multiplexer_Multiple_OLED_instances.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"demonstration-oled\">Demonstration<\/h3>\n\n\n\n<p>Upload the code to your board. Here&#8217;s what you should get.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/OLED-Demonstration-TCA9548A-I2C-Multiplexer-Sensor-Module.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"OLED SSD1306 Demonstration TCA9548A I2C Multiplexer Sensor Module\" class=\"wp-image-105178\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/OLED-Demonstration-TCA9548A-I2C-Multiplexer-Sensor-Module.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/OLED-Demonstration-TCA9548A-I2C-Multiplexer-Sensor-Module.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<p>As you can see, it is pretty easy to control multiple OLED displays showing different graphics using an I2C multiplexer.<\/p>\n\n\n\n<hr class=\"wp-block-separator is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"multiple-sensors\">Read Multiple BME280 Sensors \u2014TCA9548A I2C Multiplexer<\/h2>\n\n\n\n<p>In this section, you&#8217;ll learn how to read data from multiple BME280 sensors using the TCA9548A I2C multiplexer. We&#8217;ll read data from four sensors, but you can hook up to 8 sensors.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/BME280-Demonstration-TCA9548A-I2C-Multiplexer-Sensor-Module.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"BME280 Demonstration TCA9548A I2C Multiplexer Sensor Module\" class=\"wp-image-105198\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/BME280-Demonstration-TCA9548A-I2C-Multiplexer-Sensor-Module.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/BME280-Demonstration-TCA9548A-I2C-Multiplexer-Sensor-Module.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">Parts Required<\/h4>\n\n\n\n<p>Here&#8217;s a list of the parts required for this example:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Microcontroller (<a href=\"https:\/\/makeradvisor.com\/tools\/esp32-dev-board-wi-fi-bluetooth\/\" target=\"_blank\" rel=\"noreferrer noopener\">ESP32<\/a>, <a href=\"https:\/\/makeradvisor.com\/tools\/esp8266-esp-12e-nodemcu-wi-fi-development-board\/\" target=\"_blank\" rel=\"noreferrer noopener\">ESP8266<\/a>, <a href=\"https:\/\/makeradvisor.com\/tools\/compatible-arduino-uno-r3-board\/\" target=\"_blank\" rel=\"noreferrer noopener\">Arduino<\/a>, or other);<\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/tca9548a-i2c-multiplexer\/\" target=\"_blank\" rel=\"noreferrer noopener\">TCA9548A I2C Multiplexer<\/a><\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/bme280-sensor-module\/\" target=\"_blank\" rel=\"noreferrer noopener\">Multiple BME280 sensors<\/a> (up to 8)<\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\" rel=\"noreferrer noopener\">Breadboard<\/a><\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\" rel=\"noreferrer noopener\">Jumper wires<\/a><\/li><\/ul>\n\n\n<p>You can use the preceding links or go directly to <a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\">MakerAdvisor.com\/tools<\/a> to find all the parts for your projects at the best price!<\/p><p style=\"text-align:center;\"><a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/header-200.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\"><\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Multiple BME280 Sensors with I2C Multiplexer Circuit<\/h3>\n\n\n\n<p>Connect four BME280 sensors as shown in the following schematic diagram. We&#8217;re using buses number 2, 3, 4 and 5. You can choose any other port numbers.<\/p>\n\n\n\n<p>We&#8217;re also connecting <span class=\"rnthl rntclgray\">A0<\/span>, <span class=\"rnthl rntclgray\">A1<\/span>, and <span class=\"rnthl rntclgray\">A2<\/span> to GND. This selects address 0x70 for the multiplexer.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"754\" height=\"1174\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/multiple-bme280-TCA9548A-I2C-Multiplexer.png?resize=754%2C1174&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Multiple BME280 Sensors with TCA9548A I2C Multiplexer wiring circuit diagram\" class=\"wp-image-105190\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/multiple-bme280-TCA9548A-I2C-Multiplexer.png?w=754&amp;quality=100&amp;strip=all&amp;ssl=1 754w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/multiple-bme280-TCA9548A-I2C-Multiplexer.png?resize=193%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 193w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/multiple-bme280-TCA9548A-I2C-Multiplexer.png?resize=658%2C1024&amp;quality=100&amp;strip=all&amp;ssl=1 658w\" sizes=\"(max-width: 754px) 100vw, 754px\" \/><\/figure><\/div>\n\n\n\n<p>Here are the default I2C pins depending on the microcontroller you&#8217;re using:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Microcontroller<\/strong><\/td><td><strong>I2C Pins<\/strong><\/td><\/tr><tr><td><strong>ESP32<\/strong><\/td><td>GPIO 22 (SCL), GPIO 21 (SDA)<\/td><\/tr><tr><td><strong>ESP8266<\/strong><\/td><td>GPIO 5 (D1) (SCL), GPIO 4 (D2) (SDA)<\/td><\/tr><tr><td><strong>Arduino Uno<\/strong><\/td><td>A5 (SCL), A4 (SDA)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Multiple BME280 Sensors with I2C Multiplexer Code<\/h3>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>To learn how to read data from a BME280 sensor:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-bme280-arduino-ide-pressure-temperature-humidity\/\">ESP32 with BME280 Sensor using Arduino IDE (Pressure, Temperature, Humidity)<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-bme280-arduino-ide\/\">ESP8266 with BME280 using Arduino IDE (Pressure, Temperature, Humidity)<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/bme280-sensor-arduino-pressure-temperature-humidity\/\">Guide for BME280 Sensor with Arduino (Pressure, Temperature, Humidity)<\/a><\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">Installing Libraries<\/h4>\n\n\n\n<p>We&#8217;ll use the following libraries to read from the BME280 sensor. Make sure you have these libraries installed:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/github.com\/adafruit\/Adafruit_BME280_Library\" target=\"_blank\" rel=\"noreferrer noopener\">Adafruit_BME280 library<\/a><\/li><li><a href=\"https:\/\/github.com\/adafruit\/Adafruit_Sensor\" target=\"_blank\" rel=\"noreferrer noopener\">Adafruit_Sensor library<\/a><\/li><\/ul>\n\n\n\n<p>You can install the libraries using the Arduino Library Manager. Go to&nbsp;<strong>Sketch&nbsp;<\/strong>&gt;&nbsp;<strong>Include Library<\/strong>&nbsp;&gt;&nbsp;<strong>Manage Libraries<\/strong>&nbsp;and search for the library name.<\/p>\n\n\n\n<p>If you&#8217;re using VS Code with the PlatformIO extension, copy the following to the <span class=\"rnthl rntliteral\">platformio.ini<\/span> file to include the libraries.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>lib_deps = adafruit\/Adafruit Unified Sensor @ ^1.1.4\n            adafruit\/Adafruit BME280 Library @ ^2.1.2  <\/code><\/pre>\n\n\n\n<p>After installing the libraries, you can proceed.<\/p>\n\n\n\n<p>Copy the following code to your Arduino IDE and upload it to your board. It will work straight away.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*********\r\n  Rui Santos\r\n  Complete project details at https:\/\/RandomNerdTutorials.com\/tca9548a-i2c-multiplexer-esp32-esp8266-arduino\/\r\n  \r\n  Permission is hereby granted, free of charge, to any person obtaining a copy\r\n  of this software and associated documentation files.\r\n  \r\n  The above copyright notice and this permission notice shall be included in all\r\n  copies or substantial portions of the Software.\r\n*********\/\r\n\r\n#include &lt;Wire.h&gt;\r\n#include &lt;Adafruit_Sensor.h&gt;\r\n#include &lt;Adafruit_BME280.h&gt;\r\n\r\n#define SEALEVELPRESSURE_HPA (1022)\r\n\r\nAdafruit_BME280 bme1; \/\/ I2C\r\nAdafruit_BME280 bme2; \/\/ I2C\r\nAdafruit_BME280 bme3; \/\/ I2C\r\nAdafruit_BME280 bme4; \/\/ I2C\r\n\r\n\/\/ Select I2C BUS\r\nvoid TCA9548A(uint8_t bus){\r\n  Wire.beginTransmission(0x70);  \/\/ TCA9548A address\r\n  Wire.write(1 &lt;&lt; bus);          \/\/ send byte to select bus\r\n  Wire.endTransmission();\r\n}\r\n\r\nvoid printValues(Adafruit_BME280 bme, int bus) {\r\n  TCA9548A (bus);\r\n  Serial.print(&quot;Sensor number on bus&quot;);\r\n  Serial.println(bus);\r\n  Serial.print(&quot;Temperature = &quot;);\r\n  Serial.print(bme.readTemperature());\r\n  Serial.println(&quot; *C&quot;);\r\n  \r\n  Serial.print(&quot;Pressure = &quot;);\r\n  Serial.print(bme.readPressure() \/ 100.0F);\r\n  Serial.println(&quot; hPa&quot;);\r\n\r\n  Serial.print(&quot;Approx. Altitude = &quot;);\r\n  Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));\r\n  Serial.println(&quot; m&quot;);\r\n\r\n  Serial.print(&quot;Humidity = &quot;);\r\n  Serial.print(bme.readHumidity());\r\n  Serial.println(&quot; %&quot;);\r\n\r\n  Serial.println();\r\n}\r\n\r\nvoid setup() {\r\n  Serial.begin(115200);\r\n  \r\n  \/\/ Start I2C communication with the Multiplexer\r\n  Wire.begin();\r\n\r\n  \/\/ Init sensor on bus number 2\r\n  TCA9548A(2);\r\n  if (!bme1.begin(0x76)) {\r\n    Serial.println(&quot;Could not find a valid BME280 sensor on bus 2, check wiring!&quot;);\r\n    while (1);\r\n  }\r\n  Serial.println();\r\n  \r\n  \/\/ Init sensor on bus number 3\r\n  TCA9548A(3);\r\n  if (!bme2.begin(0x76)) {\r\n    Serial.println(&quot;Could not find a valid BME280 sensor on bus 2, check wiring!&quot;);\r\n    while (1);\r\n  }\r\n  Serial.println();\r\n  \r\n  \/\/ Init sensor on bus number 4\r\n  TCA9548A(4);\r\n  if (!bme3.begin(0x76)) {\r\n    Serial.println(&quot;Could not find a valid BME280 sensor on bus 2, check wiring!&quot;);\r\n    while (1);\r\n  }\r\n  Serial.println();\r\n  \r\n  \/\/ Init sensor on bus number 5\r\n  TCA9548A(5);\r\n  if (!bme4.begin(0x76)) {\r\n    Serial.println(&quot;Could not find a valid BME280 sensor on bus 2, check wiring!&quot;);\r\n    while (1);\r\n  }\r\n  Serial.println();\r\n}\r\n\r\nvoid loop() { \r\n  \/\/Print values for sensor 1\r\n  printValues(bme1, 2);\r\n  printValues(bme2, 3);\r\n  printValues(bme3, 4);\r\n  printValues(bme4, 5);\r\n  delay(5000);\r\n}\r\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/TCA9548A\/I2C_Multiplexer_Multiple_BME280.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How the Code Works<\/h3>\n\n\n\n<p>Continue reading to learn how the code works or skip to the <a href=\"#demonstration-sensor\">Demonstration<\/a> section.<\/p>\n\n\n\n<p>First, import the required libraries to control the BME280 display: <span class=\"rnthl rntliteral\">Adafruit_BME280<\/span> and <span class=\"rnthl rntliteral\">Adafruit_Sensor<\/span>. The <span class=\"rnthl rntliteral\">Wire<\/span> library is needed to use the I2C communication protocol.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include &lt;Wire.h&gt;\n#include &lt;Adafruit_Sensor.h&gt;\n#include &lt;Adafruit_BME280.h&gt;<\/code><\/pre>\n\n\n\n<p>Then, you need to create several instances of <span class=\"rnthl rntliteral\">Adafruit_BME280<\/span>, one for each sensor: <span class=\"rnthl rntliteral\">bme1<\/span>, <span class=\"rnthl rntliteral\">bme2<\/span>, <span class=\"rnthl rntliteral\">bme3<\/span>, and <span class=\"rnthl rntliteral\">bme4<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Adafruit_BME280 bme1; \/\/ I2C\nAdafruit_BME280 bme2; \/\/ I2C\nAdafruit_BME280 bme3; \/\/ I2C\nAdafruit_BME280 bme4; \/\/ I2C<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Select the I2C Channel<\/h4>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">TCA9548A()<\/span> 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.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Select I2C BUS\nvoid TCA9548A(uint8_t bus){\n  Wire.beginTransmission(0x70);  \/\/ TCA9548A address\n  Wire.write(1 &lt;&lt; bus);          \/\/ send byte to select bus\n  Wire.endTransmission();\n  Serial.print(bus);\n}<\/code><\/pre>\n\n\n\n<p>You must call this function whenever you want to select the I2C port.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">printValues() function<\/h4>\n\n\n\n<p>Then, we create a function <span class=\"rnthl rntliteral\">printValues()<\/span> that allows us to print in the Serial Monitor the values for each sensor. This function allows us to pass the <span class=\"rnthl rntliteral\">Adafruit_BME280<\/span> instance and its bus.<\/p>\n\n\n\n<p>Inside the function, we select the I2C bus we want to talk to by calling the <span class=\"rnthl rntliteral\">TCA9548A()<\/span> function and passing the bus as an argument.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>TCA9548A (bus);<\/code><\/pre>\n\n\n\n<p>Then, we use the usual functions to get readings from the sensor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.print(\"Sensor number on bus\");\nSerial.println(bus);\nSerial.print(\"Temperature = \");\nSerial.print(bme.readTemperature());\nSerial.println(\" *C\");\n  \nSerial.print(\"Pressure = \");\nSerial.print(bme.readPressure() \/ 100.0F);\nSerial.println(\" hPa\");\n\nSerial.print(\"Approx. Altitude = \");\nSerial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));\nSerial.println(\" m\");\n\nSerial.print(\"Humidity = \");\nSerial.print(bme.readHumidity());\nSerial.println(\" %\");<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">setup()<\/h4>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, initialize a serial communication for debugging purposes.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.begin(115200);<\/code><\/pre>\n\n\n\n<p>Start I2C communication on the default I2C pins with the I2C multiplexer.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Wire.begin();<\/code><\/pre>\n\n\n\n<p>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 <span class=\"rnthl rntliteral\">bme1<\/span> instance).<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/Init sensor on bus number 2\nTCA9548A(2);\nif (!bme1.begin(0x76)) {\n  Serial.println(\"Could not find a valid BME280 sensor on bus 2, check wiring!\");\n  while (1);\n}\nSerial.println();<\/code><\/pre>\n\n\n\n<p>Initializing the other sensors is similar, but you need to call the <span class=\"rnthl rntliteral\">TCA9548A()<\/span> function with the corresponding I2C bus number. Also, don&#8217;t forget that each sensor has its own instance.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">loop()<\/h4>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">loop()<\/span>, we call the <span class=\"rnthl rntliteral\">printValues()<\/span> function for each sensor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>printValues(bme1, 2);\nprintValues(bme2, 3);\nprintValues(bme3, 4);\nprintValues(bme4, 5);<\/code><\/pre>\n\n\n\n<p>And that&#8217;s pretty much how the code works.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"demonstration-sensor\">Demonstration<\/h3>\n\n\n\n<p>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.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"601\" height=\"759\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/Multiple-BME280-Sensors-I2C-Multiplexer-Demonstration.png?resize=601%2C759&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Multiple BME280 Sensors I2C Multiplexer Demonstration\" class=\"wp-image-105237\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/Multiple-BME280-Sensors-I2C-Multiplexer-Demonstration.png?w=601&amp;quality=100&amp;strip=all&amp;ssl=1 601w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/Multiple-BME280-Sensors-I2C-Multiplexer-Demonstration.png?resize=238%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 238w\" sizes=\"(max-width: 601px) 100vw, 601px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>The examples shown throughout this tutorial are compatible with the ESP32, ESP8266, and Arduino boards.<\/p>\n\n\n\n<p>We have an extensive tutorial about I2C functions with the ESP32:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-i2c-communication-arduino-ide\/\" target=\"_blank\" rel=\"noreferrer noopener\">ESP32 I2C Communication: Set Pins, Multiple Bus Interfaces and Peripherals (Arduino IDE)<\/a><\/li><\/ul>\n\n\n\n<p>Learn more about ESP32, ESP8266, and Arduino with our resources:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><\/td><td>Free Projects<\/td><td>Course\/eBook<\/td><\/tr><tr><td><strong>ESP32<\/strong><\/td><td><a href=\"https:\/\/randomnerdtutorials.com\/projects-esp32\/\">ESP32 Tutorials<\/a><\/td><td><a href=\"https:\/\/randomnerdtutorials.com\/learn-esp32-with-arduino-ide\/\">Learn ESP32 with Arduino IDE<\/a><\/td><\/tr><tr><td><strong>ESP8266<\/strong><\/td><td><a href=\"https:\/\/randomnerdtutorials.com\/projects-esp8266\/\">ESP8266 Tutorials<\/a><\/td><td><a href=\"https:\/\/randomnerdtutorials.com\/home-automation-using-esp8266\/\">Home Automation using ESP8266<\/a><\/td><\/tr><tr><td><strong>Arduino<\/strong><\/td><td><a href=\"https:\/\/randomnerdtutorials.com\/projects-arduino\/\">Arduino Tutorials<\/a><\/td><td><a href=\"https:\/\/randomnerdtutorials.com\/arduino-step-by-step-projects\/\">Arduino Step-by-step Projects<\/a><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>We hope you find this tutorial useful. <\/p>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, you&#8217;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 &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Guide for TCA9548A I2C Multiplexer: ESP32, ESP8266, Arduino\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/tca9548a-i2c-multiplexer-esp32-esp8266-arduino\/#more-105159\" aria-label=\"Read more about Guide for TCA9548A I2C Multiplexer: ESP32, ESP8266, Arduino\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":105208,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[276,281,277,299,264],"tags":[],"class_list":["post-105159","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp32","category-esp32-project","category-esp32-arduino-ide","category-0-esp32","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/Guide-TCA9548A-I2C-Multiplexer-ESP32-EP8266-Arduino.jpg?fit=1280%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/105159","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=105159"}],"version-history":[{"count":32,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/105159\/revisions"}],"predecessor-version":[{"id":106508,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/105159\/revisions\/106508"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/105208"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=105159"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=105159"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=105159"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}