Overview of GP2D02 IR ranger
GP2D02 IR ranger is able to measure distances between approx. 10cm and 1m. Results are available as digital values you can access through a dedicated protocol. One pin, Vin, will be used to act on the ranger. Another pin, Vout, will be read to determine the distance. Basically, getting a distance involves the following:
- First you wake up the ranger and tell it to perform a distance measure
- Then, for each bit, you read Vout in order to reconstitute the whole byte, that is, the distance
- finally, you switch off the ranger
The following timing chart taken from the datasheet will explain this better.
Note: the distances obtained from the ranger aren't linear, you'll need some computation to make them so.
Sharp GP2D02 IR ranger looks like this:
- Red wire is for +5V
- Black wire ground
- Green wire is for Vin pin, used to control the sensor
- Yellow wire is for Vout pin, from which 8-bits results read
Interfacing the Sharp GP2D02 IR ranger
Interfacing such a sensor is quite straight forward. The only critical point is Vin ranger pin can't handle high logic level of the PIC's output, this level mustn't exceed 3.3 volts. A zener diode can be used to limit this level.
Note: be careful while connecting this diode. Don't forget it, and don't put it in the wrong side. You may damage your sensor. And I'm not responsible for ! You've been warned... That's said, I already forgot it, put it in the wrong side, and thought I'd killed my GP2D02, but this one always got back to life. Anyway, be cautious !
Here's the whole schematic. The goal here is to collect data from the sensor, and light up a LED, more or less according to the read distance. That's why we'll use a LED driven by PWM.
Here's the ranger with the diode soldered on the green wire (which is Vin pin, using your previously created mental note...):
I've also added thermoplastic rubber tubes, to cleanly join all the wires:
Finally, in order to easily plug/unplug the sensor, I've soldered nice polarized connectors:
Writing the program
jallib >=0.3 contains a library, ir_ranger_gp2d02.jal, used to handle this kind of rangers. The setup is quite straight forward: just declare your Vin and Vout pins, and pass them to the gp2d02_read_pins(). This function returns the distance as a raw value. Directly passing pins allows you to have multiple rangers of this type (many robots have many of them arranged in the front and back sides, to detect and avoid obstacles).
Using PWM libs, we can easily make our LED more or less bright. In the mean time, we'll also transmit the results through a serial link.
var volatile bit gp2d02_vin is pin_a4Note: I could directly pass pin_A4 and pin_A6, but to avoid confusion, I prefer using aliases.
var volatile bit gp2d02_vout is pin_a6
var bit gp2d02_vin_direction is pin_a4_direction
var bit gp2d02_vout_direction is pin_a6_direction
include ir_ranger_gp2d02
-- set pin direction (careful: "vin" is the GP2D02 pin's name,
-- it's an input for GP2D02, but an output for PIC !)
gp2d02_vin_direction = output
gp2d02_vout_direction = input
var byte measure
forever loop
-- read distance from ranger num. 0
measure = gp2d02_read_pins(gp2d02_vin,gp2d02_vout)
-- results via serial
serial_hw_write(measure)
-- now blink more or less
pwm1_set_dutycycle(measure)
end loop
A sample, 16f88_ir_ranger_gp2d02.jal, is available in jallib SVN repository, and also in jallib released packages, starting from version 0.3. You can access downloads here.
Building the whole on a breadboard
Putting all this stuff on a breadboard should be easy. Just follow the schematics :)
I usually power two tracks on the side, used for the PIC and for the ranger:
Using the same previously created mental note, I connected the yellow Vout pin using a yellow wire, and the green Vin pin using a green wire...
Testing (and the video)
Time to test this nice circuit ! Power the whole, and check no smoke is coming from the PIC or (and) the ranger. Now get an object, like you hand, more or less closed to the ranger and observe the LED, or the serial output... Sweet !
Sébastien Lelong