Hi Hitesh_KHD,

A good place to start understanding the MPU6050 is here: http://www.geekmomprojects.com/gyroscopes-and-accelerometers-on-a-chip/.

This article describes how use a simple complementary filter to fuse the gyroscope and accelerometer data to obtain Euler angles for pitch and roll.

The gyroscope measures rotational speed in say degrees/s or radians/s, this can be integrated to obtain the angle again in degrees or radians, however like you mention it has no reference point and additionally, like all gyroscopes, drifts to a greater or lesser extent over time.

To provide a reference point we use the accelerometer to measure gravity. Using some trigonometry we can convert these raw acceleration values to pitch and roll angle with respect to the horizontal, however the accelerometer is noisy and any acceleration other than rotation can distort the measurements.

To solve this we can fuse the integrated gyroscope and accelerometer pitch and roll angles using a complementary filter. The complementary filter takes around 99.9% of the gyroscope angle mesurements and adds this to around just 0.1% of the accelerometer. Calculating this each time around the Arduino's loop() function the complementary filter has the magical effect of providing stable angle measurements that are tied to the horizontal reference. In addition the angles are more or less acceleration invariant and compensate for the gyroscope's drift.

To calibrate the gyroscope just take the mean average of a couple of hundred raw gyro readings at start-up and subtract from the raw values taken during operation.

To calibrate the accelerometer, place it on level-ground then again take the average raw values and again subtract from the raw values during operation. It's also possible to store the accelerometer values in EEPROM to save you having to calibrate each time you start-up.

There's also the issue of yaw compensation that the article above doesn't discuss. This involves transferring any small movements in yaw rotation from pitch to roll angle or vice-versa. This is covered by Joop Brokking in his quadcopter video on Youtube: MPU-6050 6dof IMU tutorial for auto-leveling quadcopters with Arduino source code - Part 2 - YouTube.

Another option is to just get the angle values using the MPU6050's in-built DMP (Digital Motion Processor). Jeff Rowberg's I2Cdev library for the MPU6050 has example code for this.

If however you require full 3D orientation then you'll also need a 3-axis magnetometer, as the accelerometer is unable to measure yaw rotation. You'll also need more complex mathematics such as Quaternions or DCM (Direction Cosine Matrix). For this reference Kris Winer's Github code: kriswiner (Kris Winer) · GitHub.