This is an old revision of the document!


 

Infrared distance sensor

Theory

Sharp GP2Y0A21YK

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

The route of the light beam from an IR distance sensor

The output of distance sensors by “Sharp” is inversely proportional, which means that when the distance is growing, the output is decreasing (decreasing is gradually slowing). The 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 credible, and this range depends on the type of sensor. The maximum distance measured is restricted by two aspects: the amount of reflected light is decreasing, and the inability of the PSD to register the small changes in the location of the reflected ray. When measuring objects that 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, meaning the output starts to decrease (again) sharply as the distance is at certain point (depending on the model 4-20 cm). This means that for one value of the output, there are two values of distance. This problem can be avoided by noticing that the object is not too close to the sensor.

Practice

Diagram of voltage-distance of IR distance sensor

The HomeLab set of sensors includes an IR distance sensor, SHARP GP2Y0A21YK. The measuring range of the sensor is 10 cm – 80 cm. The output voltage of this sensor is, depending on the distance measured, up to 3 V. The distance sensor can be connected to any ADC (the analogue-digital converter) channel of the HomeLab module. On the basis of previous exercises with sensors, it is easy to write a program that measures the output voltage of the distance sensors, but in addition, this exercise includes converting this output voltage to distance.

On the datasheet of the GP2Y0A21YK is a graph of the relation between its output voltage and measured distance. This graph is not a linear one; however, the graph of inverse values of output voltage and distance almost is, and from that, it is quite easy to find the formula for converting voltage to distance.

// 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;
}

To make the conversion, the function ir_distance_calculate_cm must be engaged. The first parameter of this function is the object of the parameters of the IR distance sensor, and the second is the value of the ADC. The function returns the calculated distance in centimeters. If the operation is wrong (unnatural value of the ADC), the returned value is -1. The following program demonstrates the use of an IR distance sensor and the conversion function. A graphical LCD is used, where measured results are displayed. If the distance is unnatural, “?” is displayed.

// The example program of the IR distance sensor of the HomeLab
// Measured results in centimeters are displayed on the LCD
#include <stdio.h>
#include <homelab/adc.h>
#include <homelab/delay.h>
#include <homelab/module/sensors.h>
#include <homelab/module/lcd_gfx.h>
 
#define ADC_CHANNEL 0
 
// Main program
int main(void)
{	
	signed short value, distance;	
	char text[16];
 
	// Initialization of LCD
	lcd_gfx_init();	
	lcd_gfx_clear();	
	lcd_gfx_goto_char_xy(1,2);	
	lcd_gfx_write_string("Distance sensor");
 
	// Setup of the ADC
	adc_init(ADC_REF_AVCC, ADC_PRESCALE_8);
 
	// Endless loop
	while (1)
	{
		// Reading the 4 times rounded value of output voltage
		value = adc_get_average_value(ADC_CHANNEL, 4);		
 
		// Conversing ADC value to distance
		distance = ir_distance_calculate_cm(GP2Y0A21YK, value);
 
                lcd_gfx_goto_char_xy(1,3);
 
		// Was the calculation successful?
		if (distance >= 0)
		{			
			// Conversing distance to text
			sprintf(text, "%3d cm   ", distance);
		}
		else
		{		
			// Creating the text for an unknown distance
			sprintf(text, "Error   ");
		}
 
		lcd_gfx_goto_char_xy(1,3);
		lcd_gfx_write_string(text);
		sw_delay_ms(500);
	}
}
en/iot-open/practical/hardware/itt/avr/ir_distance.1756807411.txt.gz · Last modified: 2025/09/02 10:03 by raivo.sell
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