Pololu Robotics & Electronics
Menu
My account Comments or questions? About Pololu Contact Ordering information Distributors

Pololu Forum

Analog IR sensor readings to distance in CM Arduino

How can I convert the analog readings obtained form my 5v sensor (https://www.pololu.com/product/2474) to distance in cm? The code I have tried is posted below. I need the conversion code for both 3v and 5v sensor voltage input just for the 5v sensor model. Thanks!

[code] float sensorValue = analogRead(sensor);
// Serial.println(sensorValue);

sensorValue = map(sensorValue, 124, 670, 670, 124);

float voltage = sensorValue * (3.3/1024);
// Serial.println(voltage);

float factor = 5.5;
float distance = pow(voltage, factor);

Serial.println(distance);
return distance;[/code]

To convert the sensor output to cm distance, you need a lookup table or a continuous function, created to fit to the distance response curve of the sensor as illustrated in the sensor data sheet.

Rather than guess the response from the graph, it would probably be easiest to create your own response curve by recording the analog readings for a number of known distances. You can then fit a simple, continuous curve to the data using any of several on-line curve fitting resources, Excel, or other graphing programs. Consider this to be a fun and interesting challenge!

Note: avoid using the Arduino map() function. It doesn’t work for out-of-range values.

[quote=“Jim Remington”]To convert the sensor output to cm distance, you need a lookup table or a continuous function, created to fit to the distance response curve of the sensor as illustrated in the sensor data sheet.

Rather than guess the response from the graph, it would probably be easiest to create your own response curve by recording the analog readings for a number of known distances. You can then fit a simple, continuous curve to the data using any of several on-line curve fitting resources, Excel, or other graphing programs. Consider this to be a fun and interesting challenge!

Note: avoid using the Arduino map() function. It doesn’t work for out-of-range values.[/quote]

Thanks so much for the basis. I knew that there was a word for what I was trying to do (response curve), but now that I know it I’ll actually be able to do this!