Project 7 – Analog Sensor: Range using Infrared distance sensor

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