SparkFun Forums 

Where electronics enthusiasts find answers.

Your source for all things Atmel.
By basic15
#139358
Hello,

I am working with the ADXL 335 accelerometer from Sparkfun and trying to output a tilt measurement using the Arduino Fio. I have tried a variety of different codes, and have verified the sensitivity (0.3) with the datasheet. However, the angle measurements are still incorrect. When I tilt and rotate the accelerometer, the angle measurements are changing, but they never go completely to zero. They are only showing relatively small changes in angle despite drastic tilting on my part. I have an accurate ADC conversion, so I know that is not the problem. Is there any way I can verify my offset value? Or is something else wrong in my code? My code is given below. Does anyone already have a successful code? Any input would be greatly appreciated!

Here is the code I have so far:
Code: Select all
// A0: self test, A1: z-axis, A2: y-axis, A3: x-axis, A4: ground, A5: vcc
const int groundpin = 18;           // analog input pin 4 -- ground
const int powerpin = 19;            // analog input pin 5 -- voltage
const int xpin = A3;                // x-axis of the accelerometer
const int ypin = A2;                // y-axis
const int zpin = A1;                // z-axis (only on 3-axis models)
const float pi = 3.14;

//const int Voffset = 1.65;
const float Sensitivity = 0.3;

void setup()
{
  Serial.begin(57600);
  //Serial.print(127);
  
  pinMode(groundpin, OUTPUT);                // Provide ground and power by using the analog inputs as normal
  pinMode(powerpin, OUTPUT);                 // digital pins.  This makes it possible to directly connect the
  digitalWrite(groundpin, LOW);              // breakout board to the Arduino.  If you use the normal 5V and
  digitalWrite(powerpin, HIGH);              // GND pins on the Arduino, you can remove these lines
}

void loop()
{  
  float Vout_x = analogRead(xpin);         // x axis      /*do we need the step value here?*/
  float Vout_y = analogRead(ypin);         // y axis
  float Vout_z = analogRead(zpin);         // z axis
  
  float Vm_x = Vout_x * .003223;            // Vmeasured for x axis
  float Vm_y = Vout_y * .003223;            // Vmeasured for y axis
  float Vm_z = Vout_z * .003223;            // Vmeasured for z axis
  
  float x= Vm_x - 1.64;
  float y= Vm_y - 1.62;
  float z= Vm_z - 1.98;
  
  Serial.print("X-axis     ");
  Serial.print(analogRead(xpin), DEC);           // print the sensor values
  Serial.print("\t");
  Serial.print("Y-axis     ");
  Serial.print(analogRead(ypin), DEC);
  Serial.print("\t");
  Serial.print("Z-axis     ");
  Serial.println(analogRead(zpin), DEC);
  
  //delay(500);          // delay before next reading


  float R_x = (x/(Sensitivity));    //radians
  float R_y = (y/(Sensitivity));
  float R_z = (z/(Sensitivity));
  
  Serial.print("x = ");          
  Serial.println(x, DEC);
  Serial.print("y = ");         
  Serial.println(y, DEC);
  Serial.print("z = ");          
  Serial.println(z, DEC);
  
  float Angle_x = acos(R_x);
  float Angle_y = acos(R_y);
  float Angle_z = acos(R_z);
  
  float degree_x = (180/pi)*Angle_x;
  float degree_y = (180/pi)*Angle_y;
  float degree_z = (180/pi)*Angle_z;

  Serial.print("Angle_x = ");          //Angle x in degrees
  Serial.println(degree_x, DEC);
  Serial.print("Angle_y = ");          //Angle y in degrees
  Serial.println(degree_y, DEC);
  Serial.print("Angle_z = ");          //Angle z in degrees
  Serial.println(degree_z, DEC);
  
  delay(500);
}