MPU-9250 Calibration Code not working

Hello,

I am new to coding, I am trying to calibrate the MPU-9250 IMU sensor. As of now im just trying to get the accelerometer to show 0 in the X and Y and -9.8 in the Z when it is flat.

This is my code so far. I am unsure if I incorporated the calibration functions from this document correctly please help:

Link to document:

My GitHub - bolderflight/mpu9250: MPU-9250 sensor driver.

I also attached my code.

#include "MPU9250.h" // includes MPU9250 library

// an MPU9250 object with the MPU-9250 sensor on I2C bus 0 with address 0x68
MPU9250 IMU(Wire,0x68); // this means that AD0 is grounded


int status;
int calibrateAccel(); //idk if this is supposed to be here


void setup() {
  
  // serial to display data
  Serial.begin(115200);
  while(!Serial) {}

// ACCELEROMETER CALIBRATION


status = IMU.calibrateAccel(); // (optional) int calibrateAccel() This function will estimate the bias and scale factor needed to calibrate the accelerometers. This function works one axis at a time and needs to be run for all 6 sensor orientations. After it has collected enough sensor data, it will estimate the bias and scale factor for all three accelerometer channels and apply these corrections to the measured data. Accelerometer calibration only needs to be performed once on the IMU, the get and set functions detailed below can be used to retrieve the estimated bias and scale factors and use them during future power cycles or operations with the IMU. This function returns a positive value on success and a negative value on failure.


// (optional) float getAccelBiasX_mss() This function returns the current accelerometer bias in the X direction in units of m/s/s.

float axb;
axb = IMU.getAccelBiasX_mss();

//(optional) float getAccelScaleFactorX() This function returns the current accelerometer scale factor in the X direction

float axs;
axs = IMU.getAccelScaleFactorX();

// (optional) float getAccelBiasY_mss() This function returns the current accelerometer bias in the Y direction in units of m/s/s.

float ayb;
ayb = IMU.getAccelBiasY_mss();

// (optional) float getAccelScaleFactorY() This function returns the current accelerometer scale factor in the Y direction.

float ays;
ays = IMU.getAccelScaleFactorY();

// (optional) float getAccelBiasZ_mss() This function returns the current accelerometer bias in the Z direction in units of m/s/s.

float azb;
azb = IMU.getAccelBiasZ_mss();

//(optional) float getAccelScaleFactorZ() This function returns the current accelerometer scale factor in the Z direction.

float azs;
azs = IMU.getAccelScaleFactorZ();

//// (optional) void setAccelCalX(float bias,float scaleFactor) This function sets the accelerometer bias (m/s/s) and scale factor being used in the X direction to the input values.
//float axb = 0.01; // accel bias of 0.01 m/s/s
//float axs = 0.97; // accel scale factor of 0.97
//IMU.setAccelCalX(axb,axs);
//
//// (optional) void setAccelCalY(float bias,float scaleFactor) This function sets the accelerometer bias (m/s/s) and scale factor being used in the Y direction to the input values.
//float ayb = 0.01; // accel bias of 0.01 m/s/s
//float ays = 0.97; // accel scale factor of 0.97
//IMU.setAccelCalY(ayb,ays);
//
//// (optional) void setAccelCalZ(float bias,float scaleFactor) This function sets the accelerometer bias (m/s/s) and scale factor being used in the Z direction to the input values.
//float azb = 0.01; // accel bias of 0.01 m/s/s
//float azs = 0.97; // accel scale factor of 0.97
//IMU.setAccelCalZ(azb,azs);

//// ACCELEROMETER CALIBRATION CODE ENDS


  // start commnication with IMU 
  status = IMU.begin();
  if (status < 0) {
    Serial.println("IMU initialization unsuccessful");
    Serial.println("Check IMU wiring or try cycling power");
    Serial.print("Status: ");
    Serial.println(status);
    while(1) {}
  }
}

void loop() {
  // read the sensor
  IMU.readSensor();
  // print the data
  Serial.print("AccelX: ");
  Serial.print(IMU.getAccelX_mss(),6);
  Serial.print("  ");
  Serial.print("AccelY: ");  
  Serial.print(IMU.getAccelY_mss(),6);
  Serial.print("  ");
  Serial.print("AccelZ: ");  
  Serial.println(IMU.getAccelZ_mss(),6);

Serial.print("AccelXBias: ");
  Serial.print(IMU.getAccelBiasX_mss(),6);
  Serial.print("  ");
  Serial.print("AccelYBias: ");  
  Serial.print(IMU.getAccelBiasY_mss(),6);
  Serial.print("  ");
  Serial.print("AccelZBias: ");  
  Serial.println(IMU.getAccelBiasZ_mss(),6);

Serial.print("AccelXsf: ");
  Serial.print(IMU.getAccelScaleFactorX(),6);
  Serial.print("  ");
  Serial.print("AccelYsf: ");  
  Serial.print(IMU.getAccelScaleFactorY(),6);
  Serial.print("  ");
  Serial.print("AccelZsf: ");  
  Serial.println(IMU.getAccelScaleFactorZ(),6);



  
//  Serial.print("GyroX: ");
//  Serial.print(IMU.getGyroX_rads(),6);
//  Serial.print("  ");
//  Serial.print("GyroY: ");  
//  Serial.print(IMU.getGyroY_rads(),6);
//  Serial.print("  ");
//  Serial.print("GyroZ: ");  
//  Serial.println(IMU.getGyroZ_rads(),6);
//
//  Serial.print("MagX: ");  
//  Serial.print(IMU.getMagX_uT(),6);
//  Serial.print("  ");  
//  Serial.print("MagY: ");
//  Serial.print(IMU.getMagY_uT(),6);
//  Serial.print("  ");
//  Serial.print("MagZ: ");  
//  Serial.println(IMU.getMagZ_uT(),6);
//  
  Serial.print("Temperature in C: ");
  Serial.println(IMU.getTemperature_C(),6);
  Serial.println();
  delay(100);
}

onlyacc_main_wcalib.ino (6.44 KB)

You are not going to get an accelerometer to measure 0. Heck, I worked with accelerometers that cost over 3 million dollars each and they never showed 0; the earth wobbles.

when it is flat

How would you know when it is flat?

Simple calibration tutorial: Programming and Calibration | ADXL345 Digital Accelerometer | Adafruit Learning System

Much better calibration tutorial: Tutorial: How to calibrate a compass (and accelerometer) with Arduino | Underwater Arduino Data Loggers