Sharp IR distance sensor linearization

It is decisely easier to linearize the IR SHARP GP2Y0A21YK than any other SHARP IR distance sensor simply because this one is used as an example inside this technical document by Acroname and all the matemathical process is very well explained there.

Sharp

Here’s the formula for the linearization of the sensor data:

where represents the distance between the obstacle and the sensor (expressed in cm) while is the corresponding 10bit ADC voltage value as sampled by the microcontroller.

Different sensors? different equations!

Now if we consider a different Sharp IR distance sensor we have to calculate that formula ourself. Take the GP2Y0A02YK0F for example:

Sharp

this sensor is capable of detecting obstacles in a range between 20 cm and 150 cm (as we know from the sensor datasheet).

So in order to obtain the formula we first need to compile a table where we have:

here’s the table I’ve compiled

R (cm) A2D
20 498
25 442
30 388
35 340
40 304
45 270
50 246
55 222
60 203
65 186
70 174
75 162
80 154
85 145
90 137
95 130
100 125
105 117
110 113
115 107
120 103
125 98
130 95
135 90
140 87
145 84
150 80

Now that we have collected all that data let’s plot them in order to get the big picture (I’m using LibreOffice Calc to do this, it will be very useful for all the upcoming calculation we need)

graph 1

As we can see there’s no linear relationship between distance and the 10bit A2D values; in other terms, equals variations in distance doesn’t correspond to equal variations in measured values.


Now let’s plot another graph. This time we place values on the x-axis and for the y-axis we use the following formula:

where is a parameter we have to choose empirically: let’s say .

This division operation acts as a “linearizing” function which helps to make the curve looking more like a straight line.

In order to see the graph like this we need to place our values correctly on the x-axis (see the appendix below on how to do this in Libre Office).

graph 2

If we choose accurately we can make the graph even more similar to a straight line: here’s how it looks for

graph 3

Now is time to calculate the regression line for these data in order to find out m and q, respectively the slope and the intercept of the line equation

Once again LibreOffice offers a tool to do this. See the appendix below for more about it.

graph 4

Going on with our reasoning, we can make some substitution placing instead of and in place of . Our equation now becomes:

Let’s obtain from it:

Making an helpful substitution with:

the equation eventually becomes:

where, to sum up, starting with , and , we get (approximately):

This way we have obtained a tailored linearization formula for the GP2Y0A02YK0F sensor:

-Nick

Appendix

The data from the sensor have been elaborated usign LibreOffice Calc. You can download the .ods file from here.

Probably the trickiest part of all this job has been to introduce the data inside the Calc document and to fit them inside graphs in order to show them correctly and to compute the regression line.

Here’re some of the difficulties I had and the solutions I’ve found (maybe they can be helpful for you as well):

  1. I found difficult to place values on the x-axis in a correct way, here’s the solution: once you have created the graph, click on it in order to focus the x-axis the click on it with the right button and select Format Axis…. Then, from the Scale tab, from the type menu, select the Date option. This way x-axis data will be correctly displaced on the axis;

  2. How to visualize decimals? In order to visualize a custom number of decimals you need to right click on the cell and select the Format Cells.. option. Form the Numbers tab now select Numbers from the Category column, then increase the value on the Option/Decimal Places field;

  3. Slope and Intercept calculation formulae: these formulae are =SLOPE(Data Y, Data X) and =INTERCEPT(Data Y, Data X) respectively and let you select data ranges for X and Y. You can use the Function Wizard to insert them inside a cell. These formulae are also described from this LibreOffice help article.

References


If you find this article useful and you like it, please leave a comment below: let us know what do you think about it, we'd really appreciate it. Thank you very much and, as always, stay tuned for more to come!