Inconsistent data about gravity vector using mpu6050

Hello everyone,
I am trying to use the mpu6050 module with arduino nano and I have encountered a problem that I cannot explain to myself.
When the component is stationary it should only detect the acceleration of gravity (g), so the magnitude of the vector g should be constant at each measurement .. but in my case I see it change and even a lot (from a value of 1.25 to 0.8).

#include <MPU6050_tockn.h>
#include <Wire.h>

MPU6050 mpu6050(Wire);

float angX_cal = 0, angY_cal = 0, angZ_cal = 0;
float accX, accY, accZ;

void setup() {
  Serial.begin(9600);

  Wire.begin();
  mpu6050.begin();
}

void loop() {
  mpu6050.update();
  accX = mpu6050.getAccX();
  accY = mpu6050.getAccY();
  accZ = mpu6050.getAccZ();
  Serial.print(accX);
  Serial.print(" / ");
  Serial.print(accY);
  Serial.print(" / ");
  Serial.print(accZ);
  Serial.print(" / ");
  Serial.println(sqrt(accX*accX +accY*accY +accZ*accZ));
  delay(100);
}

These are the data detected by mpu6050 from stationary in two different positions, the last value is the magnitude of the total acceleration.

Does anyone know if this problem is due to my error or if it is a component problem?

That said, my goal was to be able to eliminate the gravity vector with some trigonometric calculation in order to read only the pure accelerations, if someone knows a library that is able to do this it would be very useful.
Thank you all
Riccardo

If the sensor really is stationary, and you see large jumps in the acceleration measurements, something is wrong.

Try a different library.

If the sensor is in an environment with changing orientation and/or accelerations, you will not be able to accurately enough calculate and subtract the gravity vector to be useful for any practical purpose.

See the discussion on these pages explaining the problem: https://www.chrobotics.com/library

Edit: chrobotics seems to be offline at the moment.

Note: Each axis will have a different offset error ('zero' != 0) and scale error ('1g' != 1g). And those errors often change with temperature. You have to calibrate your particular sensor to determine the offset and scale errors so you can correct for them.

THEN you can calculate the gravity vector and hope to get a repeatable answer.

This is the shortest test sketch for a MPU-6050: https://playground.arduino.cc/Main/MPU-6050/#short.
Do they give the same numbers ?

The MPU-6050 is old and noisy and outdated. In the past there were modules with a wrong or missing capacitor and those gave bad results. Who knows what is wrong with your module. Perhaps it is not even a MPU-6050 chip.

Those values should not chance that much, and the 1g should not be 0.8g, even without calibration.

Can you buy something better ?

Thanks for the replies,
now i think i solved it, the problem was that wrong offsets were set for what i wanted to measure.
I tried to calculate my offsets in the setup and I added 1g to the z axis, in this way the measurements are much more precise with an error of about 5% (on the measure of g).

The sketch:

#include <MPU6050_tockn.h>
#include <Wire.h>

MPU6050 mpu6050(Wire);

float accX_cal = 0, accY_cal = 0, accZ_cal = 0;
float accX, accY, accZ, acc_tot;
float err=0;

void setup() {
  Serial.begin(9600);

  Wire.begin();
  mpu6050.begin();

  for (int i = 0; i < 1000; i++) {
    mpu6050.update();
    accX_cal += mpu6050.getAccX();
    accY_cal += mpu6050.getAccY();
    accZ_cal += mpu6050.getAccZ();
    delay(1);
  }
  accX_cal /= 1000;
  accY_cal /= 1000;
  accZ_cal = accZ_cal / 1000 - 1;
}

int count = 0;

void loop() {
  if (count < 1000) {

    //these lines are only to move your mpu6050 in an other position without affecting the measurement of g 
    if (count % 300 == 0){
      Serial.println("move");
      delay(2000);
    }
    mpu6050.update();
    accX = mpu6050.getAccX() - accX_cal;
    accY = mpu6050.getAccY() - accY_cal;
    accZ = mpu6050.getAccZ() - accZ_cal;
    acc_tot = sqrt(accX * accX + accY * accY + accZ * accZ);
    err += abs(acc_tot - 1);    
    count++;
    Serial.println(acc_tot);
    delay(20);
  }
  if (count == 1000){
    Serial.print("average error: ");
    Serial.println(err / count);
    count++;
  }
}

Thank you all.
Riccardo

That won't work, ever. But you would have figured that out eventually.

Here is the best approach to calibrating accelerometers (and magnetometers) Tutorial: How to calibrate a compass (and accelerometer) with Arduino | Underwater Arduino Data Loggers

But it really is a waste of time to work with the ancient, poorly performing and obsolete MPU-6050.