# retrieving distance from sharp IR gp2y0a51sk

Hi guys i’m currently working on a sch proj. Ive been working with this 5th order polynomial for so long but i can’t seem to get the distance (x-axis)
I’m using the sharp IR gp2y0a51sk sensor. I cant find a distance library for it therefore im hoping to come out with an algorithm myself.
Using excel i managed to find the formula representing the graph in picture attached. Can anyone pls help thanks

You have already seen the datasheet for sharp IR gp2y0a51sk ?

There is a distance/voltage table there which may also help to get you started.
The intensity of the reflected light diminishes in proportion to the square of the distance from the sensor.
How you convert the graphical results back into a formula goes back too far into my school days, but you could do a crude program trick as below, with some fixed points and then some linear interpolation between them (not illustrated).

``````// av = voltage

if ( av >= 0.8 && av < 1.0 )  {
distance = 5
}
else if ( av >= 1.0 && av < 1.2 )  {
distance = 4
}
else if ( av >= 1.2 && av < 1.4 )  {
distance = 3
}
``````

You have one problem that for all returned voltages, there are two possible distances.

Edit:
Looking again at the graph you supplied, I now see you’ve managed to get a formula which converts distance into what looks like the output of the sensor.
Clearly you want this the other way round, so that the output of the sensor is converted to distance.
Instead of trying to solve the formula given (Y in terms of X), can’t you get Excel to return X in terms of Y ?
If not, replot the figures reversing the axis so distance is Y and sensor output is X.
Once you have the formula, it should be relatively trivial to convert it into C.
Incidentally, what are the units on the Y axis ?