Accelerometers, by definition, are used to measure how fast an object is accelerating. While this is traditional in the horizontal X/Y plane, adding the Z axis can unlock a whole new level of capabilities – especially for orientation sensing.

SOH CAH TOA Waterfall

As you might recall from Trigonometry, the inverse tangent function can be used to obtain an angle from two vector rays. This can be extrapolated to get an objects position in 3 dimensional space.

In the graphic presented below, both the X and Z axes are subjected to the force of gravity.

A way to always verify the values from an accelerometer is to use the Pythagorean Theorem to validate that the readings all add up to approximately 9.8 meters per second squared. We’ll also need the magnitude for the calculations.

We can do this using 12 = a2 + b2 + c2 , which morphs into:

9.82 = ax2 + ay2 + az2
9.8 = sqrt ( ax2 + ay2 + az2 )

If you run a script at boot-up when the object is relatively level, you might get something along the lines of
accelerometerX = 0.05 m/s2
accelerometery = 0.05 m/s2
accelerometerZ = -9.75 m/s2

sqrt ( 0.052 + 0.052 + -9.752 )
sqrt ( 0.0025 + 0.0025 + 95.0625)
sqrt (95.0675) = 9.75 m/s2

3 dimensional pitch and roll sketch

Example 1: Pitch (2-D)

Let us presume the following:
accelerometerX = -3.0 m/s2
accelerometery = 0.0 m/s2
accelerometerZ = -9.3 m/s2

First, we validate the data:
g = sqrt ( -3.02 + 0.02 + -9.32 )
g = sqrt ( 9.0 + 0.0 + 86.5)
g = sqrt (95.5) = 9.77 m/s2

Pitch in 2D can be found in three ways – using sine, cosine, or tangent.

sin-1( ax / g )
sin-1( -3.0 / 9.77 ) = 17.8 degrees of pitch

We can use sine because ax is the opposite side and gravity is the hypotenuse

cos-1( az / g)
cos-1( -9.3 / 9.77 ) = 17.8 degrees of pitch

We can use cosine because az is the adjacent side and gravity is the hypotenuse

tan-1( ax / az )
tan-1( -3.0 / -9.3 ) = 17.8 degrees of pitch

We can use tangent because ax is the opposite side and az is the adjacent side.

Example 2: Pitch (2-D)

Let us presume the following:
accelerometerX = -7.5 m/s2
accelerometery = 0.0 m/s2
accelerometerZ = -6.2 m/s2

First, we validate the data:
sqrt ( -7.52 + 0.02 + -6.22 )
sqrt ( 56.25 + 0.0 + 38.44)
sqrt (94.69) = 9.73 m/s2

Pitch in 2D can be found three ways – using sine, cosine, or tangent.

sin-1( ax / g)
sin-1( -7.5 / 9.73 ) = 50.4 degrees of pitch

We can use sine because ax is the opposite side and gravity is the hypotenuse

cos-1( az / g )
cos-1( -6.2 / 9.73 ) = 50.4 degrees of pitch

We use cosine because az is the adjacent side and gravity is the hypotenuse

tan-1( ax / az )
tan-1( -7.5 / -6.2 ) = 50.4 degrees of pitch

We can use tangent because ax is the opposite side and az is the adjacent side.

Example 3: Roll (2-D)

Let us presume the following:
accelerometerX = 0.0 m/s2
accelerometery = -2.5 m/s2
accelerometerZ = -9.5 m/s2

First, we validate the data:
sqrt ( -0.02 + 2.52 + -9.52 )
sqrt ( 0.0 + 6.25 + 90.25)
sqrt (96.5) = 9.82 m/s2

Roll in 2D can be found two ways – using sine or cosine.

sin-1( ay / g)
sin-1( -2.5 / 9.82 ) = 14.75 degrees of roll

We can use sine because ay is the opposite side and gravity is the hypotenuse

cos-1( az / g )
cos-1( -9.5 / 9.82 ) = 14.67 degrees of roll

