Converting IR value to distance

So I have a SHARP GP2Y0A02YK0F IR sensor and I was wondering how to go about converting the value given into actual measurement of distance given the odd curve of the output value charts. I'm using just a standard Arduino UNO for the programming and for the moment a single sensor to just merely tell distance.

Currently this is my code:

// Sets up IR Sensor
int pinIR = A0 ;

// For convertToDistance()
double sensorValue = 0.0 ;
double distanceFromIR = 0.0 ;

void setup()
{
// Sets up IR Sensor
Serial.begin( 9600 );
}

void loop()
{
// For convertToDistance()
sensorValue = analogRead( pinIR ); // Reading from sensor
convertToDistance();
Serial.print( "Distance: " );
Serial.println( distanceFromIR );
delay( 600 );
}

// Converts the data read out by the sensor to a measurement of distance
void convertToDistance()
{
sensorValue = .0049;
Serial.print( "Value: " );
Serial.println( sensorValue );
distanceFromIR = 3
pow ( ( sensorValue - 3 ) , 4 ) + 15; // Output on datasheet shows it not to be linear so a converstion using 3* the power of something was needed in order to get a closer to exact answer
// Current equation assuming that its sitting on top of the robot spinning and will always maintain a distance of AT LEAST 15 inches from the floor
}

I'm assuming a maintained distance of at least 15 inches at all times, how ever I'm still getting an error of anywhere from 2-5 inches. Also I'd like to find a formula that would help me encompass below 15 inches as well.

  1. You are reading an analog voltage value so the Arduino will be quite sensitive to the voltage you supply it unless you are using a Aref voltage (which is not in your code). Are you sure you have stable voltage (5v from your conversion factor) power supply? Actually measure the voltage using a voltmeter of you are not sure. My experience with the supply through the USB connector has not been good, varying from 4.2 to 5V.

  2. An alternative approach may be to use a table of values (or a case statement) indexed on the voltage returned by the and interpolate between the two closest values in the table supplied in the data sheet. Over a short span of the graph you can probably assume a linear interpolation will be accurate enough.

As you've already taken all of the "analogRead vs. distance" readings and plugged them into your spreadsheet to graph them, just use your spreadsheet program's method of creating a line of best fit to those values.

Excel's function is "Add Trendline" and OpenOffice Calc calls this "Line of Best Fit" and either option will be found within the Chart menu. When either spreadsheet calculates that line it will provide you with the formula you need to plug into your Arduino sketch.

I've gone ahead and gathered 17 reading for every two inches up to 30 inches, and calculated a trendline for them. Is this in anyway over kill? Also I was thinking about tossing in a low pass filter, either via software version [IIR single pole low pass filter] or via an added mini circuit, to cut back on the noise. Am I just being paranoid with adding the low pass filter?

Have you looked at the datasheet? It has a curve of values and distances for the sensor.

The thing is though its seeming like the chart in the data sheet isn't entirely accurate. At least the more I keep taking my own values to plot in a trendline.

Are you absolutely sure that your 5V supply is 5V. You are using that 0.0049 conversion factor assuming it is exactly 5V (5/1024 = 0.0049). I have had this problem before and when I fixed the power it was all working fine - to the spec in the datasheet.

I can double check but it should be.