Project 7 – Analog Sensor: Range using Infrared distance sensor

PROJECT 7

ANALOG SENSOR: RANGE USING ANALOG DISTANCE SENSOR

Back to Project 6                                                                                                          Go to Project 8

Analog distance sensor which have a function that same as the Ultrasonic sensor which is use to measure range. But for this sensor, the output value are in centimeter(cm) and also the output are only given an analog value. To get the result, an mathematics formula are required to apply into our C-code for calculation.

———————————————————————————————————————————————————-

COMPONENT NEEDED

———————————————————————————————————————————————————-

16 x 2 LCD Display
 

ANALOG DISTANCE SENSOR

SN – GP2Y0A21

  BREADBOARD
  JUMPER WIRES

———————————————————————————————————————————————————-

CONNECTION

———————————————————————————————————————————————————-
Analog Distance Sensor
Analog distance sensor have 3 pin which is VDD, GND and Vout.

1 – Vout 2 – GND 3 – VCC (5V)

*User must refer to analog distance sensor datasheet before connect power to sensor. Cable’s color are not standard.

By referring to the figure below, the YELLOW colour wire are connected to RA0, RED wire are VDD and BLACK wire are GND.

———————————————————————————————————————————————————-

ADDITIONAL INFORMATION

———————————————————————————————————————————————————-
Referring to the chart above are the output voltage(V) versus distances(cm) of the analog distance sensor. By plotting the output voltage versus inverse number distance (1/cm), we can get the linear relation such as in figure below.

The linear equation is y = 20.99x + 0.19, where y is voltage output and x invert distance. Notice that the trusted output voltage are about 0.4 to 2.8 V.

———————————————————————————————————————————————————-

CODE OVERVIEW

———————————————————————————————————————————————————-

Vout = (adc_value*500000)/1024;
After take the ADC output value for 100 times. The adc_value are time with 500000 and divided by 1024 to eliminate the DOT and get the bigger value. Besides that, we also get the value after the DOT.
E.g : adc_value = 1022,
~ if using adc_value*5/1024,
Vout = 4
~ if using adc_value* 500000/1024
Vout = 499023

if ((Vout > 43945) && (Vout < 279785))
Check if Vout are between 0.4V to 2.8V. If the value is more then less then 0.4 and more then 2.8 are not valid and LCD will display “OUT OF RANGE”. See example below to get the Vout.
E.g-1 : If using Vout = adc_value*5/1024
Vout = 2.8V
2.8V*1024 / 5 = 573. (convert to Decimal)
573*5 / 1024 = 2 (Convert to Volt)
E.g-2 : If using Vout = adc_value*500000/1024
Vout = 2.8V
2.8V*1024 / 5 = 573. (convert to Decimal)
573*500000/1024 = 279785. (Convert to Volt)

range_an = (Vout – 19000)/2099;
Refer example below on how to get the range in centimeter(cm).
E.g-1 : if Vout = 279785
279785 – 19000 = 260785
260785 / 2099 = 124
1000 / 124 = 8

Back to Project 6                                                                                                       Go to Project 8

———————————————————————————————————————————————————-

ATTACHMENT

1. P7 User Manual.pdf
2. Project_7_code.zip

Buy

, , , ,

Related Post

Control and Monitor via SMS Part 2

Simple steps to control Stepper Motor using 2Amp Motor Driver Shield and CIKU

PWM from CT-UNO+ MDDS10

PIC16F VS PIC18F:Analysis of PIC Mid-Range 8 bit family and Advanced 8-bits Family

10 thoughts on “Project 7 – Analog Sensor: Range using Infrared distance sensor

  1. The sample code is being compiled and loading to actual hardware. You might need to check the compiler version and the microcontroller.

  2. This file is written for PIC16F887, if i use 16f877a model of PIC , which parts in the code need to modify…

    tq

  3. sorry to interrupt…i’am just a beginner
    I’m using this sensor n pic16f877a for my project…there is an error ANS0, ADFM, VCFG1, FCFG0 because this code is for pic16f887…
    can u give the code for pic16f877a ?? TQ

  4. #include
    #use delay(clock=4000000)
    unsigned char ma7D[10]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
    unsigned long Vout;
    unsigned long adc_value=0;
    unsigned long range_an=0;
    int32 value;
    void HIENTHI()
    {
    int16 i;
    for(i=0;i<10;i++)
    {
    output_C(ma7D[value%10]);output_low(PIN_B4);delay_ms(10);output_high(PIN_B4);
    output_C(ma7D[value/10]);output_low(PIN_B5);delay_ms(10);output_high(PIN_B5);
    }
    }
    void read_value_ADC()
    {
    set_ADC_channel(0);
    delay_us(20); // delay 20 us
    adc_value=read_adc();
    Vout=(adc_value*500000)/1024;
    }
    void main()
    {
    set_tris_A(0xFF);
    set_tris_B(0x00);
    set_tris_C(0x00);
    setup_adc(ADC_CLOCK_INTERNAL);
    setup_adc_ports(PIN_A0);
    while(1)
    {
    read_value_ADC();
    range_an=(Vout-19000)/2099;
    value=1000/range_an;
    HIENTHI();
    }
    }

    Help me
    Code does not simulate to display LED 7 segment anode common

Leave a Reply

Your email address will not be published. Required fields are marked *