|
DAN120
Interfacing a Sharp
GP2D120 or GP2D12 Distance Sensors
By Michael
Simpson
I have been playing
around with the various sharp IR distance sensors. The only real
difference between the GP2D120 and GP2D12 is the range of coverage.
GP2D120 4-30
Centimeters.
GP2D12 10-80
Centimeters.
The sensors
outputs approximately .4 to 3 volts depending on the distance to the item in front of
the detector. The closer it gets to an object the higher the
voltage. You can check out all the technical data by viewing
the data sheet found here.
Since the Dios has
built-in analog to digital ports using this sensor is quite easy.
Hookup
The first challenge
was interfacing to the connector on the sensor. The connector on the
sensor is a 2mm header. Several companies sell headers for this
connector. I made a small
extender by taking a 3 pin female connector and soldering it to a 3 pin
header.

While you could just
as easily solder three wires to the connector or the back of the PCB I
decided to just replace the connector. I like using .1mil headers so
it will mate with most US headers and bread boards. I removed the old one and
inserted a slightly modified 3 pin right angle header.


Schematic 1
In schematic 1 I show the connection to the Dios Versa
Board. Its the same for all the Dios Boards and chips.
Another problem area
with these sensors is the amount of noise the place on the power lines.
These things will crash most microcontrollers unless you place a capacitor
across the Vdd and Gnd leads as close to the sensor as possible.
The noise will also affect the accuracy of the sensor. You
should use a minimum of 100uf. However a 3000uf will
completely remove the noise an deliver full accuracy. Later I will discuss
a few software techniques for averaging out the readings.
Program 1
download it here
'GP2D120 Demo
func main()
dim range
AtoDinit(14)
loop:
range = AtoD(0)
pause 300
print range
goto loop
endfunc
include \lib\DiosAtoD.lib
This program will print a reading from the sensor once every 300ms.
If you used a 100uf capacitor for C1 you will see the value jump every so
often. Place a 470uf cap across the sensors Gnd and Vdd and it will
improve accuracy.
Program 2
download it here
'Avaraging Demo for the
GP2D120
func main()
dim x
dim rangavg as float
AtoDinit(14)
loop:
rangavg = 0
for x = 1 to 10
rangavg = rangavg + AtoD(0)
next
rangavg = rangavg / 10
print rangavg
goto loop
endfunc
include \lib\DiosAtoD.lib
This program is a bit more accurate. You can still use a 100uf cap and
get a reading within a few points.
Program 3
download it here
'Avaraging Demo for the
GP2D120
func main()
dim x
dim rangavg as float
AtoDinit(14)
loop:
rangavg = 0
for x = 1 to 10
rangavg = rangavg + AtoD(0)
next
rangavg = rangavg / 100
print rangavg
goto loop
endfunc
include \lib\DiosAtoD.lib
This program is tweaks the numbers just a bit by dividing the average a bit
more.
Program 4
download it here
'Range Status Demo for
the GP2D120
func main()
dim range,status
AtoDinit(14)
loop:
range = GP2Dread(0)
status = checkrange(range)
if status = 1 then print range , " Closer"
if status = 2 then print range , " farther"
if status = 12 then print range , " !! farther !!"
if status = 11 then print range , " !! Closer !!"
if status = 10 then print range, " !! Real Close !!"
goto loop
endfunc
'------------------------------------------
func GP2Dread(port) as integer
dim x
dim rangavg as float
rangavg = 0
for x = 1 to 10
rangavg = rangavg + AtoD(port)
next
rangavg = rangavg / 25
exit rangavg
endfunc
'---------------------------------------
function checkrange(range)
global oldrange as integer
if range > oldrange+1 then
oldrange = range
if range > 170 then
exit 11
else
exit 1
endif
endif
if range < oldrange-1 then
oldrange = range
if range > 170 then
exit 12
else
exit 2
endif
endif
if range > 200 then
exit 10
endif
exit 0
endfunc
include \lib\DiosAtoD.lib
We have added a function that checks the range and checks to see if the
object is moving closer or farther away. Also once we get within a
couple of inches it will tell us.
The programs above will
work identical with the GP2D12. Just keep in mind the range is
different.
Parts List
Easy RS232 Driver
DiosPro 28 Pin Chip
Dios 32 Pin Carrier
(Carrier #1)
|