AN 1005 – Converting Tilt Angle to Degrees

Converting Tilt Angle to Degrees


This document explains how to convert the raw output from an electrolytic tilt sensor into degrees.

Linear Range and Operating Range of Tilt Sensors

An electrolytic tilt sensor’s measurement range can be described in two ways: linear range and operating range. Operating range is the range over which the sensor will give a monotonic output, but otherwise has no defined characteristics (it is non-linear). Linear range is the portion of the operating range where the output is linear to within a certain percentage. Figure 1 shows the difference between the two ranges.

Figure 1 Linear Range (left) and Operating Range (right).

Basic Derivation of Tilt Angle

To find the tilt angle within the linear range, it is necessary to find a linear function that matches the output of the sensor. The slope of this function is the number of counts per degree, which can be found with the following formula:

Note that the total linear range has to include both positive and negative parts of the range.

Once we know the counts per degree for our sensor, we can convert the raw output to an angle with this formula:

Let’s say we have a 16-bit (0 to 65535) raw output of 58303 from a ±20° sensor. We can find the total possible counts and total range as follows:

16 bits = 216 – 1 = 65535 counts range

±20° = 40° range

We can use these to calculate counts per degree:

We center this around zero by subtracting half of the range. Then we can divide by the counts per degree to find the angle:

Note that the counts per degree can be found for other raw output units when applicable. For example, it may be easier to use volts per degree when using a sensor with an analog output.

This method will assume that the sensor’s output will be centered, symmetrical, and cover the entire output range. This is not the case with most sensors, so this method is generally inaccurate.

Linear Approximation

All sensors have mechanical tolerances and their operating specifications often only indicate a portion of their total range. This causes significant inaccuracies in the previous conversion method. Therefore, it is necessary to make your calculations specific to a particular sensor. This can be done using a linear approximation.

A linear approximation follows the same procedure as the previous method shown. However, rather than using the entire output range to calculate the total possible counts, you use only the counts that are known to be in the sen-sor’s linear range. This requires taking measurements at the ends of the linear range, providing a much more precise counts per degree value.

For example, let us consider a single axis electrolytic sensor with a linear range of ±25° with a 12-bit (0 to 4095 counts) raw output. This time, we will individually characterize the sensor. To do this, we will first take measurements at each end of the linear range of the sensor:

X Axis Tilt at -25° = 400

X Axis Tilt at +25° = 3500

Now that we know the measurements at the edge of the sensor’s range, we can use this to calculate a new counts per degree value:

3500 – 400 = 3100 counts range

+25° = 50° range

This new counts per degree value can then be used in the same way as in the initial example to calculate angles.

This method will provide an accurate measurement with very little initial setup. However, even within the linear range, tilt sensors in general are not perfectly linear. This leads to some error in measurements, especially outside of the linear range. For this reason, applications with higher accuracy requirements should use one of the methods below.

Multi-Point Interpolation

The multi-point interpolation method uses samples taken ahead of time to derive the measurement in degrees. This method’s accuracy will depend on the number and distribution of the samples; however, even taking 3 samples generally provides a better result than a linear approximation.

Sample distribution is one of the most important factors to reaching an accurate conversion. Conversions close to samples will be more accurate. Because of this, samples taken closer together will result in more accurate conversions. These samples should be taken across the entire range of the sensor. Depending on the application, they can be evenly dispersed evenly across the entire range or focused in one region where precision is necessary.

To convert a raw value to degrees, identify two reference samples. These are the two samples that are closest to the measurement in each direction. Then, find a linear approximation between the two samples and map the raw output onto this approximation. The point on this line will give the sensor’s tilt in degrees. The graph in figure 2 shows a visualization of this concept.

Figure 2 Graph displaying samples, reference samples, and the measurement.

The formula for this conversion is shown below.

Let’s say we have a raw measurement of 56412 from a tilt sensor with a ±60° operating range. Also, suppose we already took samples at 5° intervals to use for the conversion. By comparing 56412 with the samples, we determine our two reference points will be at 50° (54213) and 55° (57878). We can then use the formula to calculate the angle:

Therefore, our final measurement is 53°.

This method can be used to convert measurements from anywhere within the sensor’s operating range to degrees. It will also achieve a better accuracy than the linear approximation. However, it requires a large number of accurate samples to achieve the best results.

Polynomial Interpolation

Polynomial interpolation can be used to achieve accurate measurements without taking many samples. This method will use samples to find a polynomial equation that will provide an accurate conversion across the operating range of the sensor.

The samples taken will be used to develop a polynomial equation that will describe the sensor output. At a minimum, you will need N-1 samples to derive a degree N polynomial. For example, 7 samples will create a degree 6 polynomial. You can also use more samples to create a lower degree polynomial, but this will generally have lower accuracy than creating a higher degree polynomial with the same points.

Any number of samples can be used, but we recommend taking at least 3 samples. Using 2 samples would work, but will result in a linear approximation. Note that there is little accuracy improvement from polynomials of degree 7 or higher.

While the polynomial can be determined manually, it is much easier to calculate it using software. One simple method is to use Microsoft Excel’s line of best fit; however, this will may not be precise enough for a usable conversion. Wolfram Alpha is a more accurate option, but the equation will still have to be solved manually. The best option is to use a programming library. For example, the method from the NumPy Python library can be used. The code below shows how this can generate a 6th order polynomial from 7 points:

import numpy.polynomial.polynomial
coefficients =
            [56668, 46553, 36140, 32845, 31207, 21913, 9586], # raw values
            [60, 42, 12, 0, -6, -36, -60], # corresponding angle values to the raw values
            6) # order of polynomial
# Output:
# [-7.88427627e+01 2.57631617e-03 -7.71958174e-08 -1.75354175e-13
# 2.17551410e-16 -5.89635260e-21 4.45379575e-26]

Note that when using a function like this, it is important to specify the order properly. For example, 4 samples will generate a 3rd order polynomial. However, if 4 is given as the order, the library will make a random guess to create an equation. This will result in less accurate conversions.

The array returned by are the coefficients of the polynomial. Based on the output above, the equation would be:

Angle = (4.45e-26)x6 – (5.89e-21)x5 + (2.17e-16)x4 – (1.75e-13)x3 – (7.72e-8)x2 + (2.57e-3)x1 – 78.84

This function can then be used to solve unknown angles by plugging in the raw value for x. For example, let’s say we have the function above, and have a raw measurement of 41643. We can find the angle using the following calculations:

Angle = (4.45e-26)(41643)6 – (5.89e-21)(41643)5 + (2.17e-16)(41643)4 – (1.75e-13)(41643)3 – (7.72e-8)(41643)2 + (2.57e-3)(41643)1 – 78.84

Angle = 232.264 – 738.402 + 654.227 – 12.663 – 133.868 + 107.285 – 78.842 = 30.002°

If using the NumPy library shown above, this can be done using the polyval() method.

print(numpy.polynomial.polynomial.polyval(41643, coefficients))
# Output: 30.001695321007

Note that the result of this calculation is very sensitive to any change in input. For example, if the coeffecients above are rounded to 3 significant digits (as written), the result of the equation is 31°. Because of this, it is important to not round any values while calculating angle with this method.