reading 2 mpu-6050 from i2c bus .......

hi there,

I’m trying to connect 2 mpu-6050 sensors to arduino uno. I have managed to expand the code for first sensor (however im not sure if it works or not)

the problem : at the end of the library, there is a section as;

int MPU6050_read(int start, uint8_t *buffer, int size)

and in this section first mpu-6050 is addressed by

Wire.beginTransmission(MPU6050_I2C_ADDRESS);

i dont know how can i add the second mpu-6050 sensor’s address to this section…

any idea???

complete code for this section is as ;

[color=orange]int MPU6050_read(int start, uint8_t *buffer, int size)[/color]
{
  int i, n, error;

  Wire.beginTransmission([color=red]MPU6050_I2C_ADDRESS[/color]);
  n = Wire.write(start);
  if (n != 1)
    return (-10);

  n = Wire.endTransmission(false);    // hold the I2C-bus
  if (n != 0)
    return (n);

  // Third parameter is true: relase I2C-bus after data is read.
  Wire.requestFrom(MPU6050_I2C_ADDRESS, size, true);
  i = 0;
  while(Wire.available() && i<size)
  {
    buffer[i++]=Wire.read();
  }
  if ( i != size)
    return (-11);

  return (0);  // return : no error
}

Add a parameter for the I2C address.

int MPU6050_read(int i2c_address, int start, uint8_t *buffer, int size)
{
  int i, n, error;

  Wire.beginTransmission(i2c_address);
  n = Wire.write(start);
  if (n != 1)
    return (-10);

  n = Wire.endTransmission(false);    // hold the I2C-bus
  if (n != 0)
    return (n);

  // Third parameter is true: relase I2C-bus after data is read.
  Wire.requestFrom(i2c_address, size, true);
  i = 0;
  while(Wire.available() && i<size)
  {
    buffer[i++]=Wire.read();
  }
  if ( i != size)
    return (-11);

  return (0);  // return : no error
}

Call it like this:

#define MPU6050_I2C_ADDRESS_1 0x68
#define MPU6050_I2C_ADDRESS_2 0x69

MPU6050_read(MPU6050_I2C_ADDRESS_1, buffer1, size);
MPU6050_read(MPU6050_I2C_ADDRESS_2, buffer2, size);

Or something else like that.
The best solution would be if each MPU-6050 would have its own class, with the i2c-address in the class.

thank you for your reply Peter_n.

this is what i have done based on your recommendation, is that correct kinda???

#define MPU6050_I2C_ADDRESS_1 0x68
#define MPU6050_I2C_ADDRESS_2 0x69


int MPU6050_read(int MPU6050_I2C_ADDRESS_1, int start, uint8_t *buffer, int size)
{
  int i, n, error;

  Wire.beginTransmission(MPU6050_I2C_ADDRESS_1);
  n = Wire.write(start);
  if (n != 1)
    return (-10);

  n = Wire.endTransmission(false);    // hold the I2C-bus
  if (n != 0)
    return (n);

  // Third parameter is true: relase I2C-bus after data is read.
  Wire.requestFrom(MPU6050_I2C_ADDRESS_1, size, true);
  i = 0;
  while(Wire.available() && i<size)
  {
    buffer[i++]=Wire.read();
  }
  if ( i != size)
    return (-11);

  return (0);  // return : no error
}

int MPU6050_read(int MPU6050_I2C_ADDRESS_2, int start, uint8_t *buffer, int size)
{
  int i, n, error;

  Wire.beginTransmission(MPU6050_I2C_ADDRESS_2);
  n = Wire.write(start);
  if (n != 1)
    return (-10);

  n = Wire.endTransmission(false);    // hold the I2C-bus
  if (n != 0)
    return (n);

  // Third parameter is true: relase I2C-bus after data is read.
  Wire.requestFrom(MPU6050_I2C_ADDRESS_2, size, true);
  i = 0;
  while(Wire.available() && i<size)
  {
    buffer[i++]=Wire.read();
  }
  if ( i != size)
    return (-11);

  return (0);  // return : no error
}

with this way, can I read the datat from both sensors at the same time???
thanks in advance

Read my Reply #1 once more please. You have two functions with a fixed i2c address, I have a single function that uses a parameter for the i2c address. That function can be called with any i2c address.