# Snippet - Sharp 2Y0A21 IR Distance Sensor (NETMF 4.2)

Sharp 2Y0A21 IR Distance Sensor (NETMF 4.2)

The Sharp 2Y0A21 is a distance measuring sensor unit, composed of an integrated combination of PSD (position sensitive detector), IRED (infrared emitting diode) and signal processing circuit. The variety of the reflectivity of the object, the environmental temperature and the operating duration are not influenced easily to the distance detection because of adopting the triangulation method. This device outputs the voltage corresponding to the detection distance. So this sensor can also be used as a proximity sensor.

This class implements a simple driver, along with a polynomial calculated from the datasheet in order to convert ADC readings into distances in centimeters. It is much faster than other drivers which have been posted. Other drivers can take as many as 17ms to take 100 20-sample readings, while this driver can accomplish the same in only 6ms. As the sensor is quite noisy, the driver implements filtering of the readings in order to produce quite accurate results (given an accurate polynomialâ€¦).

3 Likes

Thanks â€“ I have a Domino project I may want to convert to Gadgeteer and this will be helpful.

How did you work out the formula? For what I gathered from your post, you replicated a graph from the documentation in Excel and used that to generate the formula.

Is that correct? I have a different ranged Sharp sensor, and may need to convert that as well.

Thanks again!

1 Like

Yes, exactly. Itâ€™s horribly inaccurate, because the graph isnâ€™t accurate. Sharp did that on purpose, they want you to test in your real world implementation and use that data.

In order to do it really well, youâ€™d need to take actual measurements at several (many) different distances under the same circumstances that youâ€™ll be using in your implementation (i.e. if youâ€™re measuring the distance to something white, then test with something white), and then curve-fit to that data for the formula. I used Excel to do the curve fitting, but there are more accurate ways.

1 Like