Get data from MPU 6050 or GY 521 or MPU 9250 through I2C in Arduino IDE

You can use this code and configurations to get data from MPU 6050 or GY 521 or MPU 9250 with correct measurement units.

#include <Wire.h>

double accx, accy,accz, gyrox,gyroy,gyroz,temp;
const int Mpu=0x68;

void setup() 
{
Wire.begin();
Wire.setClock(400000); // I2C clock rate ,You can delete it but it helps the speed of I2C (default rate is 100000 Hz)
delay(100);
// turn Mpu on
Wire.beginTransmission(Mpu);
Wire.write(0x6B);  // Power register
Wire.write(0);     // wakes up the MPU-6050
Wire.endTransmission();
//

delay(100);
// Configure Gyro
Wire.beginTransmission(Mpu);
Wire.write(0x1B); // Gyro Config
Wire.write(0x0); // (0: 250 dps (131 LSB/dps); 1: 500 dps (65.5 LSB/dps) ; 2: 1000 dps ( 32.8 LSB/dps) ; 3: 2000 dps (16.4 LSB/dps)
Wire.endTransmission();
//

delay(100);
// Configure Accelerometer
Wire.beginTransmission(Mpu);
Wire.write(0x1C); // Accelerometer  Config
Wire.write(0x0); // (0: 2g (16384 LSB/g); 1: 4g (8192 LSB/g) ; 2: 8g ( 4096 LSB/g) ; 3: 16g (2048 LSB/g)
Wire.endTransmission();
//
delay(100);
Serial.begin(115200);  //Setting the baudrate
delay(100);

}
void loop()
{
// Get data from MPU
Wire.beginTransmission(Mpu);
Wire.write(0x3B); 
Wire.endTransmission();
Wire.requestFrom(Mpu,14);
accx=(int16_t)(Wire.read()<<8|Wire.read())/16384.00; // g
accy=(int16_t)(Wire.read()<<8|Wire.read())/16384.00; // g
accz=(int16_t)(Wire.read()<<8|Wire.read())/16384.00; // g
temp=(int16_t)(Wire.read()<<8|Wire.read())/340.00 + 36.53;  // Celsus
gyrox=(int16_t)(Wire.read()<<8|Wire.read())/131.00; // Dps 
gyroy=(int16_t)(Wire.read()<<8|Wire.read())/131.00; // Dps 
gyroz=(int16_t)(Wire.read()<<8|Wire.read())/131.00; // Dps
//
Serial.print("accx= ");
Serial.print(accx);
Serial.print(" accy= ");
Serial.print(accy);
Serial.print(" accz= ");
Serial.println(accz);

}

if you want to get Euler Angles directly and exactly you can see this:

Also Here is my code to get Euler angles from GY-25 (MPU 6050 + Kalman filter) through Serial connection:

Thanks for sharing but give it a proper coding format so that other can understand it easily.

You're welcome. Ok, I have done that.

Thanks for sharing a clear sample code.

You're welcom.