Sensing tilt using accelerometer alone

johnwasser:
When you tilt the X axis up past 90 degrees, the Z axis goes through zero and then starts going positive. Since yrot is based on the X and Z measurements, when Z goes from negative to positive you get a different answer, even though Y is staying near zero.

To get pitch and roll, try:

pitch=atan2(ax,sqrt((ay*ay)+(az*az)))

roll=atan2(ay,sqrt((axax)+(azaz)))




Note that the result is in Radians so if you want degrees you still have to convert. You shouldn't get a problem with both arguments to atan2() being zero unless your accelerometer is in free-fall. :)

Great, thank you.
I think I understand the point, in case of pitch,you need to take the projection of the y and z vectors in the y-z plane so that you get the angle b/w the x vector and this projected vector.
I have not been able to try this, I am out of town for now.
I just would like to ask for 1 last favour, can you please explain how atan2 works. I have the basic understanding, but as you saw that wasn't good enough. I have never found a good answer to this question.
This would be a great help to a lot of people starting out in robotics, I've noticed even the pros do not have a clear understanding of the concept. I wish to share that understanding in my small local robotics group.
Thanks a lot