Help with IR distance sensor Sharp 2Y0A02

Hi guys! I'm involved in a school project that requires tu misure a distance of an object, i was considering the PING ultrasonic sensor, but i read that is used for far away objects, and i need to measure short distances. Than my teacher came up with a IR distance sensor manufactured by Sharp. Now, the point is how can i get a good measure? If anyone want to help me here is the datasheet.

A simple analogRead isn't good enough. Thank's to everyone who's gonna help or suggest, and sorry 4 my poor english.

Your closest distance will be 7.88 inches. Is this what you need and why is an analogread not good enough?

Hi, Read this :

Thank's for the link airman00. 7.88 inches ar perfect for my project, i only need to understand how to manage the values that i retrieve from the sensor. According to the datasheet there isn't a linear proportion between the values and the distance, but maybe i'm wrong... I need to have an high precision measure ( less than a cm? ).

Record the values that come out for various known distances. Plug them into Excel, and plot them. Use a polynomial trend line, and find one that "fits" pretty good. Copy down the coefficients, and you have a function that you can use to turn the values into a distance.

can you please post the formular, if you found one that fits nicely?

thx sunny


Thanks for the tip about using Excel - it works great!
I used “power” rather than polynomial for the trend line and got an almost perfect fit.

For the Sharp GP2Y0A02YK0F (long range) I came up with . . .

#define VOLTS_PER_UNIT    .0049F        // (.0049 for 10 bit A-D) 
float volts;
float inches;
float cm;

  volts = (float)proxSens * VOLTS_PER_UNIT; // ("proxSens" is from analog read)
  inches = 23.897 * pow(volts,-1.1907); //calc inches using "power" trend line from Excel
  cm = 60.495 * pow(volts,-1.1904);     // same in cm
  if (volts < .2) inches = -1.0;        // out of range

The pow function costs about 900 bytes.
Thanks again.

Sunny, your formula will likely be a little different for your particular sensor.

thx! Thats ok, i din't have a ir sensor right now. ;o)

ciao sunny

Glad it worked for you, BroHogan! I suggested polynomial because they can be of arbitrary order and are good at interpolating, but if a power fits well, then that's definitely simpler. Good luck with the rest of your project!

inches = 23.897 * pow(volts,-1.1907); //calc inches using “power” trend line from Excel
cm = 60.495 * pow(volts,-1.1904); // same in cm

Just got my range sensor, and found these formulas. I was wondering why there is a - although tiny - difference in the exponent, and why 60.495 / 23.897 = 2.531489 while I learned the factor should be 2.54.