Friday, September 3, 2010

Using a GP2D02 IR distance sensor with Arduino

Just wanted to share an Arduino library I made over a year ago which I just realized might come in handy for others playing with the popular GP2D02 distance sensor.

The GP2D02 is a neat digital distance sensor made by Sharp which uses reflected IR light to measure distances from about 3" to 3' (I think it's been discontinued now though). The fact that it has a digital output is convenient when using a microcontroller, although since the Arduino has analog inputs you might also consider using the analog variants like the GP2D12 or GP2Y0A02 instead.

To install the libary, you need to download and place the GP2D02 directory into the Arduino libraries directory and then restart the Arduino IDE.

Once you've installed the library you can set up a sensor like this:

#include <GP2D02.h>
  o|       |---Black--------|Gnd
   | GP2D02|---White--->|---|Pin 2 (note the interfacing diode)
  O|       |---Red----------|+5V
   |_______|---Yellow ------|Pin 7

const int INPUT_DATA_PIN = 7;
const int OUTPUT_CLOCK_PIN = 2;


Then you can get the distance from it like this:

byte distance =;

And that's it!

(Note that the shift-in function in the library was adopted from and modified in order to account for the timing requirements of the GP2D02 sensor)

No comments:

Post a Comment