Calibration adxl345 acelerometer


I am trying to calibrate the measurements of an accelerometer adxl345 of the imu gy80, with the following code that I found on the internet I collect the maximum and minimum values ​​that are these:

Minimos: X=0 | Y=0 | Z=0 Maximos: X=5019 | Y=5019 | Z=5010

The three axes measure the same value, and when I calibrate it with the offset and the gain I recived 9.81 m / s ^ 2 in the three axes.

I know I should have x=0; y=0; and z=9,81 [m^2]

How do I solved that?

Thankyou verymuch!

#include <Wire.h> 
#include <IMU_GY80.h> 

IMU_GY80 CALI = IMU_GY80(); 

  int AcelMinX = 0;
  int AcelMaxX = 0;
  int AcelMinY = 0;
  int AcelMaxY = 0;
  int AcelMinZ = 0;
  int AcelMaxZ = 0; 
void setup()
          Serial.println("Calibracion del acelerometro ADXL345 y magnetometro HMC5883L");

void loop()
              Serial.println("Envia cualquier caracter para mostrar los valores"); 
                       while (!Serial.available()){} // Esperando el valor que sera enviado por serial para continuar

// una vez obtenido el valor de los ejes, se procede a obtener los maximos y minimos de cada eje
   if(CALI.acel[1] < AcelMinX) AcelMinX = CALI.acel[1];
   if(CALI.acel[1] > AcelMaxX) AcelMaxX = CALI.acel[1] ;

   if(CALI.acel[0] < AcelMinY) AcelMinY = CALI.acel[0];
   if(CALI.acel[0] > AcelMaxY) AcelMaxY = CALI.acel[0];

   if(CALI.acel[2] < AcelMinZ) AcelMinZ = CALI.acel[2];
   if(CALI.acel[2] > AcelMaxZ) AcelMaxZ = CALI.acel[2];

// Minimos Acel (Estatico)
   Serial.print("Minimos Acel: "); Serial.print("X="); Serial.print(AcelMinX); Serial.print(" | ");
                                               Serial.print("Y="); Serial.print(AcelMinY); Serial.print(" | "); 
                                               Serial.print("Z="); Serial.print(AcelMinZ); Serial.println();
// Maximos Acel (Estatico)
   Serial.print("Maximos Acel: "); Serial.print("X="); Serial.print(AcelMaxX); Serial.print(" | ");
                                                Serial.print("Y="); Serial.print(AcelMaxY); Serial.print(" | "); 
                                                Serial.print("Z="); Serial.print(AcelMaxZ); Serial.println();

   while (Serial.available())
; // Se elimina el buffer

Thankyou, I actually got the code from that page,

but it's obvious that I'm measuring something bad

but it's obvious that I'm measuring something bad

Not to us, because we don't know what you are measuring.

I actually got the code from that page

The code you posted is incomplete, and did NOT come from the Adafruit page I linked.

Rolfe Schmidt's sphere-fitting method form 2011 is the best one I've come across.

Here is the first blog post in the series

I took his code and modified it for my purposes. I use it on all of my accelerometer projects. Usually I just load the calibration code to the Arduino and calibrate a number of accelerometers. They get serial numbers written on them and I keep the calibration constants in a spreadsheet.

Rolfe Schmidt’s sphere-fitting method form 2011 is the best one I’ve come across.

Yes, Schmidt’s method is quite ingenious. He makes only one pass through the data, storing only useful intermediate sums, so there is no need for large arrays.

There is a nonfatal bug in the parameter refinement algorithm, as you will see in the discussion that follows the third installment of that blog. If one makes changes to the code in function “calibrate()” along the following lines, convergence is much, much faster.

The version below is for debugging using Code::Blocks but also runs on Arduino with replacement of printf().

    findDelta(JtJ, JtR);

    // bug: scale down (divide by) beta[3..5], not multiply!
    // try change = sum squared relative shifts in all params (JR)
    // below: was "*(_beta[.]*_beta[.])"
    // change = JtR[0]*JtR[0] + JtR[1]*JtR[1] + JtR[2]*JtR[2] + JtR[3]*JtR[3]/(_beta[3]*_beta[3]) + JtR[4]*JtR[4]/(_beta[4]*_beta[4]) + JtR[5]*JtR[5]/(_beta[5]*_beta[5]);

    change =0.;
    for (i=0; i<6; i++) {
            if ( fabs(_beta[i]) > 1.0 ) change += JtR[i]*JtR[i]/(_beta[i]*_beta[i]);  //squared magnitude, relative parameter shifts
            else change += JtR[i]*JtR[i];  //avoid dividing by small parameter values
    printf("it# %d, rel. delta = %10.5f\n",20-num_iterations,change);
    //Serial.print(", ");
    if (!isnan(change)) {

Thank you very much for the help!