Take the 2-minute tour ×

I have accelerometer values for the 3 axis(usually when there is only gravity contains data between -1.0 and 1.0 ):

  float Rx;
  float Ry;
  float Rz;

I make soma calculations, then I get the angles for each axis.

  float R =  sqrt(pow(Rx,2)+pow(Ry,2)+pow(Rz,2));
  float Arx = acos(Rx/R)*180/M_PI;
  float Ary = acos(Ry/R)*180/M_PI;
  float Arz = acos(Rz/R)*180/M_PI;

Then I set the values for the box angles in opengl

rquad = Arx;
yquad = Ary;

Which rotates my box:


It work on hemisphere. I would like to use the full sphere and I know that I have to use the Arz value to make it work, but I don't know how can I use that for this rotation. Could you help me?

Update: The final answer is in my case:

  rquad = -atan2(Rx/R, Rz/R)*180/M_PI;
  yquad = -atan2(Ry/R, Rz/R)*180/M_PI;
share|improve this question

2 Answers 2

up vote 13 down vote accepted

The correct answer is:

Roll = atan2(Y, Z) * 180/M_PI;
Pitch = atan2(-X, sqrt(Y*Y + Z*Z)) * 180/M_PI;

Source: http://www.freescale.com/files/sensors/doc/app_note/AN3461.pdf (page 10, Eqn. 25 & 26)

uesp's answer is wrong. It looks like an acceptable approximation until pitch and roll both go above 45 degrees.

I may be assuming a different orientation convention, but even if you swap axes and invert values in any consistent way, uesp's computations will never be equivalent.

share|improve this answer
I know this question is very old, but I hate seing wrong ansswers. I myself was looking for this, and I found hundreds of hits in google, most of them with the same wrong answer –  matteo Apr 25 '12 at 17:14
By the way I forgot to mention the source, which contains a very exhaustive explanation: freescale.com/files/sensors/doc/app_note/AN3461.pdf –  matteo Apr 25 '12 at 17:14
Assuming you are using equations 25 & 26 from the linked source (a great resource btw) shouldn't the Pitch be atan2(-X, sqrt(Y*Y + Z+Z))? –  uesp Apr 25 '12 at 18:22
Yes sorry, that was a typo, actually it's atan2(X, sqrt(YY+ZZ)) without the minus sign on X if we assume that the accelerometer gives positive values when the axis is aligned with gravity and points UPWARDS, which is at least what my phone does. In the article they assume the opposite convention. I haven't digged into why I don't need another minus sign for Roll but i've tried it and it works –  matteo Apr 25 '12 at 22:28
What are X, Y and Z? Do you mean Rx, Ry, and Rz? Or are you referring to something else completely? –  Anubian Noob Aug 3 at 15:45

I use the following calculations to convert our accelerometer readings into roll and pitch values:

Roll = atan2( sqrt(Y*Y + X*X), Z) * 180/M_PI;
Pitch = atan2( sqrt(X*X + Z*Z), Y) * 180/M_PI;

You may need to swap the X/Y/Z values or translate the Roll/Pitch depending on how your accelerometers are defined. To use them in the display them it is a simple matter of:

glRotatef (Pitch, 0.0f, 0.0f, 1.0f);
glRotatef (Roll,  1.0f, 0.0f, 0.0f);
share|improve this answer
Thanks for answering, but I tried it and I get 0°-180° for pitch and roll. I would like to get 0°-360° –  Roland Soós Sep 20 '10 at 21:01
I tried again with clear head :) It works well –  Roland Soós Sep 21 '10 at 9:35
the equation for roll is wrong. See my answare (I don't take any credit, I found it out there) –  matteo Apr 25 '12 at 17:16
I must confess to indeed basing my code and this answer on the variety of other sources out there but (unfortunately) never came across the source you posted. My equations indeed don't match anything in your source although they are close to equations 38 & 39 except for the reversed atan2() parameters. I would of also guessed that the pitch would be wrong and not the roll as from previous testing the roll appears to work fine from -90 to +90 degrees and the pitch has never been tested. I'll have to look at in more detail and figure it out... –  uesp Apr 25 '12 at 18:25
The wrong one is the roll, but the roll in your equation does work correctly from -90 to 90 IF the pitch stays at 0. It becomes wrong when the pitch is non-zero, and noticeable when the pitch goes above about 45 degrees. If "the pitch has never been tested" means that in your tests you only varied the roll keeping the pitch close to zero, that would explain that you didn't detect the error in the roll formula –  matteo Apr 25 '12 at 22:37

Your Answer


