AVR Controlled GP2D120 Distance Sensor
May 11, 2006
Sharp's GP2D120 (and the GP2D12) is an infared distance sensor which is pretty common in the hobby robotics community. I purshaces a couple of these from DigiKey to use in a small wall-huggin robot. I spent quite a bit of time experimenting with it. This page will provide you with my schematic and AVR gcc source code for my demo file. It simply uses the ADC on an ATMega8 mpu to read the analog output of the GP2D120 and send the distance in centimeters to my laptop through the serial port (actually, through a USB to serial converter).
Circuit Description
I began simply experimenting with the GP2D120 comparing the analog voltage to distance, plotting that data in a chart (Fig. 1), and comparing with the data sheet. My data turned out to be pretty similar to the chart in the datasheet-- which was good news. However, one important thing to notice is that the curve increases from 0 up to a peak at around 3cm. So the data for 0-3cm must be ignored. It is because of this that you will want to mount the sensor about 3cm back when using it in a robot or other practical application. Since the maximum output I got from the sensor was 3.15V or so, I have setup my ATMega8 ADC to use a 3.3V reference voltage. This allows me to use the maximum range. I setup my 3.3V reference to the AREF pin on the AVR using a LM317Z variable voltage regulator. I keep a few of these around as they are a cheap and easy way to get any a regulated voltage from 1.2V to almost 30V. In the schematic, (Fig. 2) the LM317Z's output is controlled by adjusting R3. It's important to use this 3.3V reference if you're going to be using my code, as my lookup table is based a 3.3V reference voltage. I originally had the output of the sensor on an LCD, however, I changed over to using the AVR's UART and sending the data to my laptop through the RS-232 serial port (actually, I only have USB so I'm using a RS-232 to USB converter). I convert the TX/RX output from the AVR to RS-232 using the MAX232 chip. This is not shown in the schematic as I'm actually using a little board I made from a magazine article in Nuts and Volts Novermber 2000 issue: RS-232 ON A BREADBOARD by Al Williams. There are hundreds of schematics for using this chip out there, or, you can just purchase the RS-I Serial Interface Adapter. The data is sent using Peter Fleury's UART library where the ADC value and it's corresponding distance is simply scrolled continually down a terminal screen (Fig. 3). I'm using Minicom on Linux, however, Hypterterminal works just fine in Windows. You just need to setup the emulation for N-8-1 at 9600 baud. I added the 8MHz crystal as I got garbage data when using the ATMega8's internal 1MHz oscillator (which is what is factory shipped, so I had to change the fuses). The lookup table is stored in program flash and based on an 8-bit ADC value, which means that I have to first setup the ADC to left-justify the result and then just take the upper 8 bits of the 10-bit result by reading the ADCH register. Fig. 1 - Data Charted Through Experiment
Click to Enlarge
Fig. 2 - Schematic
Click to Enlarge
Fig. 3 - Screenshot of Data from AVR (via) RS-232
Click to Enlarge
Download Project Files
Download gp2d120-1.tar.gz Files in gp2d120-1.tar.gz:- main.c - Main program for the ATMega8 AVR (GPL).
- gp2d120.c/gp2d120.h - Routines and lookup table for GP2D120 (GPL).
- uart.c/uart.h - Peter Fleury's UART library.
- Makefile - Makefile for avr-gcc and avrdude in Linux.
- gp2d120-1.hex - Tested hex file of compiled program for ATMega8 AVR.
- circuit.sch - Schematic in Eagle format.
- circuit.png, screenshot.png, datachart.jpg - Figures 1, 2, and 3 above.
Resources
- Sharp GP2D120 Datasheet and Application Note
- Peter Fleury's avr-gcc UART library
- The RS-I Serial Interface Adapter (commercial version of article below)
- RS-232 ON A BREADBOARD - Al Williams. Nuts & Volts Magazine, Nov, 2000
Categories:
Robotics & Electronics
Copyright © 2004 - 2010 Micah Carrick. All Rights Reserved.
4 Comments about "AVR Controlled GP2D120 Distance Sensor"
RSS Feed
Thank you very much for publishing an useful experiment with AVR and Sharp IR distance sensor.
From the curve it is found that its output is nonlinear type and its voltage change is very very less in last part of its range, can you tell me how it can be make linear so that we can get Y = mX( +C type relation.
How it can be tackled , by providing OP amp or using software in AVR, how it will be, please reply.
Thanks and regards,
Uttam Dutta
u.dutta@tatasteel.com
i'm a beginner in avr programming that trying to use the ADC in ATMEGA16 to read the analog data from SHARP GP2D12 and need a c program for CodeVision compiler.can u show me how to connect this sensor to avr and a LCD ?
Leave a Comment about "AVR Controlled GP2D120 Distance Sensor"