

It is unwise to ever rely on undefined behavior in C or C++. Likely that everything runs fine and you get the expected result, but Than 127, then the bit shift causes a signed integer overflow, which inĬ and C++ is undefined behavior (read: “illegal operation”). On them, an int is 16-bitsĪnd can only hold values up to 32767. However, mostĪrduinos are based on an 8-bit AVR chip. Which is equivalent to multiplying by 256. Wire.read(), which is an int, and shifts it 8 bits to the left,


The expression Wire.read() << 8 takes the value returned by Just adding a couple of points to Michel Keijzers's answer. Is it that <<8| makes something like a sum or a logic OR to switch the register to read? However, I can't get how, if the first instruction only requests from the MPU_addr (constant), it changes to the consecutive registers. Hex numbers indicates, of course, the address of each register. In the loop, there are this lines for getting the next data from its registers: Wire.requestFrom(MPU_addr, 14, true) // request a total of 14 registersĪcX = Wire.read()<<8 | Wire.read() // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)ĪcY = Wire.read()<<8 | Wire.read() // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)ĪcZ = Wire.read()<<8 | Wire.read() // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L) I'm using the Arduino's site test code for getting raw values from a MPU-6050 accelerometer + gyro.
