This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TCA9548A: Multiple Layers of Multiplexers - Code

Part Number: TCA9548A

Tool/software:

Hello, I am trying to read data from 18 IMUs. The IMUs all have the same addresses, so I am using the TCA9548A. For my setup, I want one primary multiplexer and three secondary multiplexers. The secondary multiplexers will be connected on channels 0, 1, and 2 on the primary multiplexer. My 1st secondary multiplexer will have 8 IMUs connected. My 2nd secondary multiplexer will have 8 IMUs connected. My 3rd secondary multiplexer will have 2 IMUs connected. I am having trouble with an Arduino code to talk to each IMU individually. I can hear from IMUs 1-8 but not 9-18. Here is the intro and setup loop of my code. My question is, how can I request say, channel 1 on my primary multiplexer then channel 3 on my secondary multiplexer to read data from IMU 12? Thank you! 

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

#define PRIMARY_TCA_ADDR 0x70 // Address of first multiplexer
#define SECONDARY_TCA_ADDR1 0x71 // Address for second-level multiplexer 1
#define SECONDARY_TCA_ADDR2 0x72 // Address for second-level multiplexer 2
#define SECONDARY_TCA_ADDR3 0x73 // Address for second-level multiplexer 3

Adafruit_BNO055 bno = Adafruit_BNO055(55, 0x28);

// Function to select a channel on a multiplexer
void selectChannel(uint8_t tca_addr, uint8_t channel) {
     Wire.beginTransmission(tca_addr);
     Wire.write(1 << channel); // Select channel by bit-shifting
     Wire.endTransmission();
}

void setup() {
     Serial.begin(115200);
     Wire.begin();

     // Initialize all IMUs
     // loop through channels 0, 1, 2 on primary MUX
     for (int primary = 0; primary < 3; primary++) {
          selectChannel(PRIMARY_TCA_ADDR, primary);
          delay(10);

          uint8_t SECONDARY_TCA_ADDR_temp;
          if (primary == 0) {SECONDARY_TCA_ADDR_temp = SECONDARY_TCA_ADDR1;}
          else if (primary == 1) {SECONDARY_TCA_ADDR_temp = SECONDARY_TCA_ADDR2;}
          else if (primary == 2) {SECONDARY_TCA_ADDR_temp = SECONDARY_TCA_ADDR3;}

          // loop through all channels on secondary MUX
          for (int secondary = 0; secondary < 8; secondary++) {
               // skip channels not being used on last MUX
               if ((primary == 2) && (secondary > 1)) {
                    break;
               }

               selectChannel(SECONDARY_TCA_ADDR_temp, secondary);
               delay(20); // Allow time for I2C device to wake up

                if (!bno.begin()) {
                    Serial.print("IMU "); Serial.print(primary * 8 + secondary + 1);
                    Serial.println(" not detected.");
                    delay(10);
               }
               else {
                    bno.setExtCrystalUse(true); // Use external crystal for better accuracy
                    Serial.print("IMU "); Serial.print(primary * 8 + secondary + 1);
                    Serial.println(" DETECTED.");
               }
          }
     }
}