Arduino nano every using Qwiic MUX + Multiple Qwiic IMU sensors

Hi everyone!

Im using the arduno nano Every with a qwiic shield connected to SparkFun Qwiic Mux Breakout with two SparkFun 9DoF IMUs - ICM-20948:

And my problem is that using the following code, I can’t seem to address the second IMU and I get none readings (all zeros).

My code:

#include "ICM_20948.h" // Click here to get the library: http://librarymanager/All#SparkFun_ICM_20948_IMU
#include <Wire.h>
#define Serial Serial

#include <SparkFun_I2C_Mux_Arduino_Library.h> //Click here to get the library: http://librarymanager/All#SparkFun_I2C_Mux
QWIICMUX myMux;

#define AD0_VAL 1      // The value of the last bit of the I2C address.                \
                       // On the SparkFun 9DoF IMU breakout the default is 1, and when \
                       // the ADR jumper is closed the value becomes 0

ICM_20948_I2C myICM; // Create an ICM_20948_I2C object


void setup()
{

  Serial.begin(115200);
  while (!Serial)
  {
  };

  Wire.begin();
  Wire.setClock(400000);

  if (myMux.begin() == false)
  {
    Serial.println("Mux not detected. Freezing...");
    while (1);
  }
  Serial.println("Mux detected");

  myMux.setPort(0);
  
  bool initialized = false;
  while (!initialized)
  {
    myICM.begin(Wire, AD0_VAL);

    Serial.print(F("Initialization of the sensor returned: "));
    Serial.println(myICM.statusString());
    if (myICM.status != ICM_20948_Stat_Ok)
    {
      Serial.println("Trying again...");
      delay(500);
    }
    else
    {
      initialized = true;
    }
  }
}

void loop()
{

  if (myICM.dataReady())
  {
    myMux.setPort(0);
    myICM.getAGMT();         // The values are only updated when you call 'getAGMT'                 
    Serial.println("IMU 0");
    printScaledAGMT(&myICM); // This function takes into account the scale settings from when the measurement was made to calculate the values with units
    delay(50);

    myMux.setPort(1);
    myICM.getAGMT();         // The values are only updated when you call 'getAGMT'                 
    Serial.println("IMU 1");
    printScaledAGMT(&myICM); // This function takes into account the scale settings from when the measurement was made to calculate the values with units
    delay(50);
    myMux.setPort(0);
   }
  else
  {
    Serial.println("Waiting for data");
    delay(500);
  }
}

I noticed that when using just 1 IMU by commenting the lines:

myMux.setPort(1);
myICM.getAGMT();         // The values are only updated when you call 'getAGMT'                 
Serial.println("IMU 1");
printScaledAGMT(&myICM); // This function takes into account the scale settings from when the measurement was made to calculate the values with units
delay(50);
myMux.setPort(0);

I get good constant readings for that 1 IMU.

What am I doing wrong?

Thanks!

You need a separate instance if ICM_20948_I2C for each port on the mux. Look at the last post of viewtopic.php for some more info.

/mike

Hi! Sorry for the late reply, the link you provided is unavailable now.

Do you mean declaring ICM_20948_I2C myICM in the void loop as follows?

void loop()
{

  if (myICM.dataReady())
  {
    ICM_20948_I2C myICM; // Create an ICM_20948_I2C object
    myMux.setPort(0);
    myICM.getAGMT();         // The values are only updated when you call 'getAGMT'                 
    Serial.println("IMU 0");
    printScaledAGMT(&myICM); // This function takes into account the scale settings from when the measurement was made to calculate the values with units
    delay(50);

    ICM_20948_I2C myICM; // Create an ICM_20948_I2C object
    myMux.setPort(1);
    myICM.getAGMT();         // The values are only updated when you call 'getAGMT'                 
    Serial.println("IMU 1");
    printScaledAGMT(&myICM); // This function takes into account the scale settings from when the measurement was made to calculate the values with units
    delay(50);
    myMux.setPort(0);
   }
  else
  {
    Serial.println("Waiting for data");
    delay(500);
  }
}

n1ist:
You need a separate instance if ICM_20948_I2C for each port on the mux. Look at the last post of https://forum.sparkfun.com/viewtopic.php for some more info.

/mike

Sorry, I somehow lost part of that link. Try [https://forum.sparkfun.com/viewtopic.ph ... 7&p=223469](https://forum.sparkfun.com/viewtopic.php?f=79&t=52587&p=223469)

/mike