The aim of this post is to explain how to convert voltage from Sharp ranging sensor into an accurate distance. Experiments have been performed with Sharp GP2Y0A02YK distance sensors. The range of this triangulation-based sensors is from 20cm to 150cm.
An accurate measurement experimental campaign has been performed. Range has been measured with a step of 10mm. The following graph show the behavior of the GP2Y0A02YK sensor according to the distance (ADC value vs distance). As it is explained in the sensor’s specifications the sensor is not suited for measurements below 20cm due to ambiguity.
Thanks to the Matlab polyfit fonction, the curv has been approximated with a fourth degree polynomial. The approximation covers the range from 14cm to 150cm. The following figure shows the result:
The approximation for the GP2Y0A02YK is given by the following equation. \(ADC\) is the raw value returned by the Arduino analogRead() function in range from 0 to 1024 (10 bits analog to digital converter).
Based on this result, a C++ function can easily be written in order to compute the distance:
/*!
\brief make a distance measurement with a Sharp GP2Y0A02YK sensor
\return the measured distance in mm
*/
float get_Sharp_GP2Y0A02YK_Distance(int PinID)
{
// Read analog to digital converter value
float ADCValue = (float)analogRead(PinID);
// Convert in millimeters and return distance
return 2583.711122992086
- 20.197897855471 * ADCValue
+ 0.071746539329 * ADCValue * ADCValue
- 0.000115854182 * ADCValue * ADCValue * ADCValue
+ 0.000000068590 * ADCValue * ADCValue * ADCValue * ADCValue;
}
Excel files, images, raw data and Matlab scripts can be downloaded here: