# Sharp IR Sensor Algorithms

#### erco

##### Senior Member
I like those Sharp IR distance sensors, even if their analog output is non-linear. I suspect most of us use these either with lookup tables or in non-precision applications where rough ranges are acceptable. But this morning I found some hardcore equations at https://www.upgradeindustries.com/product/58/Sharp-10-80cm-Infrared-Distance-Sensor-(GP2Y0A21YK0F) and https://www.upgradeindustries.com/product/57/Sharp-20-150cm-Infrared-Distance-Sensor-(GP2Y0A02YK0F)

Depending on your ADC resolution and other factors, you might not need floating-point accuracy to 6-7 sig figs.

#### techElder

##### Well-known member
No time to work on it now. For what it's worth:

range (cm) = 306.439 + voltage * (-512.611 + voltage * (382.268 + voltage * (-129.893 + voltage * 16.2537)))

#### stan74

##### Senior Member
v*16.2537-v*v+382.268*v-512.611*v+306.439 in picaxe left to right...or maybe not.
By the time it's worked that out, the robot's crashed.

#### MarkLondon

##### New Member
I have used these sensors and found that there is a simple formula that gives good results.
Distance in cm = 1735/8bitADC - 6
This was derived using the solver in MSExcel to give the best fit to the manufacturer's datasheet calibration.

This calibration has an accuracy of +/- 1cm to the data in the graph of figure #2 published in the datasheet:

www.pololu.com/file/0J85/gp2y0a21yk0f.pdf

e.g.
True_____ Measured _ 8bit_____ Calc _____ Error
distance__ Voltage ___ adc ____ Distance __ Distance
cm ______ Volts _____ # _____ cm ______ cm
80, ______ 0.40, ____ 20, ____ 80, ______ 0
70, ______ 0.45, ____ 23, ____ 69, ______ -1
60, ______ 0.50, ____ 26, ____ 60, ______ 0
50, ______ 0.60, ____ 31, ____ 49, ______ -1
40, ______ 0.75, ____ 38, ____ 39, ______ -1
30, ______ 0.90, ____ 46, ____ 31, ______ +1
20, ______ 1.25, ____ 64, ____ 21, ______ +1
15, ______ 1.60, ____ 82, ____ 15, ______ 0
10, ______ 2.10, ___ 107, ____ 10, ______ 0
05, ______ 3.20, ___ 163, ____ 04, ______ -1

Last edited:

#### erco

##### Senior Member
Mark, that's excellent work. Thank you for sharing that formula. Impressive accuacy!

#### stan74

##### Senior Member
a2dval=6050/a2dval
if a2dval>=2 then
a2dval=a2dval-2
end if
if a2dval<10 then
a2dval=10
end if
if a2dval>80 then
a2dval=80
end if
ie 10 bit a-d. Sampling is slower than SRF04 type ultrasonic even allowing 10 ms for settling.
It's easy to get accuracy but the device is too slow updating for a robot with it mounted on a scanning servo.

#### erco

##### Senior Member
It's easy to get accuracy but the device is too slow updating for a robot with it mounted on a scanning servo.
IR sensors play nicer with servos than ultrasonic sensors do. And of course, at under \$4 (and most Picaxes having plenty of ADC channels), why not use several stationary Sharp sensors in an array? These guys went overkill on both ultrasonic and IR sensors:

#### stan74

##### Senior Member
1st Sharp I ordered never arrived so bought 1 from UK supplier. They're over £12 each. I can't remember if it's acquisition time is in the data sheet but it was missing object in front of it on a robot servo. moving your hand in front and monitoring range I can see it's laggy.
As for the robot in your photo,that would miss a chair leg between the sensors,the beam is only 15 degrees. Also the sensors should be vertical as they don't detect straight ahead.
The servo I use scans in steps of 15 and at the ends of scan,starts next scan from + or - 5 so it checks the angles it missed in the previous scan. Also 2 us sensors at 45 degrees on a servo was the best solution in that they look in different directions and the servo has less movement and can move in smaller steps. Waiting 10 mS for each sensor to settle is ok..I don't know about 5 us and 3 IR as in the photo though. Bit OTT

#### hbl2013

##### Senior Member
Just as a remark, why convert the reading at all? Although the actual range can be important in some cases, in many mobile anti-collision applications it is not. It might speed up the process a little which might be helpful if your car moves fast.

#### erco

##### Senior Member
Absolutely right, in many cases a relative number or range works fine, no conversion required.

But when you need calibration, this quick curve fit can help.

Hmmm, almost makes me want to revisit this ultrasonic distance display and calibrate it for IR...