IR Sensor Question

Okay, trying to calibrate the 2D120x IR sensor (3cm-40cm) and need help...

I used the data from the graph on page 9 of the datasheet (http://www.sparkfun.com/datasheets/Sensors/Infrared/GP2D120XJ00F_SS.pdf) to reproduce the same graph in Excel.

I then used a trendline to fit the points and and came up with a polynomial equation: f(x)=polynomial, where f(x)=distance and x=reading(volts). Here's my code (PS the trendline almost fits exact)


int IRpin = 7; // analog pin for reading the IR sensor
void setup() {
Serial.begin(9600); // start the serial port
}

void loop() {
float volts = analogRead(IRpin)*.0048828125;
// value from sensor * (5/1024) for ea. step

float distance = -(3.078pow(volts,5))+(29.645pow(volts,4))-(110.68pow(volts,3))+(201.94(volts,2))-(186.84*pow(volts,1))+81.524;
// calibrated using 5th degree polynomial

Serial.println(distance); // print the distance
delay(100); // arbitrary wait time.
}


I'm getting numbers in the 100-500 range and can't understand why it won't give me distance in cm if I directly calculated the distance using the voltage output.

I also noticed that with different surfaces I got dramatically different readings. I need a sensor to be able to accurately give close proximity readings without having to worry about surface differences. Can anyone help, and is this the best sensor for the job, or is ultrasonic better?

I'm fairly new at this, so i apologize for sounding ignorant to this problem. For this part of my project, I need to accurately measure the distance from an object. I've seen projects where people take the distance output and create a 360 degree schematic of the surrounding environment. I want to take it a step further and make a 3D environment.

I have a known origin that acts as my constant, and I want to use the IR sensor to measure the distance from the object so I can calculate the distance from the origin - ultimately using those numbers to create an object in a 3D cad program. Everything I will be measuring is no more then 5 inches away.

When you ask for RAW data are you asking or the Serial Monitor output or actual measurements compared to the serial output data...

In the meantime I will compare my results with the datasheet, keeping in mind that you're right and that the datasheet can be somewhat misleading.

Does this explination of the project help?

Thanks Rich,
Justin.

Infrared distance measurement is quite sensitive to surface reflectivity. Some things just do not reflect IR well (cloth-type surfaces for example).

I'm also not really understanding your question. You say your trendline fits the data perfectly but your results are unexpected.

Have you tried using double instead of float?

--
Check out our new shield: http://www.ruggedcircuits.com/html/gadget_shield.html

Right, its a fifth degree polynomial that fits the curve of the line (ignoring the first few "jumpy" points). I know the equation is pretty accurate (assuming the datasheet numbers are accurate). I've been having to do this graph stuff in school now for years. Which makes me believe that the error is in how I coded it or this part:

void loop() {
float volts = analogRead(IRpin)*.0048828125; // (5/1024) per step

My setup is pretty basic- simply
IR red-> 5V
IR yellow -> Analog 7
IR Black -> ground

I've tried using double and got the same results. I'm using paper as the surface.

You're right Rich, it would be dumb to reinvent. I've looked around the WWW for a couple days now and find similar results, none of which are anything close to my goal with this IR sensor. I'll keep looking. I want to thank you guys for replying. Simply reading your responses helps me understand different thought processes.

I'll keep searching.

When i read the volt output, it matches up with my+the datasheet data. meaning that my mistake isn't where i mentioned above, its how im coding the polynomial.

Hmmm...OK, what polynomial did you come up with? Maybe if we see that we can spot the mistake in its implementation.

--
Check out our new shield: http://www.ruggedcircuits.com/html/gadget_shield.html

Rugged, Richard,
I found the mistake, and got it to work. If you notice below at the original code I posted, (201.94*(volts,2)) should read (201.94*pow(volts,2)). I fixed that and it gives pretty accurate reading.


float distance = -(3.078pow(volts,5))+(29.645pow(volts,4))-(110.68pow(volts,3))+(201.94(volts,2))-(186.84*pow(volts,1))+81.524;

// calibrated using 5th degree polynomial


I have 3 more of these same sensors I plan on using in conjunction with this one. I will take the data from the 4 different IR sensors, and average them together for a finial reading. Sounds like a lot, and I might only need 2 sensors to get an accurate reading, but can I connect 4 of these sensors to a single Mega board and have them work together?

(186.84*pow(volts,1))

pow is pretty expensive (resource and time wise) to be wasting a call like this.

You're right Paul, I actually caught that mistake before I realized I forgot "pow*" in the operation before that. Thanks though, I did not know that it took up a lot of resources - i just simply fixed it because it was un-needed.