Sharp IR distance sensor linearization
06 Aug 2017It 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.
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:
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:
 in the first column, the distances  expressed in cm  between the sensor and an obstacle while this one is placed closer and closer to the sensor;
 in the second column, the corresponding 10bit voltage values value as returned from the microcontroller A2D converter ( ).
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)
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 xaxis and for the yaxis 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 xaxis (see the appendix below on how to do this in Libre Office).
If we choose accurately we can make the graph even more similar to a straight line: here’s how it looks for
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.
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):

I found difficult to place values on the xaxis in a correct way, here’s the solution: once you have created the graph, click on it in order to focus the xaxis 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 xaxis data will be correctly displaced on the axis;

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;

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
 Sharp GP2Y0A21YK and GP2Y0A02YK0F datasheets;
 an interesting article about IR sensor linearization by Acroname;
 IR sensor linearization topic on the Arduino forum;
 Here’re two links I found useful to understand how to work with LibreOffice regression line: link 1 and link 2;