This is an old revision of the document!


Infrared distance sensor

Necessary knowledge: [HW] Sensors Module, [HW] lcd, [AVR] Analog-to-digital Converter, [LIB] Analog to Digital Converter, [LIB] Alphanumeric LCD, [LIB] Sensors

Theory

Sharp GP2Y0A21YK

For measuring the distance to an object there are optical sensors using triangulation. Company “Sharp” produces most common infra-red (IR) wavelength using distance sensors which have analogue voltage output. The sensors made by “Sharp” have IR LED equipped with lens, which emits narrow ray of light. After reflecting from the object, the ray will be directed through the second lens on a position-sensible photo detector (PSD). The conductivity of this PSD depends on the position where the ray falls on it. The conductivity is converted to voltage and if the voltage is digitalized using analogue-digital converter of the micro controller, the distance can be calculated. The route of rays which reflected from different distance is presented on the drawing next to the text

The route of the light ray from an IR distance sensor
Diagram of voltage-distance of IR distance sensor

The output of distance sensors by Sharp is inversely proportional, this means that when the distance is growing the output is decreasing (decreasing is gradually slowing). Exact graph of the relation between distance and output is usually on the data-sheet of the sensor. All sensors have their specific measuring range where the measured results are creditable and this range depends of the type of the sensor. Maximum distance measured is restricted by two aspects: the amount of reflected light is decreasing and inability of the PSD registering the small changes of the location of the reflected ray. When measuring objects which are too far, the output remains approximately the same as it is when measuring the objects at the maximum distance. Minimum distance is restricted due to peculiarity of Sharp sensors which is that the output starts to decrease (again) sharply as the distance is at certain point (depending on the model 4-20 cm). This means that to one value of the output corresponds two values of distance. Avoiding that problem is possible by noticing that the object is not too close to the sensor.

Practice

In the home-lab’s set of sensors includes IR distance sensor GP2Y0A21YK by company “Sharp”. Its measuring range is 10 cm – 80 cm. The output voltage of this sensor is, depending on the distance measured, up to 3 V. The distance sensor is connected to the module of sensors. Its output voltage is sent to the channel 0 of the analogue-digital converter of the AVR. On the basis of previous exercises of sensors, it is easy to write a program which measures the output voltage of the distance sensors, but in addition to that, this exercise includes converting this output voltage to distance.

On the datasheet of the GP2Y0A21YK is graph of relation between its output voltage and measured distance. This graph is not a linear one, but the graph of inverse values of output voltage and distance almost is linear and from that is quite easy to find the formula for converting voltage to distance. To find the formula, the points of the same graph must be inserted to some spreadsheet application and then generate a new graph of them. In spreadsheet programs is possible to calculate the trend-line automatically. Next, the graph of GP2Y0A21YK’s corrected output voltage inverse value’s relation to the corrected inverse value of measured distance with linear trend-line is presented. To simplify, the output voltage is already converted to 10 bit +5 V values of analogue-digital converter with comparison voltage.

The graph of linearizing ADC value and distance

As seen on the graph, the trend-line (blue) overlaps quite exactly the points of the graph. Such overlapping is achieved by using the help of the corrective constant. This corrective constant is discovered by using the trial-and-error method – many variables were tested until such was found which made the graph overlap the trend-line the most. This corrective constant of present graph is +2; this means that to all real distances +2 is added. Since so is the graph very similar to the linear trend line a generalization can be made and we may say that the relation between the distance and the voltage is following:

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

where

  • d - distance in centimeters.
  • k - corrective constant (fund using tial-and-error method)
  • ADC - digitalized value of voltage.
  • a - linear member (value is determined by the trend line equation)
  • b - free member(value is determined by the trend line equation)

Distance d can be expressed from the formula:

d = (1 / (a * ADC + B)) - k

Now it is basically possible to calculate the distance by using this formula, but this requires floating-point calculations, because when dividing fractions will occur. Because the micro controller operates using integers, the formula must be simplified and converted to larger ratios. The when dividing the quotient with a linear-member it will look as follows:

