AN #16 - Using the Sharp GP2D02 distance sensor |
|
Using the Sharp GP2D02 distance sensor
The following application note was donated by Lex Bolkesteijn.
Lex is using the GP2D02 in his robots but there are many applications
for this part.
The GP2D02 from Sharp is a distance sensor that can measure
distances from 7cm to 1.2 meter.
It uses an optical transmitter and receiver to measure the distance and
outputs the distance in 8 bit serial format.
The datasheet of the GP2D02 can be downloaded from :
http://www.sharp.co.jp/ecg/sys/gp2d02/gp2d02-fea.html
A more in depth desciption of the GP2D02 is also available.
Dr.-Ing. Claus Kuehnel donated one of the chapters
of his BASIC
STAMP book that decripes the GP2D02.
In fact the BASIC STAMP book is a good source for interfacing all
kind of hardware.
Download the .PDF file for GP2D02
'--------------------------------------------------------------
' (c) 1999 Lex Bolkesteijn
'--------------------------------------------------------------
' file: GP2D02.BAS
'
' Sample program to use a Sharp GP2D02 distance sensor
' with a 89C2051. The GP2D02 is non lineair. The shorter
' the distance between object and sensor the higher the
output.
'
' The GP2D02 is availlible at Conrad Electronics
' http://www.sharp.co.jp/ecg/sys/gp2d02/gp2d02-fea.html
'
' GP2D02 89C2051
' pin 1 : Gnd -> Gnd +-- connect Vin/P1.0 here
' pin 2 : Vin -> P1.0 (Vcc - 1K --X-- 1K - Gnd)
' pin 3 : Vcc -> Vcc
' pin 4 : Vout -> P1.1 (GP2D02 12K internal pull up)
'--------------------------------------------------------------
'$sim
Dim D As Byte 'distance value
Dim T As Byte
Dim I As Byte
Dim S As String * 20
Config Lcd = 16 * 2 'configure lcd screen
Cls
Set P1.0 'set powerdown for sensor
Waitms 10 'default wait
Do
Reset P1.0 'Start cycle
Waitms 70 'time to calculate
Set P1.0
Shiftin P1.1 , P1.0 , D , 0 'clock bits in MSB first on falling edge
Set P1.0 'set powerdown for sensor
Waitms 2 'minimum delay for sensor
Cls
T = D / 16 'we use a 16 char. display (255/x)= 16 max
Lcd "Distance : " ; D 'display the distance
Lowerline
If T > 0 Then 'else string will
fail
S = String(t , 61)
Lcd S
End If
Loop
End
|