Hello,
I'm trying to measure pitch angle between 0-180 degress. I'm using Arduino Uno R3, my Arduino Compiler version is 1.6.8 and I'm using ADXL345 for the measurement.
When I put the sensor horizontal to table I'm measuring pitch angle about -12.68 degrees and when it becomes orthogonal with the table I'm measuring pitch angle about 112 degrees. Theese values have to be 0-90 degrees and this is the problem. My measurement is wrong and I don't know why.
I've used I2C communication between the devices. Here is my pin configuration:
ADXL345 SCL --> Arduino SCL
ADXL345 SDA --> Arduino SDA
Vss --> 3.3 V
Cs --> 3.3 V
GND --> GND
ADXL345 datasheet link: http://www.analog.com/media/en/technical-documentation/data-sheets/ADXL345.pdf
Here is my code:
#define ADXL345 0x53 // device adress
#define POWER_CTL 0x2D
#define DATA_FORMAT 0x31
#define DATAX0 0x32
#include <Wire.h>
byte buff[6];
int rawX, rawY, rawZ;
double x,y,z,pitch;
void setup()
{
Serial.begin(9600);
Wire.begin();
//measurement mode
Wire.beginTransmission(ADXL345);
Wire.write(POWER_CTL);
Wire.write(0x08);
Wire.endTransmission();
// DATA FORMAT = +-16g, Full Resolution (4 mg/LSB) 13 bits. (see table 1 and table 21)
Wire.beginTransmission(ADXL345);
Wire.write(DATA_FORMAT);
Wire.write(0x0B);
Wire.endTransmission();
}
void loop()
{
int i=0;
Wire.beginTransmission(ADXL345);
Wire.write(DATAX0);
Wire.requestFrom(ADXL345, 6);
while(Wire.available())
{
buff[i] = Wire.read();
++i;
}
Wire.endTransmission();
rawX = ((int)buff[1] << 8) | buff[0];
rawY = ((int)buff[3] << 8) | buff[2];
rawZ = ((int)buff[5] << 8) | buff[4];
//Gs = Measurement Value * (G range/2^n), n = bits
//Gs = Measurement Value *(32/2^13) = 0.00390625
x = rawX * 0.00390625;
y = rawY * 0.00390625;
z = rawZ * 0.00390625;
pitch = atan2(x,sqrt(y*y+z*z));
pitch *= 57.2957795+180; // radian to degree
Serial.print("rawX = ");
Serial.print(rawX);
Serial.print("\trawY = ");
Serial.print(rawY);
Serial.print("\trawZ = ");
Serial.print(rawZ);
Serial.print("\tx = ");
Serial.print(x);
Serial.print("\ty = ");
Serial.print(y);
Serial.print("\tz = ");
Serial.print(z);
Serial.print("\tpitch=");
Serial.println(pitch);
delay(100);
}
Here some pictures about what am I doing:
When sensor is horizontally to the table:
I'm getting -12 degrees (which has supposed to be 0 degree)
When sensor is orthogonal with table:
I'm getting 112 degrees (which has supposed to be 90 degree)
Should I add offset values for each of axes raw values ?