# 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.

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

where $R$ represents the distance between the obstacle and the sensor (expressed in cm) while $V_{a2d}$ 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 ($V_{a2d}$ ).

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 $V_{a2d}$ values.

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

where $k$ is a parameter we have to choose empirically: let’s say $k=0.5$.

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 $V_{a2d}$ values correctly on the x-axis (see the appendix below on how to do this in Libre Office).

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

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 $\frac{1}{(R+k)}$ instead of $y$ and $V_{a2d}$ in place of $x$. Our equation now becomes:

Let’s obtain $R$ from it:

• $m' = \frac{1}{m}$;
• $q' = \frac{q}{m}$;

the equation eventually becomes:

where, to sum up, starting with $k = 11$, $m = 0,0000603553$ and $q = 0,0015304544$, 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 $V_{a2d}$ 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

• 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;

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!