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);
}