Could someone review my code please?

Hello. I am currently working on a project that uses 2 Adafruit LSM6DSOX IMU's, connected to Arduino Uno R3 through a Adafruit TCA9548A I2C Multiplexer. I am new to I2C coding, and I need help fixing my code. I believe the problem is is that both IMUs aren't being detected, as when I run the program I only get the same data again and again.

Crude layout of my setup (there is a breadboard involved and 5V power source for all of the IMU's, as well as the A0, A1, and A2 pins):

Code:

#include <Arduino_LSM6DSOX.h>
#include <TCA9548A.h>
#include <Wire.h>

//Testing by sending one byte of data to the address and back
void TCA9548A(uint8_t i){
  Wire.beginTransmission(0x70);  // TCA9548A address is 0x70
  Wire.write(1 << i);          // send byte to select bus
  Wire.endTransmission();
  Serial.print(i);
}

//Beginning wire+ all the setup code
void setup() {
  while (!Serial);
  delay(10);
  Wire.begin();
  Serial.begin(9600);

  //Checks to see if it fails and printing out different sample rate values//
  if (!IMU.begin()) {
    Serial.println("Failed to initialize IMU!");
    
    //Defines terms at beginning of code for each individual IMU
    Serial.print("Accelerometer1 sample rate = ");
    Serial.print(IMU.accelerationSampleRate());
    Serial.println(" Hz");
    Serial.println();
    Serial.println("Acceleration in g's");
    Serial.println("X\tY\tZ");
    Serial.print("Gyroscope1 sample rate = ");
    Serial.print(IMU.gyroscopeSampleRate());
    Serial.println(" Hz");
    Serial.println();
    Serial.println("Gyroscope1 in degrees/second");
    Serial.println("U\tV\tW");
    
    Serial.print("Accelerometer2 sample rate = ");
    Serial.print(IMU.accelerationSampleRate());
    Serial.println(" Hz");
    Serial.println();
    Serial.println("Acceleration in g's");
    Serial.println("A\tB\tC");
    Serial.print("Gyroscope2 sample rate = ");
    Serial.print(IMU.gyroscopeSampleRate());
    Serial.println(" Hz");
    Serial.println();
    Serial.println("Gyroscope2 in degrees/second");
    Serial.println("D\tE\tF");
  }
}

void loop() 
{
  TCA9548A(0);
  float x, y, z, u, v, w; //Variables being used//

  if (IMU.accelerationAvailable()) {
    IMU.readAcceleration(x, y, z);

    Serial.print(x);
    Serial.print('\t');
    Serial.print(y);
    Serial.print('\t');
    Serial.println(z);
  }
  if (IMU.gyroscopeAvailable()) {
    IMU.readGyroscope(u, v, w);

    Serial.print(u);
    Serial.print('\t');
    Serial.print(v);
    Serial.print('\t');
    Serial.println(w);
    Serial.println("imu1");
  }


  delay(0);
  
   TCA9548A(1);
   float  a, b, c, d, e, f;
   
   if (IMU.accelerationAvailable()) {
    IMU.readAcceleration(a, b, c);
    Serial.print(a);
    Serial.print('\t');
    Serial.print(b);
    Serial.print('\t');
    Serial.println(c);
  }
  if (IMU.gyroscopeAvailable()) {
    IMU.readGyroscope(d, e, f);

    Serial.print(d);
    Serial.print('\t');
    Serial.print(e);
    Serial.print('\t');
    Serial.println(f);
    Serial.println("imu2");
  }
  delay(5000);
}

image

Don't you select which I2C port by putting the port selection number on the A0 to A2 pins? There they are all tied high so only that one port is selected.

Why is Vin connected to the TCA9548 Vcc. Vin is not a 5V output.

If you have two accelerometers, IMU.begin() should be run twice. Before calling Imu.begin(), you should select the one of the buses by TSA9548

Let look to the setup() :

First, you print some data and said that it is from the first IMU, and then in the same way - from the second. Why did you decide that it will be data from different IMU? You did not switch TSA9548 bus between these lines - which means that this is data from the same sensor, if any...

You might what to reconsider connecting Vdd to GND !

And for connecting GND TO 5V :woozy_face:
:open_mouth:

Thank you to all that replied! All of your suggestions were helpful, but I found the solution to my problem. I needed to initialize the IMUs separately (so TCA9548A(0) then IMU.begin, TCA9548A(1) then IMU.begin). Such a simple mistake that I overlooked.

Sorry, that was a rushed diagram. Everything was correct in real life though :+1:

Please understand when we are given inaccurate information, we end up in a rabbit hole :woozy_face:

if you want a review, shouldn't you describe what you expect the code to do?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.