Connect read from each i2c bus T4.1

Status
Not open for further replies.
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.

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
3_MPU6050_T41_3_i2c_busses_bb_s.png

Thank You!!
 
Don't think the MPU6050 library is setup to handle anything but wire.
Rather than rewrite the library Iv'e delt with this by putting: #define Wire Wire1 into the offending.cpp library and put that library into the projects folder so it gets used. This worked great for 3 different libraries. In your case their all
the same library so it wouldn't work. You'd have to rename them. Not really the way to do it.

Adafruit has a MPU6050 library with a proper begin:

bool Adafruit_MPU6050::begin(uint8_t i2c_address, TwoWire *wire,
int32_t sensor_id) {
 
Last edited:
Adafruit has a MPU6050 library with a proper begin:

bool Adafruit_MPU6050::begin(uint8_t i2c_address, TwoWire *wire,
int32_t sensor_id) {

Thanks bicycleguy! BTW I'm a bicycleguy as well!
I have the adafruit library, they are actually Adafruit MPU6050s, can you give me an example of how I assign the sensors in the sketch?
I'm guessing this takes place at the mpu1.begin. Looking at the function I see:

uint8_t i2c_address = 0x68?
TwoWire *wire = Wire, Wire1, etc.?
int32_t sensor_id = ??? is this something I need to define earlier? If so I don't know how to assign a unique sensor id.

Thanks again for the help!
 
Might be better to change the addresses I don't know.
Otherwise:
In your sketch above change something like:
change MPU6050 to Adafruit_MPU6050 everywhere
accelgyro1.initialize(); to accelgyro1.begin();
accelgyro2.initialize(); to accelgyro2.begin(&Wire1);
accelgyro3.initialize(); to accelgyro2.begin(&Wire2);
 
Status
Not open for further replies.
Back
Top