Sharp IR Distance Sensor 2Y0A21YK

Can someone explain me how this sensor works? I can’t make a function for the arduino that returns the distrance that the sensor gets but it’s totally wrong and i can’t understand the charts on the datasheet

I need a code please…

GP2Y0A21YK-IR_Sensor.pdf (278 KB)


I was trying to answer the same question!!

As i understand for now those sharp sensor are calculating the angle of the light coming back to calculate the distance... If you really want to have a precise distance you should use ultrasonic sensor like

You can find some very cheap on ebay if you have no hurry ;)


i can't understand the charts on the datasheet

In the data sheet is a graph showing distance against reading. You will notice it is only correct over a certain range. If you are outside this range, that is too close the reading will be totally wrong. You can only use these sensors if your range is greater than the minimum distance. That distance is where the peak is on the graph.