AVR Controlled GP2D120 Distance Sensor

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 Screenshot of GP2D120 Demo output Click to Enlarge

Fig. 2 - Schematic Schematic for GP2D120 AVR demo Click to Enlarge

Fig. 3 - Screenshot of Data from AVR (via) RS-232 Screenshot of GP2D120 Demo output 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

Did you enjoy AVR Controlled GP2D120 Distance Sensor? If you would like to help support my work, A donation of a buck or two would be very much appreciated.
blog comments powered by Disqus
Linux Servers on the Cloud IN MINUTES