Sharp Infrared distance sensor GP2Y0A21YK and multikopter ping-pong

01 Sep 2010

Here is one of the easiest sensors to work with, at least with respect to collecting data, the Sharp GP2Y0A21YK infrared sensor (see eg http://www.coolcomponents.co.uk/catalog/product_info.php?cPath=36_57&products_id=236). Stricktly speaking it is a 5V device but will accept input voltages from 0.5 to 7V or so. So I just used the mbed's 3.3V output and the following short program:

#include "mbed.h"

AnalogIn IrSensor(p20);
Serial serial(USBTX, USBRX);

int main()
{
float IrVoltage = 0.0;

serial.baud(9600);

while(1) {
IrVoltage=IrSensor.read();
serial.printf("Voltage: %f\r\n", IrVoltage);
wait(1);
}
}

 

Wire up the sensor: 1) yellow wire = analog output voltage to mbed p20, 2) black wire to mbed GND, and 3) red wire to mbed 3.3V Vout. Compile the program and load the bin, open a terminal window at 9600 baud and restart the mbed and you should see the measured analog output value, with values ranging from 0.1245 to 0.7089. I haven't yet converted to actual volts and converting to distance is a bit tricky as the curve has a steep upslope from 0-10cm and then a downward curve flattining with distance as also shown in the manual. But it should be easy enough to either figure out a formula which fits the data, or just measure a series of distances and base the estimates of other measurements on that series.

With 5V input the analog output voltage should range up to about 3.1 V, so even that shouldn't be big problem.

And for those wanting ideas of what to do with this type of sensor, check out the video of two guys "ping-ponging" a hovering multikopter equiped with four of these sensors and programmed to keep a minimum horizontal distance from obstacles: http://www.diydrones.com/profiles/blogs/announcing-arducopter-the (first video a little bit down from the top of the page)

02 Sep 2010 . Edited: 02 Sep 2010

On the datasheet in Figure 5 they show a max analog out voltage around 3.15 as you noticed, so I did this with a 5V supply and it seemed to work fine on a project I tried last week. I used the 4 LEDs for a bar graph display of the analog value. You could move your hand up and down and see the bar graph change. The Sharp IR sensors (also GP2D120X, GP2D12 and 2Y0A02 and long range GP2Y0A700K) are often used in robot distance sensors to complement sonar and most of the robot web stores sell them. I heard once the first application was to check the amount of toner in copy machines. There are several IR sensors from Sharp and they show some as discontinued, so if you use it in a product double check and get the one still in production to be safe. They are also used in most Sumobots. Some people used to cover their Sumobot with black felt to try to defeat the other robots IR sensor. On the datasheet, note that objects closer than 10CM report a larger distance. One solution to this is to mount it away from the edge of the board or robot so nothing can get that close to it. Sharp also has some IR sensors that shift digital data out instead of analog, so if you see a different part number double check.

P.S. Pay the extra money if you have to and get the strange mini 3 wire connector to hook up the Sharp IR sensor. Some places sell it without that cable and it is a bit hard to get to the small pins. I don't think you could find parts for that connector without a trip to Japan!

 

They way it works in the figure below is kind of interesting, it actually measures the angle of the strongest reflected IR signal. I have seen an approximate equation for this years ago and normalized to a 1 for full scale it would be something like 4*arctan(1.9/distance) + .08 - hence the somewhat nonlinear response curve. 1.9 is related to the distance from the sensor lens to the LED (in CM). There is a small offset (.08) that varies between sensors a few %. Note that it picks up a "scattered" surface reflection.

Maxbotix also makes a nice low-cost mini 3.3V analog out sonar sensor (EZ series) that works exactly the same and would even work with exactly the same program. Smaller than the other Sonars. Here is a picture below. In addition to Maxbotix, Phidgets and Robotics Connection also sells them along with some of the Sharp IR sensors. Just solder up three wires to 3.3, GND, and analog out holes provided in the PCB. It has a PIC on it to control the sonar sensor and output the data. Some objects don't pick up well using IR and some don't work well with Sonar, so you might consider using both to make sure you don't miss a object. On a robot, you can make a sensor turret that turns on a small servo or often times several sensors are mounted facing in different directions to see everything around the robot. The data sheets will show the detection beamwidth.

02 Nov 2010

Cool - I fly Tricopters and quadcopters and I will try to make a program to use some sensors like the ones above to NXP

/Niclas