Sharp GP2D02 IR Distance Sensor
with a 68HC11 (BOTBoard)

Back to circuits & robots.

Last updated:

I ran across a nice little distance sensor (DIRRS) and have experimented using it with a 68HC11 on a BOTBoard.  I have code examples in both assembly language and also in SBASIC (see below).  First, let me introduce the concept of the sensor, my experiments and findings, and conclusions.  Also, the price is very reasonable.   In quantity 1, it is available from HVW Technologies for $39 CDN each (including cable and resistors).  This is Canadian dollars; with the current (12/23/1998) exchange rate of 0.644081, this is $25.12 US. 

The sensor is a Sharp GP2D02 distance measuring sensor, good in the range from 10cm to 80cm (about 4 inches to 2.5 feet); perfect for robots.  It is an integrated package which uses an IR emitter and an array of photodiodes (Position Sensitive Detector, or PSD) behind a lens.   The distance of the object is determined by the portion of the PSD which gets the reflected IR light based on the parallax of the outgoing and incoming rays.  See the figure below (copied from HVW).

dirrs_how_to.jpg (22016 bytes)

What's the GP2D05 sensor (also known as the IRODS sensor)? This look like the same physical sensor, probably with slightly different internal electronics.  The IRODS sensor has a single-bit output which indicates if an object is within the distance threshold of the sensor.  The threshold can be set using an external potentiometer.   For robotics work, the DIRRS sensor would be the most useful, providing a measure of distance in a 4 inch to 2.5 foot range.  The IRODS, with a simple yes/no measure of a threshold distance is much more limited.

IR Light

The emission of IR light is centered at 855nm with a 50%-of-peak bandwidth of 50nm.  The documentation states that the sensor uses modulation to avoid false triggering.  This appears to work very well.  I did not experience any problems in different light environments.  Using two sensors on two different devices (not coupled) showed not interference what-so-ever.  During the time when the IR light is active, I measured modulation of the IR source as off for 112uS and on for 772uS, with a cycle time of 884uS (1.13KHz).  In one test I used a pair servo motors to position the sensor (Tower Hobbies System 2000 TS-51).  Having these two servo motors in close proximity to the sensor caused no ill effects.

I also tested two of these units pointing directly at each other.  I expected to see some interference between them, but did not see any!  This makes it possible to have multiple DIRRS sensors on a single robot, and even better, to have multiple robots, each with their own DIRRS sensor on it, all roaming about in the same vicinity.

Sensor Hookup

The DIRRS sensor has a 4-wire interface:

  1. Ground
  2. Vin (Max 3.0 volts!) Use a voltage divider from a TTL-level signal.
  3. Vcc (5 volts)
  4. Vout

I used I/O pins A7 and A2 on the 68HC11 for Vin and Vout respectively.   For simplicity, I wired up the voltage divider for Vin in the cable.  This way I can use the sensor in different projects and the voltage divider comes along with it.   I've heard reports from people who have used a full TTL signal into Vin and have fried their sensor.  I do not have any first-hand evidence of this, but please be safe.  Why Sharp didn't make Vin TTL compatible is beyond me!

I also found it important to ground the chassis of the DIRRS sensor.  It looks like black plastic, but is conductive (I check with an ohm meter).  Without grounding the chassis, the readings from the DIRRS jumped around a little.  By grounding the chassis, the reading became very stable.

Test Setup

dirrs.jpg (18355 bytes)  

To test the sensor, I used a 68HC11E1 microcontroller on a BOTBoard.   The DIRRS is connected as mentioned above: Power, ground, A7 (through a voltage divider, two 1K resistors) to Vin and A2 to Vout.  I also connected to the BOTBoard a 16-character by 2-line LCD display for readout, using port C on the 68HC11.  I mounted the BOTBoard, LCD, 4-AA batteries on a spare piece of polycarbonate, with the DIRRS sensor on a second piece of polycarbonate standing up. 


The 68HC11 was connected to the LCD display with the following connections:



V+ LCD2 Power
Gnd LCD1 Ground
Vref LCD3 Reference voltage (Some LCD's need negative voltage)
C0 LCD11 Data I/O to LCD data bit 4
C1 LCD12 Data I/O to LCD data bit 4
C2 LCD13 Data I/O to LCD data bit 4
C3 LCD14 Data I/O to LCD data bit 4
C4 LCD4 RS (register select) L=control, H=data
C5 LCD5 R/W H=read, L=write
C6 LCD6 E (enable) H is enabled, transaction on H->L edge
C7 -unused- -unused-


This sensor is very easy to interface with.  To get a distance reading, use the following steps:

  1. Set Vin LOW.
  2. Wait for Vout to go HIGH.  (Sensor is getting distance measurement)
  3. For each of the 8 data bits: Set Vin HIGH, then set Vin LOW.  Read data bit.
  4. Set Vin HIGH.

These steps are implemented in 68HC11 assembly language as follows:

  1.     ldx    #$1000
        bclr    0,x %10000000
  2. Wait_For_Distance
        brclr    0,x %00000100 Wait_For_Distance ; wait for IR unit
  3.     clrb
        bsr    Get_IR_Bit
        bsr    Get_IR_Bit
        bsr    Get_IR_Bit
        bsr    Get_IR_Bit
        bsr    Get_IR_Bit
        bsr    Get_IR_Bit
        bsr    Get_IR_Bit
        bsr    Get_IR_Bit
        . . . . . . .
        bset    0,x %10000000    ; HIGH
        bclr    0,x %10000000    ; LOW
        nop              ; delay to allow data to stabilize
        nop              ; Note:   Additional NOP needed for 2 out of 6 of my test sensors
        ldaa    0,x   ; data in A2
        lsra             ; data in A1
        lsra             ; data in A0
        lsra             ; value from A2 is now in C, roll into B
  4.     bset    0,x %10000000

dirrs_timing.gif (5053 bytes)

The resulting timing using this code with a 68HC11 running at 2MHz (8MHz crystal) was about 45.6 mS for the sensor to take a distance measurement (step 2 above).  The eight pulses for getting the data bits were 3.5uS high and 16.5uS low.  Notice the NOP instruction in step 3 above.  This was found to be necessary to allow the sensor data bit to become stable for reading.  The NOP adds 2 machine cycles, or a 1uS delay.  I tested 6 of these Sharp sensors, and 2 of them did not work with a single NOP instruction here.  I added a second NOP and the resulting sensor data was good.   The high time in step 4 was 4.18mS.  This gave an overall cycle time of just under 50mS for each sensor reading.  This gives us about 20 sensor reading per second.

The Code

I have written code for the 68HC11 in both assembly language and in Karl Lunt's SBASIC.