d = (1 / a) / (ADC + B / a) - k

When introducing the corrective constant to the formula and the linear-member and the free-member from the trend-line equation also, the formula for calculating the distance will be:

d = 5461 / (ADC - 17) - 2

This formula is calculatable with 16-bit integers and completely suitable to AVR. Before calculating, must be ensured that the value of the ADC is over 17, or dividing with 0 or negative distance may occur.

Following is the function for converting the values of ADC to centimetres, it is written in the library of the home-lab. Linear- and free-member and corrective constant are not stiffly written in to the function, they are feed with the structure object parameters of the IR distance sensor. By holding the parameters in separate constant, it is easy to add new IR distance sensors to the program.

//
// The structure of the parameters of the IR distance sensors
//
typedef const struct
{
	const signed short a;
	const signed short b;
	const signed short k;
}
ir_distance_sensor;
 
//
// The object of the parameters of GP2Y0A21YK sensor.
// 
const ir_distance_sensor GP2Y0A21YK = { 5461, -17, 2 };
 
//
// Converting the values of the IR distance sensor to centimeters
// Returns -1, if the conversion did not succeed
//
signed short ir_distance_calculate_cm(ir_distance_sensor sensor,
	unsigned short adc_value)
{
	if (adc_value + sensor.b <= 0)
	{
		return -1;
	}
 
	return sensor.a / (adc_value + sensor.b) - sensor.k;
}

Teisenduse tegemiseks tuleb välja kutsuda ir_distance_calculate_cm funktsioon, mille esimene parameeter on IR kaugusanduri parameetrite objekt, teine aga ADC väärtus. Funktsioon tagastab arvutatud kauguse sentimeetrites. Väära tehte (ehk siis ebaloomuliku ADC väärtuse) korral tagastab funktsioon -1. IR kaugusanduri ja teisendusfunktsiooni kasutamist demonstreerib järgnev programm. Kasutusel on alfabeetiline LCD ekraan, kus kuvatakse mõõtetulemust. Ebaloomuliku kauguse puhul kuvatakse küsimärki.

//
// Kodulabori IR kaugusmõõdiku näidisprogramm.
// LCD ekraanil kuvatakse mõõdetud kaugus sentimeetrites.
//
#include <stdio.h>
#include <homelab/adc.h>
#include <homelab/delay.h>
#include <homelab/module/sensors.h>
#include <homelab/module/lcd_alpha.h>
 
//
// Põhiprogramm
//
int main(void)
{	
	unsigned short value;
	signed short distance;	
	char text[16];
 
	// LCD ekraani seadistamine
	lcd_alpha_init(LCD_ALPHA_DISP_ON);
 
	// LCD ekraani puhastamine
	lcd_alpha_clear();
 
	// Programmi nimi
	lcd_alpha_write_string("Kaugusandur");
 
	// ADC muunduri seadistamine
	adc_init(ADC_REF_AVCC, ADC_PRESCALE_8);
 
	// Lõputu tsükkel
	while (true)
	{
		// Anduri väljundpinge 4-kordselt ümardatud väärtuse lugemine
		value = adc_get_average_value(0, 4);		
 
		// ADC väärtuse kauguseks ümberarvutamine
		distance = ir_distance_calculate_cm(GP2Y0A21YK, value);
 
		// Kas arvutus õnnestus ?
		if (distance >= 0)
		{			
			// Kauguse tekstiks teisendamine
			sprintf(text, "%d cm   ", distance);
		}
		else
		{		
			// Teadmata kauguse teksti loomine
			sprintf(text, "? cm   ");
		}
 
		// Teksti kuvamine LCD teise rea alguses
		lcd_alpha_goto_xy(0, 1);
		lcd_alpha_write_string(text);
 
		// Paus
		sw_delay_ms(500);
	}
}

Lisamaterjalid

en/examples/sensor/ir_distance.1266150932.txt.gz · Last modified: 2020/07/20 09:00 (external edit)
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0