# Project 7 – Analog Sensor: Range using Infrared distance sensor

PROJECT 7

ANALOG SENSOR: RANGE USING ANALOG DISTANCE SENSOR

 Back to Project 6

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 SENSORSN – GP2Y0A21

### 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.

### ———————————————————————————————————————————————————-

———————————————————————————————————————————————————-
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

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

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.
Vout = 4
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

ATTACHMENT

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

1. can u give example of coding on same sensor but between (4-30cm) SN-GP2D120XJ00F.. using PIC18F452?

2. nite says:

the coding cannot use. it failed, please update new one

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

4. me_zain says:

i am unable to make hex file…please upload hex filr i really need it.

5. rakimi says:

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

tq

6. Rakimi,

Please discuss in our technical forum.

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

8. rexaze says:

can i also have the code for 16f877a TQ

9. can you give code in micro c program

10. #include
#use delay(clock=4000000)
unsigned char ma7D[10]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
unsigned long Vout;
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);
}
}
{
delay_us(20); // delay 20 us
}
void main()
{
set_tris_A(0xFF);
set_tris_B(0x00);
set_tris_C(0x00);
while(1)
{
range_an=(Vout-19000)/2099;
value=1000/range_an;
HIENTHI();
}
}

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