0

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?

  • 1
    "Also, does 0b1101000 and 0x3B equal to each other?" - converting between binary and hexadecimal is *trivial*. Why don't you do the conversion yourself? – Jesper Juhl Jan 26 '20 at 18:59
  • The second one would fail code review because of reliance on [Magic Numbers](https://stackoverflow.com/questions/47882/what-is-a-magic-number-and-why-is-it-bad). – user4581301 Jan 26 '20 at 19:00
  • Do you understand what the code is doing, how it's doing it, and why? How familiar with SPI are you? How far down does a potential answerer need to dig to explain the differences? – user4581301 Jan 26 '20 at 19:05
  • @JesperJuhl Thanks! I got 0b1101000 as the MPU address. – ShoutOutAndCalculate Jan 26 '20 at 19:06
  • @user4581301 I'm new to SPI, but I read through those code, the main confusing for me is why in method 2 it's not Wire.endTransmission(false), and why does it terminate/not terminate the transmission. – ShoutOutAndCalculate Jan 26 '20 at 19:07
  • I'm not familiar with the API being used, but I suspect `endTransmission(false)` is doing something similar to `while(Wire.available() < 8);`, waiting to make sure the request has been sent. – user4581301 Jan 26 '20 at 19:26

1 Answers1

1

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.

Xgh05t
  • 230
  • 2
  • 11