We use cosine because az is the adjacent side and gravity is the hypotenuse

tan-1( ay / az )
tan-1( -2.5 / -9.5 ) = 14.7 degrees of roll

We can use tangent because ay is the opposite side and az is the adjacent side.

Example 4: Roll (2-D)

Let us presume the following:
accelerometerX = -0.0 m/s2
accelerometery = -4.5 m/s2
accelerometerZ = -8.6 m/s2

First, we validate the data:
sqrt ( -0.02 + 4.52 + -8.62 )
sqrt ( 0.0 + 20.25 + 73.96)
sqrt (94.21) = 9.70 m/s2

roll in 2D can be found two ways – using sine or cosine.

sin-1( ay / g)
sin-1( -4.5 / 9.70 ) = 27.64 degrees of roll

We can use sine because ax is the opposite side and gravity is the hypotenuse

cos-1( az / g )
cos-1( -8.6 / 9.70 ) = 27.55 degrees of roll

We use cosine because az is the adjacent side and gravity is the hypotenuse

tan-1( ay / az )
tan-1( -4.5 / -8.6 ) = 27.62 degrees of pitch

We can use tangent because ay is the opposite side and az is the adjacent side.

Example 5: Generally Flat

Let us presume the following:
accelerometerX = -0.8 m/s2
accelerometery = -0.5 m/s2
accelerometerZ = -9.7 m/s2

First, we validate the data:
sqrt ( -0.82 + 0.52 + -9.72 )
sqrt ( 0.64 + 0.25 + 94.09)
sqrt (94.98) = 9.74 m/s2

Pitch

When working in a 3 dimensional space, simply using sine, cosine, or tangent as in 2D space is not quite as straight forward.
Example:
sin-1( ax / g ) = sin-1( -0.8 / 9.74 ) = 4.7 degrees of pitch
cos-1( az / g ) = cos-1( -9.7 / 9.74 ) = 5.2 degrees of pitch
tan-1( ax / az ) = tan-1( -0.8 / -9.7 ) = 4.71 degrees of pitch
These do not agree! This is because the Y (Roll) component az is not accounted for.

If we combine vectors, however:

ayz = sqrt ( ay2 + az2 ) = sqrt ( 0.52 + 9.72 ) = sqrt ( 0.25 + 94.09 ) = sqrt (94.34) = 9.71
sin-1( ax / ayz ) = sin-1( -0.8 / 9.71 ) = 4.72 degrees of pitch

Roll

When working in a 3 dimensional space, simply using sine or cosine as in 2D space is not quite as straight forward.
Example:
sin-1( ay / g ) = sin-1( -0.5 / -9.74 ) = 2.9 degrees of roll
cos-1( az / g ) = cos-1( -9.7 / -9.74 ) = 5.2 degrees of roll
tan-1( ay / az ) = tan-1( -0.5 / -9.7 ) = 2.9 degrees of roll
These do not agree! This is because the X (Pitch) component az is not accounted for.

If we combine vectors, however:

axz = sqrt ( ax2 + az2 ) = sqrt ( 0.82 + 9.72 ) = sqrt ( 0.64 + 94.09 ) = sqrt (94.73) = 9.73
sin-1( ay / axz ) = sin-1( -0.5 / 9.73 ) = 3.12 degrees of roll

Example 6: Moderate Pitch and Roll

Let us presume the following:
accelerometerX = -2.8 m/s2
accelerometery = -2.3 m/s2
accelerometerZ = -9.0 m/s2

First, we validate the data:
sqrt ( -2.82 + 2.32 + -9.02 )
sqrt ( 7.84 + 5.29 + 81)
sqrt (94.13) = 9.70 m/s2

Pitch

When working in a 3 dimensional space, simply using sine or cosine as in 2D space is not quite as straight forward.
Example:
sin-1( ax / g) = sin-1( -2.8 / -9.70 ) = 16.7 degrees of pitch up
cos-1( az / g ) = cos-1( -9.0 / -9.70 ) = 21.9 degrees of pitch up
tan-1( ax / az ) = tan-1( -2.8 / -9.0 ) = 17.28 degrees of pitch
These do not agree! This is because the Y (Roll) component az is not accounted for.

