Wednesday, May 25, 2011

Long Range Infrared Sensor: GP2Y0A02YK0F

Has it really been over two weeks since I last updated the blag? I for one blame reddit, which has made me far more efficient at wasting time leaving more room for... wasting time.

I've acquired some sensors, motors, and a servo this past week and have been waiting to start plugging them into the Arduino. The servo and motor code seemed straight forward enough so instead I thought I'd first start with one of the cooler looking sensors I bought, the Sharp Long Range Infrared Proximity Sensor -  GP2Y0A02YK0F, courtesy of SparkFun.
I can't be the only one who thinks these look a lot like...
WHOA!
Don't forget your extra wire connectors
The sensors have an odd JST connector, so I'd suggest buying the additional wire adapter (only another $1.50) as well so you don't have to open it up and try to build your own and worry about damaging the sensor. I stripped the ends of the wires and soldered on a single header pin on each for easy wiring.

This long range infrared sensor can calculate the distance from objects up to 150cm away(roughly 5ft). It outputs an analog voltage corresponding to the distance that varies from roughly 2.8V at 15cm to 0.4V at 150cm. This value would be read by the Arduino using the analogRead() function.

Now, while this sensor is quite accurate between the range of 15-150cm (0.5-5ft), it does have two drawbacks:
  • The minimum cutoff for an accurate reading is 15cm, so any reading closer than that will be garbage
  • The voltage output over distance is not linear (more of a power function)

From the Sharp product manual

If your project requires checking for a distance closer than 15cm, there are short and mid-range versions available. If you don't want to sacrifice distance you can always just use more sensors in combination with the long range IR to cover it's "blind spot". Another way to compensate for the minimum distance would be to just place the sensor 6 inches behind the front of your robot...

As for the non-linearity, this just means that we'll have to throw together a nifty little algorithm to convert the sensor's output voltage into distance. Thankfully, Sharp has already taken care of the hard part and provided us with a chart of output values (as seen in the graph above). All we have to do is convert the analog voltages into a 10-bit digital value and determine a best-fit line equation.

Fancy graph drawn up in Excel

By placing the data values into Excel, I got the following graph. The graph most closely resembled a Power function, so the best-fitting line equation was y = 1735 * x ^ (-1.07), with an R^2 value of 0.997. Not bad. You may have noticed that the graph starts at 20 cm instead of 15 cm. I decided to sacrifice an extra 5 cm on the minimum distance in order to have a more accurate algorithm. Since I'll be using the sensor to make sure nothing comes within about 15 cm (6 in) of my robot, this won't be a problem. Also note that the algorithm doesn't take into account the x axis values, so in the equation x = 20 cm would be equivalent to x = 3... just make sure that you take that into consideration if your making an equation this way.

So, what do we do with this equation? Right now it doesn't help us at all since we are reading in y (voltage)   and want to solve for x (distance). We could bust out some scratch paper and a calculator and solve for x, or we could use the All Mighty WolframAlpha.

Is there anything this website can't do? 

So, taking into account the oddities of my graph, our equation to calculate the distance in cm is:
y = 10650.08 * x ^ (-0.935) - 10

For us 'mericans and our imperial units of measurment (10 chains in a furlong?), our algorithm in inches is:
y = 4192.936 * x ^ (-0.935) - 3.937

To use the algorithm in your code, you need to use the pow() function. So, in your Arduino sketch it would look something like this:
inches = 4192.936 * pow(sensorValue,-0.935) - 3.937

In order to test our algorithm, I taped my IR sensor to a tape measure and used a white sheet of printer paper as the test object.

Very rigorous and strict testing standards
 SCIENCE!
I see nothing, nothing!
Here is the code that I wrote up to test the IR sensor:

//  Noah Stahl
//  5/25/2011
//  http://arduinomega.blogspot.com
//  Arduino Mega 2560
//This sketch is used to test the Sharp Long Range Infrared Sensor.
//The sensor output is attached to analog pin 15. Once the distance
//is calculated, it is printed out to the serial monitor.

#define sensorIR 15               //Must be an analog pin
float sensorValue, inches, cm;    //Must be of type float for pow()

void setup() {
  Serial.begin(9600);
}

void loop() {
  sensorValue = analogRead(sensorIR);
  inches = 4192.936 * pow(sensorValue,-0.935) - 3.937;
  //cm = 10650.08 * pow(sensorValue,-0.935) - 10;
  delay(100);
  Serial.print("Inches: ");
  Serial.println(inches);
}


The algorithm works surprisingly well and, without interference, is easily accurate within the inch. The sensor starts at approximately 7.5 inches (19cm) so anything closer than that should be considered garbage data.

The only problem with the sensor is that it is susceptible to noise on the power supply line and needs to be perpendicular to the object for an accurate reading. Being an analog sensor, the output value is going to be dependent on the supply voltage. If you aren't supplying a constant voltage, the numbers are going to fluctuate and if your voltage starts to dip under 5V, you wont get the full 5 ft range. You may need to add an extra 0.1uF cap at the power supply or maybe average every few readings to smooth out the data.

