How to calibrate Mpu6050 using library?
I personally suggest you MPU6050_tockn library.https://github.com/tockn/MPU6050_tockn

If you use calcGyroOffsets() in setup(), it will calculate calibration of the gyroscope, and the value of the gyroscope will calibrated.

#include &lt;MPU6050_tockn&gt;
#include &lt;Wire.h&gt;

MPU6050 mpu6050(Wire);

void setup(){
  Wire.begin();
  mpu6050.begin();
  mpu6050.calcGyroOffsets();
}


If you use calcGyroOffsets(true) in setup(), you can see state of calculating calibration in serial monitor.

#include &lt;MPU6050_tockn&gt;
#include &lt;Wire.h&gt;

MPU6050 mpu6050(Wire);

void setup(){
  Serial.begin(9600);
  Wire.begin();
  mpu6050.begin();
  mpu6050.calcGyroOffsets(true);
}


Serial monitor:

Calculate gyro offsets
DO NOT MOVE MPU6050.....
Done!
X : 1.45
Y : 1.23
Z : -1.32
Program will start after 3 seconds


If you know offsets of gyroscope, you can set them by setGyroOffsets(), and you don't have to execute calcGyroOffsets(), so you can launch program quickly.

#include &lt;MPU6050_tockn&gt;
#include &lt;Wire.h&gt;

MPU6050 mpu6050(Wire);

void setup(){
  Serial.begin(9600);
  Wire.begin();
  mpu6050.begin();
  mpu6050.setGyroOffsets(1.45, 1.23, -1.32);
}


But if want to do it with Kalman filter goto https://github.com/jarzebski/Arduino-KalmanFilter/blob/master/KalmanFilter_MPU6050/KalmanFilter_MPU6050.ino It will guide you.