Search code examples
c++arduinoi2c

What's the difference between two methods of accessing thorugh I2C bus?


I saw two different methods of requesting the data from I2C bus:

Method 1:

  Wire.beginTransmission(MPU);
  Wire.write(0x43); // Gyro data first register address 0x43
  Wire.endTransmission(false);
  //https://www.arduino.cc/en/Reference/WireEndTransmission
  //false will send a restart, keeping the connection active. 

  Wire.requestFrom(MPU, 6, true); // Read 4 registers total, each axis value is stored in 2 registers
  GyroX = (Wire.read() << 8 | Wire.read()) / 131.0; // For a 250deg/s range we have to divide first the raw value by 131.0, according to the datasheet
  GyroY = (Wire.read() << 8 | Wire.read()) / 131.0;
  GyroZ = (Wire.read() << 8 | Wire.read()) / 131.0;

Method 2:

Wire.beginTransmission(0b1101000); //I2C address of the MPU //Accelerometer and Temperature reading (check 3.register map) 
Wire.write(0x3B); //Starting register for Accel Readings
Wire.endTransmission();
Wire.requestFrom(0b1101000,8); //Request Accel Registers (3B - 42)
while(Wire.available() < 8);
accelX = Wire.read()<<8|Wire.read(); //Store first two bytes into accelX
accelY = Wire.read()<<8|Wire.read(); //Store middle two bytes into accelY
accelZ = Wire.read()<<8|Wire.read(); //Store last two bytes into accelZ
temp = Wire.read()<<8| Wire.read();

It appeared that the first method did not terminate the transmission, i.e. Wire.endTransmission(false) while the second one was not specified. What's the difference between them? and which one is better, i.e. response time/loop time.

Also, does

0b1101000 and 0x3B(the register address for Accel in Method 2)

equal to each other?


Solution

  • One is requesting gyro reading, other accelerometer readings, they are different things. Same for your other question, 0b1101000 is MPU's address, while 0x3B is accelerometer register address (so not same), you should probably read about the library you are using for this, or in case you are not using any library, you can find a few in Arduino library option(whatever it's called, haven't used it in a while).

    If you don't wanna use a library, read the datasheet of your MPU for better understanding.

    Depending on whether it's a MPU6-series, MPU9-series, or maybe it's by another manufacturer, things will change, but most have already built libraries, I would advise you use them, unless you are interested in doing the work from ground up, which can get fairly complicated really fast.