ADXL345 send divergent Degrees

Hi Everyone

I played around with the ADXL345 to build a electronic bubble level. Code workes almost right, but the released Degrees differs about approx. 5 Degrees from exakt horizontal position measured by two different mechanical bubble levels. Is there any need to adjust the accelerometer Chip? Thanks for any Answers and good Ideas!

#include "Wire.h"
#include "ADXL345.h"

const float alpha = 1;

ADXL345 acc;
void setup()
{
  acc.begin();
  Serial.begin(9600);
  delay(100);
}

void loop()
{
  double Xg, Yg, Zg;
  acc.read(&Xg, &Yg, &Zg);

  //Low Pass Filter to smooth out data
  double fXg = Xg * alpha + (fXg * (1.0 - alpha));
  double fYg = Yg * alpha + (fYg * (1.0 - alpha));
  double fZg = Zg * alpha + (fZg * (1.0 - alpha));
  //Convert to Degrees
  int x = RAD_TO_DEG * (atan2(-fYg, -fZg) + PI);
  int y = RAD_TO_DEG * (atan2(-fXg, -fZg) + PI);
  int z = RAD_TO_DEG * (atan2(-fYg, -fXg) + PI);
  
  Serial.print(x);
  Serial.print("   ");
  Serial.println(y);

  delay(250);
}

All of the accels that I have used have had an inherent offset. The offsets seem to be repeatable, meaning that they don't change. You can set the accel to level and record the offsets. Then code them into your program. In your code you can then subtract the offsets.

The gain for each axis is usually a bit different, too. Google "arduino calibrate accelerometer" for some suggestions on how to deal with these two problems.

Playing around with the values and adding the following two Lines in the loop(). Seems to Work ...

Xg+= - 0.12;
Yg+= + 0.08;

Thanks!