If we combine vectors, however:

ayz = sqrt ( ay2 + az2 ) = sqrt ( 2.32 + 9.02 ) = sqrt ( 5.29 + 81 ) = sqrt (86.29) = 9.728
sin-1( ax / ayz ) = sin-1( -2.8 / 9.28 ) = 17.5 degrees of pitch

Roll

When working in a 3 dimensional space, simply using sine or cosine as in 2D space is not quite as straight forward.
Example:
sin-1( ay / g) = sin-1( -2.3 / -9.70 ) = 13.71 degrees of roll
cos-1( az / g ) = cos-1( -9.0 / -9.70 ) = 23.8 degrees of roll
tan-1( ay / az ) = tan-1( -2.3 / -9.0 ) = 14.33 degrees of roll
These do not agree! This is because the X (Pitch) component of az is not accounted for.

If we combine vectors, however:

axz = sqrt ( ax2 + az2 ) = sqrt ( 2.82 + 9.02 ) = sqrt ( 0.64 + 94.09 ) = sqrt (94.73) = 9.73
sin-1( ay / axz ) = sin-1( -2.3 / 9.73 ) = 13.67 degrees of roll

Example 7: Considerable Pitch and Roll

Example 7 drawing

Let us presume the following:
accelerometerX = -6.0 m/s2
accelerometery = -4.0 m/s2
accelerometerZ = -6.7 m/s2

First, we validate the data:
sqrt ( -6.02 + -4.02 + -6.72 )
sqrt ( 36 + 16 + 44.9)
sqrt (96.89) = 9.84 m/s2

Pitch

When working in a 3 dimensional space, simply using sine or cosine as in 2D space is not quite as straight forward.
Example:
sin-1( ax / g) = sin-1( -6.0 / -9.84 ) = 37.57 degrees of pitch up
cos-1( az / g ) = cos-1( -6.7 / -9.84 ) = 47.08 degrees of pitch up
tan-1( ax / az ) = tan-1( -6.0 / -6.7 ) = 41.84 degrees of pitch
These do not agree! This is because the Y (Roll) component az is not accounted for.

If we combine vectors, however:

ayz = sqrt ( ay2 + az2 ) = sqrt ( 4.02 + 6.72 ) = sqrt ( 16 + 44.9 ) = sqrt (60.9) = 7.80
sin-1( ax / ayz ) = sin-1( -6.0 / 7.8 ) = 50.28 degrees of pitch

Roll

When working in a 3 dimensional space, simply using sine or cosine as in 2D space is not quite as straight forward.
Example:
sin-1( ay / g) = sin-1( -4.0 / -9.84 ) = 23.98 degrees of roll
cos-1( az / g ) = cos-1( -6.7 / -9.84 ) = 47.08 degrees of roll
tan-1( ay / az ) = tan-1( -4.0 / -6.7 ) = 30.83 degrees of roll
These do not agree! This is because the X (Pitch) component of az is not accounted for.

If we combine vectors, however:

axz = sqrt ( ax2 + az2 ) = sqrt ( 6.02 + 6.72 ) = sqrt ( 36 + 44.9 ) = sqrt (80.9) = 8.99
sin-1( ay / axz ) = sin-1( -4.0 / 8.99 ) = 26.4 degrees of roll


Links

https://www.analog.com/en/resources/app-notes/an-1057.html

https://www.analog.com/media/en/analog-dialogue/volume-52/number-1/how-to-improve-the-accuracy-of-inclination-measurement-using-an-accelerometer.pdf

https://www.st.com/resource/en/design_tip/dt0140-tilt-computation-using-accelerometer-data-for-inclinometer-applications-stmicroelectronics.pdf

https://www.nxp.com/docs/en/application-note/AN3107.pdf