Skip to main content

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 GP2D02.zip 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;

GP2D02 distanceSensor(INPUT_DATA_PIN,OUTPUT_CLOCK_PIN);


Then you can get the distance from it like this:

distanceSensor.refresh();
byte distance = distanceSensor.read();


And that's it!

(Note that the shift-in function in the library was adopted from http://www.arduino.cc/en/Tutorial/ShftIn22 and modified in order to account for the timing requirements of the GP2D02 sensor)

Comments

  1. This comment has been removed by a blog administrator.

    ReplyDelete

Post a Comment

Popular posts from this blog

OpenSCAD Rendering Tricks, Part 3: Web viewer

This is my sixth post in a series about the  open source split-flap display  I’ve been designing in my free time. Check out a  video of the prototype . Posts in the series: Scripting KiCad Pcbnew exports Automated KiCad, OpenSCAD rendering using Travis CI Using UI automation to export KiCad schematics OpenSCAD Rendering Tricks, Part 1: Animated GIF OpenSCAD Rendering Tricks, Part 2: Laser Cutting OpenSCAD Rendering Tricks, Part 3: Web viewer One of my goals when building the split-flap display was to make sure it was easy to visualize the end product and look at the design in detail without having to download the full source or install any programs. It’s hard to get excited about a project you find online if you need to invest time and effort before you even know how it works or what it looks like. I’ve previously blogged about automatically exporting the schematics, PCB layout , and even an animated gif of the 3D model to make it easier to understand the project at a glanc

OpenSCAD Rendering Tricks, Part 1: Animated GIF

This is my fourth post in a series about the open source split-flap display I’ve been designing in my free time. Check out a video of the prototype . Posts in the series: Scripting KiCad Pcbnew exports Automated KiCad, OpenSCAD rendering using Travis CI Using UI automation to export KiCad schematics OpenSCAD Rendering Tricks, Part 1: Animated GIF OpenSCAD Rendering Tricks, Part 2: Laser Cutting OpenSCAD Rendering Tricks, Part 3: Web viewer Early when designing the split flap 3D model using OpenSCAD I wanted to include a visualization in the project’s README so others could see what it looked like. It’s possible to capture an image manually (File→Export→Export as Image), but that’s an extra thing to remember to do after every change and it’s also not very consistent. The image that’s exported is basically a snapshot of the current preview window, so the image dimensions and camera angle would be different each time. Plus, a single static image doesn’t fully convey the 3D mo

Scripting KiCad Pcbnew exports

This is my first post in a series about the  open source split-flap display  I’ve been designing in my free time. Check out a  video of the prototype . Posts in the series: Scripting KiCad Pcbnew exports Automated KiCad, OpenSCAD rendering using Travis CI Using UI automation to export KiCad schematics OpenSCAD Rendering Tricks, Part 1: Animated GIF OpenSCAD Rendering Tricks, Part 2: Laser Cutting OpenSCAD Rendering Tricks, Part 3: Web viewer For the past few months I’ve been designing an open source split-flap display in my free time — the kind of retro electromechanical display that used to be in airports and train stations before LEDs and LCDs took over and makes that distinctive “tick tick tick tick” sound as the letters and numbers flip into place. I designed the electronics in KiCad, and one of the things I wanted to do was include a nice picture of the current state of the custom PCB design in the project’s README file. Of course, I could generate a snapshot of the