Go Down

Topic: Measuring tilt with an MMA7260Q accelerometer (Read 3 times) previous topic - next topic

deviukk

Hi,im trying to measure tiltangles with a MMA7260Q accelerometer. I'm using the following code:

Code: [Select]

float x, y, z;  // X and Y in-plane sensing
int i = 0;
float Ax, Ay, Az;
float angleX, angleY, angleZ;
float sens = 800; // mV/g
float Voff = 1650; // mV
const float pi = 3.14;


void setup()
{
 Serial.begin(9600);      // sets the serial port to 9600
}

void loop()
{
 if (i == 10)
 {
   x = x/10;
   y = y/10;
   z = z/10;
   Ax = x-Voff/sens;
   Ay = y-Voff/sens;
   Az = z-Voff/sens;
   angleX = atan(Ax/sqrt(pow(Ay,2)+pow(Az,2)));
   angleY = atan(Ay/sqrt(pow(Ax,2)+pow(Az,2)));
   angleZ = atan(Az/sqrt(pow(Ay,2)+pow(Ax,2)));
   angleX = angleX*180/pi;
   angleY = angleY*180/pi;
   angleZ = angleZ*180/pi;
   Serial.print("Angles: ");
   Serial.print(angleX);    // print the rotational rate in the X axis
   Serial.print(" ");       // prints a space between the numbers
   Serial.println(angleY);  // print the rotational rate in the Y axis
   Serial.print(" ");       // prints a space between the numbers
   Serial.println(angleZ);  // print the rotational rate in the Y axis
   delay(100);              // wait 100ms for next reading
   i = 0;
   x = 0;
   y = 0;
   z = 0;
 }
 else
 {
   x = x + analogRead(0);
   y = y + analogRead(1);
   z = z + analogRead(2);
   i = i + 1;
   delay(50);
 }
   
}


I use time-averaging over 10 samples.. Offset and Sensitivity came from the datasheet..

When I align the axis of the accelerometer with the ground, I receive angles of x: 27°, y:28° and z: 47° when i accept them to be 0°...

Now when I start tilting the accelerometer, the angles change (dont think correctly). The problem is that the highest angle is 50° and the lowest 20°. Behind those values the angles start moving in the other direction again.. Must be something wrong in the code. Can someone please help?!

Thx

Fabio Varesano

Code: [Select]
Ax = x-Voff/sens;

You are using the read value (x above) as it would be expressed in mV as the other numbers. But this is not the case. analogRead() will return a value from 0 to 1024 proportional to the voltage on the associated pin.

The following holds true:
read value from analogRead() : 1024 = read value in mV : 3.3 V (provided that you connected the AREF to 3.3V)

so if you want x expressed in mV..
x = (analogRead() / 1024) * 3300mV

Hope this helps,

Fabio Varesano

Groove

analogRead() will return a value from 0 to 1023 proportional to the voltage on the associated pin
Per Arduino ad Astra

Fabio Varesano

ops.. sure.. replace 1024 with 1023 in my post above.

liudr

Besides the problems pointed out already, could you link to the spec sheet of the part? Some accelerometers have on board 5V-3.3V converter and only output 3.3/2=1.65V when there is no acceleration. I want to make sure you're not overlooking this possibility.

Go Up