Noise, noise, noise, noise, noise!

If the sensor is not level to the object, your distance is going to be off by a few inches. If you place your sensor on a servo and it's not secured properly, jumbling about (in my case poorly taped to a tape measure), you are going to get random spikes in your data. The sensor is really sensitive, which can be either a good or bad thing depending on your design.

Right now the code doesn't have a cutoff for minimum and maximum distances, but this would be an easy exercise to see if you've been paying any attention. The sensor should be able to read any value inside of roughly 7.5 - 60 inches if you're using this code. Also, if you set your sensor up about 7 inches back from the front of your robot, you can use this code to detect whether an object gets too close without having to worry about the sensor's blind spot. You technically wouldn't need any close ranged sensors if you place it this way to get a basic robot going, but its always good to have more than one sensor to double check your values and in case something manages to sneak in too close. Sensors are the eyes and ears of your robot and you can never have enough eyeballs.

16 comments:

  1. hello:
    i m using the same sensor as u did in ur projects..
    i m making a wall tracking robot.
    can u plz help me out in reducing the noise and unwanted variations in the circuit using passive components,,
    thanx

    ReplyDelete
  2. Sensors are transforming the way engineers think about position sensing

    Distance Sensor

    ReplyDelete
  3. i do my project in eyeblink detection.whether i use this sensor for this project or not

    ReplyDelete
  4. hey man, nice meeting up with you, im having abit of difficulty in coding the gp2y0a710k sensor. any advice. Im not so keen with this microsoft excel method. Really need some help man

    ReplyDelete
  5. Hello, I'm a portuguese studen. I'm working in a project that will use this sensor Sharp.
    I Tried to understand all the steps that you explain, but i'm stoked.

    after put all data on excel, and made the graph, the power function result is y = 1735 * x ^ (-1.07), with an R^2 value of 0.997. like yours,
    The next step it's easy, just put the data on wolframe site and pick the results.
    After this I didn't understand.

    my result is y = 1065.08 * x ^ (-0.935) and you show the same on the photo.

    however you use this y = 10650.08 * x ^ (-0.935) - 10.
    where came the last ten? and Why 10650.08?

    I hope you can help me understand this.

    best regards
    Daniboy

    ReplyDelete
  6. i have use arduino due properly not working .please tell how to write the code

    ReplyDelete
  7. sorry, in this form "//cm = 10650.08 * pow(sensorValue,-0.935) - 10;" what does the "-10" mean?
    And why is it there?
    Thx very much

    ReplyDelete
  8. Most of the time I don’t make comments on any post, but I'd like to say that this content is really helpful. Please share with us more post like this.
    Ultrasonic Level Sensor

    ReplyDelete
  9. hi i have an arduino with ethernetshield and i want to use the sharp sensor to detect if object pass by and count it. do you know how i can do that please?

    ReplyDelete
  10. This comment has been removed by the author.

    ReplyDelete
  11. The IR sensor gives constant output while used together with ultrasonic sensors. I am using arduino uno. please help.

    ReplyDelete
  12. Tank Level Sensor are commonly used for tank level measurement and control. They offer cost-effective, continuous level measurement and a wide range of features to handle almost any tank level management task. A wide variety of material combinations make them compatible with most liquid media and challenging fluids. Tank Level Sensors are available in multiple sensing technologies including float, radar and ultrasonic liquid level sensing.
    Tank Level Sensor

    ReplyDelete
  13. Are you looking for a level sensor. You will find various types of Tank level sensor in market, where some of them are cheap and some of them are too costly to buy and also you have to think about price. Icon process Control offering you the best quality Tank Level Sensor with an affordable price.

    ReplyDelete
  14. Sorry i have a question i have just used this code and the maximum distance it can measure is only 40cm please help me

    ReplyDelete
  15. I live in Madagascar ( East Africa),and life is worth living comfortably for me and my family now and really have never seen goodness shown to me this much in my life as I have been going through a problem as seriously as my son found a terrible accident last two weeks, and the doctors states that he needs to undergo a delicate surgery for him to be able to walk again and I could not pay the bills, then your surgery went to the bank to borrow and reject me saying that I have no credit card, from there i run to my father and he was not able to help, then when I was browsing through yahoo answers and i came across a loan lender Mr, Benjamin Breil Lee, offering loans at affordable interest rate I had no choice but to give it an attempt and surprisingly it was all like a dream, I got a loan of $ 110,000.00 to paid for my son surgery then get myself a comfortable business to help me going as well. I  thank God today is good and you can walk and is working and the burden is longer so much on me more and we can feed well and my family is happy today and i said to myself that I will mourn aloud in the world of the wonders of God to me through this God fearing lender  Mr Benjamin Breil Lee and I would advise anyone in genuine and serious need of loan to contact this God-fearing man on  ......  247officedept@gmail.com through .. and I want you all to pray for this man for me or Chat with him on whatsapp  +1-989-394-3740 as well.
    Thank you

    ReplyDelete