Hello guys, I'm trying to connect two mpu-9250 on an Arduino UNO R3. I have also Sparkfun's 74HC4051 multiplexer, because I thought I needed it.
So I connected them like in this image
and used this code:
#include <MPU9250_RegisterMap.h>
#include <SparkFunMPU9250-DMP.h>
//#define SerialPort SerialUSB
MPU9250_DMP imu0;
MPU9250_DMP imu1;
const int selectPins[3] = {2, 3, 4}; // S0~2, S1~3, S2~4
const int zOutput = 5;
void setup() {
Serial.begin(9600);
for (int i=0; i<3; i++)
{
pinMode(selectPins[i], OUTPUT);
digitalWrite(selectPins[i], HIGH);
}
// pinMode(zInput, INPUT);
selectMuxPin(0);
if (imu0.begin() != INV_SUCCESS)
{
while (1)
{
Serial.println("Unable to communicate with 1st MPU-9250");
Serial.println("Check connections, and try again.");
Serial.println();
//delay(5000);
}
}
else
{
imu0.setSensors(INV_XYZ_GYRO | INV_XYZ_ACCEL | INV_XYZ_COMPASS);
imu0.setGyroFSR(2000);
imu0.setAccelFSR(2);
imu0.setLPF(5);
imu0.setSampleRate(10);
imu0.setCompassSampleRate(10);
Serial.println("IMU0 set=OK");
}
selectMuxPin(1);
if (imu1.begin() != INV_SUCCESS)
{
while (1)
{
Serial.println("Unable to communicate with 2nd MPU-9250");
Serial.println("Check connections, and try again.");
Serial.println();
//delay(5000);
}
}
else
{
imu1.setSensors(INV_XYZ_GYRO | INV_XYZ_ACCEL | INV_XYZ_COMPASS);
imu1.setGyroFSR(2000);
imu1.setAccelFSR(2);
imu1.setLPF(5);
imu1.setSampleRate(10);
imu1.setCompassSampleRate(10);
Serial.println("IMU1 set=OK");
}
}
void loop() {
selectMuxPin(0);
if ( imu0.dataReady() )
{
imu0.update(UPDATE_ACCEL | UPDATE_GYRO | UPDATE_COMPASS);
printIMU0Data();
}
selectMuxPin(1);
if ( imu1.dataReady() )
{
imu1.update(UPDATE_ACCEL | UPDATE_GYRO | UPDATE_COMPASS);
printIMU1Data();
}
//delay(1000);
}
void selectMuxPin(byte pin)
{
for (int i=0; i<3; i++)
{
if (pin & (1<<i))
digitalWrite(selectPins[i], HIGH);
else
digitalWrite(selectPins[i], LOW);
}
}
void printIMU0Data(void)
{
// After calling update() the ax, ay, az, gx, gy, gz, mx,
// my, mz, time, and/or temerature class variables are all
// updated. Access them by placing the object. in front:
//if (i==0) Serial.println("1st MPU");
//if (i==1) Serial.println("2nd MPU");
// Use the calcAccel, calcGyro, and calcMag functions to
// convert the raw sensor readings (signed 16-bit values)
// to their respective units.
float accelX = imu0.calcAccel(imu0.ax);
float accelY = imu0.calcAccel(imu0.ay);
float accelZ = imu0.calcAccel(imu0.az);
float gyroX = imu0.calcGyro(imu0.gx);
float gyroY = imu0.calcGyro(imu0.gy);
float gyroZ = imu0.calcGyro(imu0.gz);
float magX = imu0.calcMag(imu0.mx);
float magY = imu0.calcMag(imu0.my);
float magZ = imu0.calcMag(imu0.mz);
Serial.println("1st MPU");
Serial.println("Accel: " + String(accelX) + ", " +
String(accelY) + ", " + String(accelZ) + " g");
Serial.println("Gyro: " + String(gyroX) + ", " +
String(gyroY) + ", " + String(gyroZ) + " dps");
Serial.println("Mag: " + String(magX) + ", " +
String(magY) + ", " + String(magZ) + " uT");
//Serial.println("Time: " + String(imu.time) + " ms");
Serial.println();
}
void printIMU1Data(void)
{
// After calling update() the ax, ay, az, gx, gy, gz, mx,
// my, mz, time, and/or temerature class variables are all
// updated. Access them by placing the object. in front:
//if (i==0) Serial.println("1st MPU");
//if (i==1) Serial.println("2nd MPU");
// Use the calcAccel, calcGyro, and calcMag functions to
// convert the raw sensor readings (signed 16-bit values)
// to their respective units.
float accelX = imu1.calcAccel(imu1.ax);
float accelY = imu1.calcAccel(imu1.ay);
float accelZ = imu1.calcAccel(imu1.az);
float gyroX = imu1.calcGyro(imu1.gx);
float gyroY = imu1.calcGyro(imu1.gy);
float gyroZ = imu1.calcGyro(imu1.gz);
float magX = imu1.calcMag(imu1.mx);
float magY = imu1.calcMag(imu1.my);
float magZ = imu1.calcMag(imu1.mz);
Serial.println("2nd MPU");
Serial.println("Accel: " + String(accelX) + ", " +
String(accelY) + ", " + String(accelZ) + " g");
Serial.println("Gyro: " + String(gyroX) + ", " +
String(gyroY) + ", " + String(gyroZ) + " dps");
Serial.println("Mag: " + String(magX) + ", " +
String(magY) + ", " + String(magZ) + " uT");
//Serial.println("Time: " + String(imu.time) + " ms");
Serial.println();
}
I get no errors, but I get no serious data as well. (0 for acc+mag, -0.06 for gyro (whatever that means))
After researching a while I found out that I could use SPI for this, without the multiplexer also. However, my problem is, if I connect the one on pin 10 (SS), then where will I connect the other?
Thanks