It's been a long day at work, you hop in your car and drive home through the daily rush hour. Finally, you've arrived, only thing left is to park the machine in your garage, going backwards in. You put your gears in reverse, give some slight gas, and look at your rear view. But the windows are a little bit foggy, the light is pretty weak, and you just don't know how near that wall is behind you. You think you have a little bit more space, you add a little bit more gas and... BAM! You hit the wall! Luckily, no real damage is done, but you arrive home frustrated and nervous. Newer cars have distance sensors which help you when parking to avoid these types of situations. Today we'll discuss one of those. Which is located in our IR Distance click.
The source code and libraries can be found on GitHub and Libstock.
How does it work?
The IR distance click carries the GP2Y0A60SZ0F distance sensor from Sharp. It is composed of an integrated combination of PSD (position sensitive detector) , IR-LED (infrared emitting diode) and signal processing circuit. This module measures distance using the Triangulation method, here's how it works:
The IR LED emits a narrow light beam. After reflecting from the object, the beam will be directed through the second lens on the PSD, depending on the distance of the object, the angle of the reflected light will be different. The conductivity of the PSD depends on the position where the reflected beam falls. The conductivity is converted to voltage and if the voltage is digitalized by using analogue-digital converter, the distance can be calculated.
The output of distance sensor is inversely proportional, this means that when the distance is growing the output is decreasing. 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 on 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 to register 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 the Sharp sensor, meaning that the output starts to decrease (again) sharply as the distance is at certain point (depending on the model 4-20 cm), our sensor works best in the range of 10cm - 100cm. This problem can be avoided by making sure that the object is not too close to the sensor.
Working with the click
So, now you attached your IR Distance click to the back of your car, hooked it up on to an MCU, and you want to program it, so that you will never again hit your car while parking. Let's see how you can set up and successfully work with the IR Distance click!
The click needs to convert analog values to digital, so you need to have your desired ADC module initialized. After that, call the initialization function. The parameters passed to the initialization function are as follow: the ADC read function which you will use to convert from analog to digital, the desired ADC channel to read from, and the desired conversion resolution.
ADC1_Init(); ir_measure_init( ADC1_Read, 4, RESOLUTION_12 ); // ADC1_Read is used for ADC conversion, reading from channel 4, 12-bit resolution
That's about it as far as initialization goes, now you just need to read your distance:
double distance; distance = ir_measure_cm();
That;s it! Simple as that! And you will never have to replace your car bumper ever again! Here's a full example:
#include "ir_distance_hw.h" void system_init( void ); void system_init() { ADC1_Init(); // Initialize ADC UART1_Init_Advanced(115200, _UART_8_BIT_DATA, _UART_NOPARITY, _UART_ONE_STOPBIT, &_GPIO_MODULE_USART1_PA9_10); // Initialize hardware UART1 module on PORTA at 115200 bps, 8 data, no parity and 1 stop bit : ir_measure_init( ADC1_Read, 4, RESOLUTION_12 ); // initialize ir distance click, adc is done on ADC1 module, channel 4, 12-bit resolution UART1_Write_Text( "Here and Readyrn" ); // check if UART is initialized and ready } void main() { char tmp_text[20]; system_init(); // initialize modules while( 1 ) { sprintf( tmp_text, "Distance is: %4.2frn", ir_measure_cm() ); // measure distance, convert and store to tmp_text UART1_Write_Text( tmp_text ); // send the measured value through UART Delay_ms( 500 ); } }
Summary
IR Distance detection is being used in a wide variety of purposes: whether you want to be better at parking your car, or you are trying to make a robot which will know it's way around the house, distance measuring is needed for successful operation in real time and space. For any additional information make sure do check out the documentation provided with our library.
References
wikipedia.org "Proximity sensor" wikipedia 2016
www.societyofrobots.com "IR vs Ultrasonic" societyofrobots 2016
roboticlab.eu "Infrared distance sensor" roboticlab 2016