humanpowered
Member
Hi All,
I'm wiring three MPU6050s to a Teensy 4.1. Because they all have the same address 0x68, I'm using the three i2c buses on the T4.1. I am able to set up Wire, Wire1 and Wire2 and scan for the sensors on an i2c scanner, however, when I try to read data from the sensors I only get data from the first one. I suspect that I'm not correctly assigning the sensors to the wire or calling the wire even though I've tried everything that I can think of. I've tried this with a multiplexer as well, but have not been able to get it to work with a similar problem. I'd really appreciate any help!
I've included the sketch below along with some comments and arrows pointing to likely trouble spots.
In case it's helpful here's the i2c scanner sketch that is able to correctly identify the sensors:
I've also included a fritzing of my wiring
Thank You!!
I'm wiring three MPU6050s to a Teensy 4.1. Because they all have the same address 0x68, I'm using the three i2c buses on the T4.1. I am able to set up Wire, Wire1 and Wire2 and scan for the sensors on an i2c scanner, however, when I try to read data from the sensors I only get data from the first one. I suspect that I'm not correctly assigning the sensors to the wire or calling the wire even though I've tried everything that I can think of. I've tried this with a multiplexer as well, but have not been able to get it to work with a similar problem. I'd really appreciate any help!
I've included the sketch below along with some comments and arrows pointing to likely trouble spots.
Code:
#include "I2Cdev.h"
#include "MPU6050.h"
#include "Wire.h"
MPU6050 accelgyro1; // <-------------- how do I associate these with a specific instance of Wire or set a unique sensor ID?
MPU6050 accelgyro2;
MPU6050 accelgyro3;
int16_t ax, ay, az;
int16_t gx, gy, gz;
void setup() {
Wire.setSCL(19); // SCL on first i2c bus on T4.1
Wire.setSDA(18); // SDA on first i2c bus on T4.1
Wire1.setSCL(16); // SCL1 on second i2c bus on T4.1
Wire1.setSDA(17); // SDA1 on second i2c bus on T4.1
Wire2.setSCL(24); // SCL2 on second i2c bus on T4.1
Wire2.setSDA(25); // SDA2 on second i2c bus on T4.1
Wire.begin();
Wire1.begin();
Wire2.begin();
Serial.begin(115200);
Serial.println("Initializing I2C device 1...");
accelgyro1.initialize();
Serial.println("Testing device 1...");
Serial.println(accelgyro1.testConnection() ? "MPU6050 1 connection successful" : "MPU6050 1 connection failed"); // <---- this works, if I unplug a sensor the correct one fails
delay(3000);
Serial.println("Initializing I2C device...");
accelgyro2.initialize();
Serial.println("Testing device 2..."); // <------------------------ for some reason it skips this and the next line
Serial.println(accelgyro2.testConnection() ? "MPU6050 2 connection successful" : "MPU6050 2 connection failed");
delay(3000);
Serial.println("Initializing I2C device 3...");
accelgyro3.initialize();
Serial.println("Testing device 3...");
Serial.println(accelgyro3.testConnection() ? "MPU6050 3 connection successful" : "MPU6050 3 connection failed");
delay(3000);
Serial.println("............................................");
}
void loop() {
// i2c on first bus starts here
accelgyro1.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
Serial.print("MPU_1 a/g:\t");
Serial.print(ax); Serial.print("\t");
Serial.print(ay); Serial.print("\t");
Serial.print(az); Serial.print("\t");
Serial.print(gx); Serial.print("\t");
Serial.print(gy); Serial.print("\t");
Serial.println(gz);
// i2c on second bus starts here
accelgyro2.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
Serial.print("MPU_2 a/g:\t");
Serial.print(ax); Serial.print("\t");
Serial.print(ay); Serial.print("\t");
Serial.print(az); Serial.print("\t");
Serial.print(gx); Serial.print("\t");
Serial.print(gy); Serial.print("\t");
Serial.println(gz);
// i2c on third bus starts here
accelgyro3.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
Serial.print("MPU_3 a/g:\t");
Serial.print(ax); Serial.print("\t");
Serial.print(ay); Serial.print("\t");
Serial.print(az); Serial.print("\t");
Serial.print(gx); Serial.print("\t");
Serial.print(gy); Serial.print("\t");
Serial.println(gz);
}
delay(1000);
}
In case it's helpful here's the i2c scanner sketch that is able to correctly identify the sensors:
Code:
#include <Wire.h>
void setup()
{
Wire.setSCL(19); // SCL on first i2c bus on T4.1
Wire.setSDA(18); // SDA on first i2c bus on T4.1
Wire1.setSCL(16); // SCL1 on second i2c bus on T4.1
Wire1.setSDA(17); // SDA1 on second i2c bus on T4.1
Wire2.setSCL(24); // SCL2 on second i2c bus on T4.1
Wire2.setSDA(25); // SDA2 on second i2c bus on T4.1
Wire.begin();
Wire1.begin();
Wire2.begin();
Serial.begin(9600);
while (!Serial);
Serial.println("\nI2C Scanner");
}
void loop()
{
byte error, address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for (address = 1; address < 127; address++ )
{
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
//---------------------------------------------------------------------------------
Wire.beginTransmission(address); // i2c on first bus starts here
error = Wire.endTransmission();
if (error == 0)
{
Serial.print("I2C device found at Wire 0x");
if (address < 16)
Serial.print("0");
Serial.print(address, HEX);
Serial.println(" !");
nDevices++;
}
else if (error == 4)
{
Serial.print("Unknown error at Wire 0x");
if (address < 16)
Serial.print("0");
Serial.println(address, HEX);
} // i2c on first bus ends here
//---------------------------------------------------------------------------------
Wire1.beginTransmission(address); // i2c on second bus starts here
error = Wire1.endTransmission();
if (error == 0)
{
Serial.print("I2C device found at Wire1 0x");
if (address < 16)
Serial.print("0");
Serial.print(address, HEX);
Serial.println(" !");
nDevices++;
}
else if (error == 4)
{
Serial.print("Unknown error at Wire1 0x");
if (address < 16)
Serial.print("0");
Serial.println(address, HEX);
}
// i2c on second bus ends here
//---------------------------------------------------------------------------------
Wire2.beginTransmission(address); // i2c on third bus starts here
error = Wire2.endTransmission();
if (error == 0)
{
Serial.print("I2C device found at Wire2 0x");
if (address < 16)
Serial.print("0");
Serial.print(address, HEX);
Serial.println(" !");
nDevices++;
}
else if (error == 4)
{
Serial.print("Unknown error at Wire2 0x");
if (address < 16)
Serial.print("0");
Serial.println(address, HEX);
}
} // i2c on third bus ends here
//---------------------------------------------------------------------------------
if (nDevices == 0)
Serial.println("No I2C devices found\n");
else
Serial.print("number of devices found = ");
Serial.println(nDevices); // this prints the total number of i2c devices found
Serial.println("done\n");
delay(2000); // wait 2 seconds for next scan
}
I've also included a fritzing of my wiring
Thank You!!