Saturday, December 24, 2016

Distance Measurement Using Sharp IR Sensor

In one of our previous post, on UltraSonic Sensors we have already seen how to measure the distance using them and they are quite good, but we always keep looking for alternatives and now we found IR Sensors from Sharp which are capable of measuring the distance.
So, in this post we will see how to use Sharp IR Sensor for measuring distance using Arduino. Different models of Sharp IR Sensors based on output type and range are available in the market. Some of the Sharp IR Sensor having analog voltage as output and 5V supply are as follow:
  • GP2Y0A51 ( 2cm - 15cm Range)
  • GP2Y0A51 ( 4cm - 30cm Range)
  • GP2Y0A21 (10cm - 80cm Range)
  • GP2Y0A02 (20cm -150cm Range)
In this post i am going to use GP2Y0A21 Sharp IR Sensor which is having range from 10cm to 80cm.
Pin Out
The above figure shows the Sharp IR sensor pin out, the first pin is Vout, the second pin is Ground and third pin is Supply (5V). When the obstacle distance increases the Output Voltage of Sharp IR Sensor decreases as shown in the image below.
Voltage-Distance Relation
From the above graph it looks obvious that the output of this sensor is not linear so the question arises that how we are going to convert the sense voltage into distance, so for this i have two method.
  • Look-Up Table Method: Make a look up table in the program memory of micro-controller, as per the data points given in the graph of the datasheet.
  • Polynomial Equation: Determine the approximate polynomial equation to convert voltage into distance.
One other simple method is to use SharpIR Arduino Library and forget all the pain, they had done all these things for you, but i am not going to use the library and will use the second method of Polynomial Equation to convert the sensed voltage into distance.
Now the question arises what is the Polynomial Equation? So for this we will use MATLAB Curve Fitting Toolbox to evaluate which Polynomial Equation approximately represent the graph.
Take some point from the graph given in the Datasheet and use MATLAB Curve Fitting Toolbox as shown below.
Generate Polynomial Equation using MATLAB Curve Fitting Toolbox
As seen clearly from the above image, order 4 Polynomial Equation approximately represent the points taken from the datasheet.

The Polynomial Equation is as follow:

f(x) = p1*x^4 + p2*x^3 + p3*x^2 + p4*x + p5
Constants:
p1 = 8.075

p2 = -68.44
p3 = 210.4
p4 = -285.2
p5 = 161.3

We will use this equation in our Arduino Program to evaluate distance from the voltage value.
The complete code is as follow (Sharp IR Output is connected to A0 analog pin:
 /*  
  * This Program uses SHARP IR Sensor 2Y0A21 to Calculate the Distance of the Obstacle  
  */  
 const int sensePin = A0; // Sharp IR Sensor Pin  
 void setup()  
 {  
  // Initialize Serial Port  
  Serial.begin(115200);  
 }  
 void loop()   
 {  
  float distance = 0;  
  distance = (float)readSharpSensor();  
  Serial.print("Obstacle Distance: ");  
  Serial.print(distance);  
  Serial.println(" cm");  
 }  
 /*  
  * This function returns the distance calculated by SharpIR Sensor  
  */  
 double readSharpSensor( void )  
 {  
  int noSamples=100;  
  uint32_t sumOfSamples=0;  
  uint32_t senseValue=0;  
  double senseDistance=0;  
  for (int i=0; i<noSamples; i++)  
  {  
   delay(40);  
   senseValue=analogRead(sensePin); //Perform analogRead  
   sumOfSamples=sumOfSamples+senseValue; //Running sum of sensed distances  
  }  
 // Serial.print("Sum of Samples: ");  
 // Serial.println(sumOfSamples);  
  senseValue=sumOfSamples/noSamples; //Calculate mean  
  senseDistance=senseValue; //Convert to double   
  senseDistance=mapDouble(senseDistance,0.0,1023.0,0.0,5.0); //Convert analog pin reading to voltage  
 // Serial.print("Voltage: ");  
 // Serial.print(senseDistance);  
 // Linear model Poly4:  
 //   f(x) = p1*x^4 + p2*x^3 + p3*x^2 + p4*x + p5  
 // Coefficients (with 95% confidence bounds):  
 //    p1 =    8.075 (3.718, 12.43)  
 //    p2 =   -68.44 (-99.23, -37.65)  
 //    p3 =    210.4 (136.7, 284.1)  
 //    p4 =   -285.2 (-353.5, -216.9)  
 //    p5 =    161.3 (141.4, 181.2)  
  senseDistance = 8.075*pow(senseDistance,4) - 68.44*pow(senseDistance,3) + 210.4*pow(senseDistance,2) -  
          285.2*(senseDistance) + 161.3;  
  //Print to Serial - Debug  
 // Serial.print(" Distance: ");  
 // Serial.println(senseDistance);  
  return senseDistance;  
 }  
 /*  
  * Re-maps a number from one range to another.   
  * That is, a value of fromLow would get mapped to toLow, a value of fromHigh to toHigh,   
  * values in-between to values in-between, etc.  
  */  
 double mapDouble(double x, double in_min, double in_max, double out_min, double out_max)  
 {  
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;  
 }  

The Output of the program is as follow:

The same method can be used for other SharpIR Sensors. Please have a look at this video for complete description.


2 comments:

  1. While i just had become within your online site while sporting specified recognition comfortably a lot of touch submits. Amiable technique for outlook, I will be book-marking at present need devices gain spgs best suited all the way up. distance calculator UK

    ReplyDelete