Sharp Analog Distance Sensor

Sharp Analog Distance Sensor has many type or maximum distance capability. In this page, two type of sharp analog distance sensor will be roughly discuss:
  1. Sharp Analog Distance Sensor 10cm to 80cm - GP2Y0A21YK0F
  2. Sharp Analog Distance Sensor 20cm to 150cm - GP2Y0A02YK0F
The graph - Voltage VS Distance

Both the datasheet give the Voltage VS Distance graph for user to use it. Sharp analog distance sensor output analog voltage in respond to the distance detect by the sensor.
 Sharp Analog Distance Sensor 10cm to 80cm
 Sharp Analog Distance Sensor 20cm to 150cm

 Excel Graph - Distance VS Voltage

In order to implement it in microcontroller, user need to export above picture to excel and make graph. From the graph created by excel, do extract the formula of the graph.
  Sharp Analog Distance Sensor 10cm to 80cm
 Sharp Analog Distance Sensor 20cm to 150cm

The formula
  1. Sharp Sensor 10cm to 80cm : distance = (26.835 x Voltage) to the power of (-1.185)
  2. Sharp Sensor 20cm to 150cm : distance = (61.362 x Voltage) to the power of (-1.12) 
Implementation into microcontroller -The Coding

void analog_distance_sensor(char adc_channel, int lcd_address, int maxDistance_80_150);

///////////////////////////////////////////////////////
// Analog Distance Sensor 80cm and 150cm
///////////////////////////////////////////////////////
void analog_distance_sensor(char adc_channel, int lcd_address, int maxDistance_80_150)
{
    lcd_goto(lcd_address + 0x04);
    lcd_printf("cm");

    float voltage = 0;
    float distance = 0;  

    voltage = (float)adc_run(adc_channel) * (5.0/4096.0);
    if(maxDistance_80_150 == 80)
    {
        if(voltage > 2.3)
        {
            lcd_goto(lcd_address);
            lcd_printf("<010");
        }
        else if(voltage < 0.4)
        {
            lcd_goto(lcd_address);
            lcd_printf(">080");
        }
        else
        {
            distance = 26.835 * pow(voltage,-1.185);
            lcd_goto(lcd_address);
            lcd_num(distance,10,4);
        }
    }
    else if(maxDistance_80_150 == 150)
    {
        if(voltage > 2.5)
        {
            lcd_goto(lcd_address);
            lcd_printf("<020");
        }
        else if(voltage < 0.45)
        {
            lcd_goto(lcd_address);
            lcd_printf(">150");
        }
        else
        {
            distance = 61.362 * pow(voltage,-1.12);
            lcd_goto(lcd_address);
            lcd_num(distance,10,4);
        }
    }
}

No comments:

Post a Comment