Two MPU9250 on Arduino Uno influences each others measurements

Hello,

i have Arduino Uno and connected two MPU9250 units over SPI. For reading and general setup i use this library: GitHub - bolderflight/mpu9250: MPU-9250 sensor driver.

The problem is that mpus influences each other in measurements - if i rotate one unit 1 then the angles are changing as they should but in the same time im getting the same values from unit 2 and vice versa.

Using 10 and 9 pins for SS

How can i make these two units measure independently?

Wiring:


Code:

#include "MPU9250.h"
#include "SensorFusion.h"


MPU9250 imuu2(SPI, 10);
MPU9250 imuu(SPI, 9);

int status;
bool calibrateMag;
SF fusion;

void initSensor(MPU9250 &imu, int i) {
  status = imu.begin();
  if (status < 0) {
    Serial.print("IMU initialization unsuccessful, num ");
    Serial.print(i, DEC);
    Serial.println();
    Serial.print("Status: ");
    Serial.println(status);
    while (1) {}
  }

  float hxb;
  float hxs;
  float hyb;
  float hys;
  float hzb;
  float hzs;

  if (calibrateMag) {

    Serial.println("Starting calibrating mag!");
    status = imu.calibrateMag();

    if (status < 0) {
      Serial.println("Failed to calibrate mag num " + i);
    } else {
      Serial.println("Mag calibration success, num " + i);

      hxb = imu.getMagBiasX_uT();
      Serial.print("hxb: ");
      Serial.print(hxb);

      hxs = imu.getMagScaleFactorX();
      Serial.print("hxs: ");
      Serial.print(hxs);

      hyb = imu.getMagBiasY_uT();
      Serial.print("hyb: ");
      Serial.print(hyb);

      hys = imu.getMagScaleFactorY();
      Serial.print("hys: ");
      Serial.print(hys);


      hzb = imu.getMagBiasZ_uT();
      Serial.print("hzb: ");
      Serial.print(hzb);

      hzs = imu.getMagScaleFactorZ();
      Serial.print("hzs: ");
      Serial.print(hzs);

      Serial.println();

      imu.setMagCalX(hxb, hxs);
      imu.setMagCalY(hyb, hys);
      imu.setMagCalZ(hzb, hzs);
    }
  }
}

void setup() {
  // serial to display data
  Serial.begin(115200);
  while (!Serial) {}

  calibrateMag = false;

  initSensor(imuu, 0);
  initSensor(imuu2, 1);
}

void readSensor(MPU9250 &imu, int i) {
  float gx, gy, gz, ax, ay, az, mx, my, mz, temp;
  float pitch, roll, yaw;
  float deltat;

  imu.readSensor();

  ax = imu.getAccelX_mss();
  ay = imu.getAccelY_mss();
  az = imu.getAccelZ_mss();
  gx = imu.getGyroX_rads();
  gy = imu.getGyroY_rads();
  gz = imu.getGyroZ_rads();
  mx = imu.getMagX_uT();
  my = imu.getMagY_uT();
  mz = imu.getMagZ_uT();

  deltat = fusion.deltatUpdate();
  fusion.MadgwickUpdate(gx, gy, gz, ax, ay, az, mx, my, mz, deltat);

  roll = fusion.getRoll();
  pitch = fusion.getPitch();
  yaw = fusion.getYaw();

  // display the data
  Serial.print(">"); //message starts
  Serial.print(roll, 6);
  Serial.print("\t");
  Serial.print(pitch, 6);
  Serial.print("\t");
  Serial.print(yaw, 6);
  Serial.print("\t");
  Serial.print(i);
  Serial.print("<"); //message ends
  Serial.print("\r");
  Serial.println();
}

void loop() {
  readSensor(imuu, 0);
  delay(100);
  readSensor(imuu2, 1);
  delay(100);
}

Example output: each unit is facing diferent angle but they are returning same measurements… why?
Hope this better explain my problem.

roll pitch yaw MPU number

47.461639 77.757209 201.196044 0<
45.776214 77.349822 199.501327 1<
49.627929 78.219291 203.360366 0<
47.830818 77.824081 201.553924 1<
51.977386 78.666725 205.712493 0<
50.050743 78.286392 203.773483 1<
54.526542 79.089790 208.254882 0<
52.442893 78.742454 206.163772 1<
57.293212 79.500862 211.023101 0<
55.129859 79.179344 208.856918 1<
60.388511 79.892318 214.124450 0<
58.043712 79.594917 211.766143 1<
63.692874 80.253921 217.432495 0<
61.292774 79.992912 215.008209 1<
67.178504 80.646697 220.912292 0<
45.962116 72.089439 201.464065 1<
48.284492 72.210899 204.432205 0<
47.137386 71.819320 203.255386 1<
49.920509 72.669441 205.975021 0<
48.605247 72.269645 204.663726 1<
51.476627 73.115081 207.492996 0<
50.063709 72.716430 206.091522 1<
53.154975 73.526260 209.121124 0<
51.667743 73.147338 207.641571 1<
54.828243 73.915847 210.760681 0<
53.327766 73.558609 209.272308 1<

Thank you

That code won't even compile.

Post the actual code, using code tags as described in "How to use this forum".

Hello,

updated my code and also added wiring setup.
Added sample output measurements of both units in action.

It looks to me like that library was not intended to handle two devices connected in parallel.

On a separate issue, your code can never work properly. You can't do printing and have delays between sensor reads, because that makes the gyro contribution worthless. The data MUST be processed as quickly and continuously as possible.

  readSensor(imuu, 0);
  delay(100);
  readSensor(imuu2, 1);
  delay(100);
  fusion.MadgwickUpdate(gx, gy, gz, ax, ay, az, mx, my, mz, deltat);

You appear to be using the SAME 'Sensor Fusion' object ('fusion') for BOTH sensors. Since the Sensor Fusion is supposed to provide a kind of running average, it will be averaging the two sensors and that would cause the results to be very similar for both. Try creating a separate Sensor Fusion object for each sensor object.

johnwasser:

  fusion.MadgwickUpdate(gx, gy, gz, ax, ay, az, mx, my, mz, deltat);

You appear to be using the SAME 'Sensor Fusion' object ('fusion') for BOTH sensors. Since the Sensor Fusion is supposed to provide a kind of running average, it will be averaging the two sensors and that would cause the results to be very similar for both. Try creating a separate Sensor Fusion object for each sensor object.

I can't believe I didn't notice. After this fix it works as expected. Thank you so much for your help.

jremington:
It looks to me like that library was not intended to handle two devices connected in parallel.

On a separate issue, your code can never work properly. You can't do printing and have delays between sensor reads, because that makes the gyro contribution worthless. The data MUST be processed as quickly and continuously as possible.

  readSensor(imuu, 0);

delay(100);
  readSensor(imuu2, 1);
  delay(100);

OK