Tuesday, August 19, 2014

How to work with Sharp Infrared sensor

I already wrote how to work with ADC In this article.



( In the simplest case ) Infrared Proximity Sensor - Sharp has 3 output pins: Vcc - Power, GND - Ground and Vo - Data. Depending on the distance varies the voltage at the output Vo, all that left is transform that data to digital information.






These sensors have infrared LED with lense, which emits narrow light beam. Then this light is reflected from objects and returns to the sensor. Distance to the object is determined by the Angle at which sensor receives the beam.

Sensor receives light on Position-sensitive diode (PSD), it means that resistance of that element is depends on the position of the beam.

That makes it possible to compute distance between sensor and object.




Some sensors have digital output, but I had GP2D12 which have analog output, so i need to use ADC to transform that in to digital value.

On the left we can see average diagram of voltage-distance of IR distance sensor, it is clearly not linear, in addition there is a peak at about 8 cm, so we need to be certain that distance is more than that. (If we don't want to measure less than 8 cm of course).












Moreover there is diagramm of reciprocal of the distance (1 / cm) and it is almost linear until 8 cm. We can use that to compute our distance.

For that we need to get output sensor voltage, convert that in digital value and transform it into distance

We need to get formula to transform voltage into distance, for that i created a line plot in MathCad, that was approximately the same as in our diagramm.

Now we need to get this line formula

And we get something like this:

Line formula is:

1 / (d + k) = a ⋅ ADC + b

d - distance in cm
k - constant (from datasheet)
ADC - ADC value
a,b - variables (we need to compute them from our line)

Now we can get distance formula

d = (1 / (a ⋅ ADC + b)) – k

We can use that, but it is better to work with integrerls than floats, so we change this formula into:

d = (1 / a) / (ADC + b / a) – k

Sharp GP2D12 final formula looks like that:

d = (6787 / (ADC - 3)) - 4

We need to always check our ADC value, because it must be > 3, or we will be dividing by 0.

This code I used for my robot, I don't need to compute exact distance, I just need to know ifsomething is in front of sensor.

#define F_CPU 8000000UL

#include <avr/io.h>
#include <util/delay.h>

void check_sensors()
{
        if(ADCSRA & 1<<ADSC) // If ADC calculations ended
        {
                unsigned int ADCdata;
                ADCdata = ADCW; // In ADCW our voltage is stored
 
                if(ADCdata <= 615) // rough measurement 13 cm is 2.5 V GP2D12
                {
                        // Do stuff
                }
  
                ADCSRA |= (1<<ADSC); // Start ADC conversion
        }


int main()
{ 
        ADMUX = 0x00; // Get voltage from PA0 pin
        ADCSRA |= (1<<ADEN)|(1<<ADSC)|(1<<ADIE)|(1<<ADPS2)|(1<<ADPS1); // configure ADC

        while(1)
        {
               if(ADCSRA & 1<<ADSC) // OR we can check here if ADC have finished working
               {
                       check_sensors();
               }
        }

}

// Moreover we can use interrupts after ADC will finish calculating voltage
ISR(ADC_vect)
{    
        ADCdata = ADCW; 
        voltage_0 = ADCdata * 48875 / 10000; // ( 5000 mV / 1023 ) = 4.8875 ( mV in 1 bit )
        voltage   = voltage_0 % 10000 / 1000;  
        voltage_2 = voltage_0 % 1000 / 100;
        voltage_3 = voltage_0 % 100 / 10;
        voltage_4 = voltage_0 % 10;

        if (ADCdata > 3) // Don't dividy by 0
        {
                range = (6787 / (ADCdata - 3)) - 4;
        }


        ADCSRA = ADCSRA | 0x40;// Start ADC again 0b01000000
}


No comments :

Post a Comment