Sensing tilt using accelerometer alone

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((ax*ax)+(az*az)))

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. :slight_smile: