Project List

Mail me

Last update: 2012-09-07



Sharp offers various distance ranging sensors based on infrared technology. This article shows how GP2D02 and GP2D120 can be evaluated by an ATmega8 microcontroller.


The following circuit is build around an Atmel ATmega8. Circuit is almost said to much, the Sharp sensors GP2D02 and GP2D120 require almost no additional parts.
Wiring diagram of IR distance ranging circuit


The measured value from the GP2D120 is much easier to process with an ATmega8. The device has an analog output and can directly be connected to the AVR's ADC inputs.

The ADC channels are sampled in the ADC's ISR. In there we also switch the channels round and round. The program continuously samples one channel and then switches to the next channel. To achieve this I don't use the free running mode. Instead sampling is triggered initially by enabling the ADC. All subsequent samples are triggered from within the ISR. The free running mode was harder to get to work with all that channel switching.

The GP2D120 is known for producing spikes on its outputs. The solution is to solder two additional capacitors (10μF and 100nF). This is a quite tricky thing since the soldering pads are quite small. I wanted to avoid destroying the sensor with my soldering iron. I also want to spare the effort. Therefore the program samples multiple values per channel. At the end an average value per channel is calculated.


The GP2D02 is an older, discontinued product. It has a digital interface. The measurement result needs to be pulsed out of the device, which is certainly more complicated than just reading analog inputs. However, I still had two of these in my drawer - so I wanted to use them.

There are two things that need to be taken care of:

  1. The input has a maximum rating of 3V which is not compatible with the 5V output of the ATmega8.
  2. Timing.

Level Conversion

There are three possible solutions to connect the sensor's 3V input with the AVR's 5V output:
  1. Use two resistors.
  2. Use a MOS-FET transistor.
  3. Use a diode.

I have decided to use the diode. This seemed the least effort to me. It works because the diode is used in reverse direction. When the output is low (0V), we pull down the input. When the output is high (5V), the diode is blocking. The input is free floating and pulled high to the right level (3V) by an internal pull-up resistor.

Three possible ways of adapting output to input voltage level


On the internet I found various program examples for this old sensor. The were all quite similar like this:
PORTB |= _BV(PORTB0); _delay_ms( 2 ); PORTB &= ~_BV(PORTB0); _delay_ms( 70 ); while( !( PINB & 0x02 ) ); uint8_t value = 0; for( uint8_t i = 0; i < 8; i++ ) { PORTB |= _BV(PORTB0); _delay_us( 100 ); PORTB &= ~_BV(PORTB0); _delay_us( 50 ); //1 value *= 2; if( PINB & 0x02 ) value += 1; _delay_us( 40 ); //99 } printf( "Distance: %d\n", value );

However, I could not really make that work. Measurement results were fluctuating. I experimented with the time constants and found out that the whole thing is quite sensitive with regards to timing (not very surprising). Hence my solution involves timer #1 to produce proper timing. The timing diagram of the Sharp GP2D02 sensor helps understanding the problem. Many things are going on as the timing diagram depicts:

  1. Measurement is initiated by pulling the sensors input down. The sensor will then issue 16 pulses of infrared light and process the results. The result is a byte which can be accessed serially MSB first.
  2. The sensor raises its output in order to signal that the result is ready for being picked up. This may take up to 70ms. I have observed shorter periods of time. It seems to depend on the distance.
  3. At this time we can begin to shift the result into the microcontroller by producing eight pulses of 200μs, where low and high are both of the same duration (100μs)
  4. Data can be read with the falling edge.
  5. After all bits were shifted in we raise the sensor input again.
  6. The sensor is ready for another measurement once the sensor input had been high for at least 2ms.

GP2D02 timing diagram

The part of the software that interacts with the GP2D02 sensors is implemented as a state machine:

Alternatively one may try to shift in the bits via the SPI interface.



Prototype with one GP2D02 attached

Version History

1.0.02012-07-28Initial version.
1.1.02012-09-07Now using libAvrUtils 1.2.0.

Copyright Statement

mega8irRanger Copyright (C) 2012 Holger Zahnleiter

This program comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions. The program and its source code are published under the GNU General Public License (GPL). See http://www.gnu.org/licenses/gpl-3.0.txt for details.


Creating this library would not have been possible without the achievements of the following persons/organisations:


Sharp IR Sensor Ausgabe verbessern Article (german) on how to improve the quality of the analog output of a Sharp GP2D120.
http://www.mikrocontroller.net/ A great (german) resource of Atmel AVR tutorials and general microcontroller know how.
RoboterNETZ Another great (german) resource of microcontroller know how.
AVR freaks English speakers should visit this website.
CrossPack CrossPack for AVR Development for Mac OS X.
AVR-GCC-Tutorial German avr-gcc tutorial from mikrocontroller.net.
http://www.nongnu.org/avr-libc/ The AVR C library (for use with avr-gcc).
http://www.nongnu.org/avrdude/ AVRDUDE, a tool for flashing AVRs.
http://www.cadsoft.de/ EAGLE a CAD software for designing circuits. Free version available for Mac OS X, Linux and Windows.
RS-232 product site MAXIM's website featuring their line of RS-232 transceivers. The most current datasheet can be downloaded from there.
Sharp IR sensor products. I could not find a web site under the sharp domain dedicated to the GP2Dxxx series of IR distance ranging sensors. Furthermore it seems Sharp has given new names to its product. This is a link to an electronic components catalog which also lists the sensors.
ATmega8 product site Atmel's website featuring ATmega8. The most current datasheet can be downloaded from there.


Source code for ATmega8 (mega8irRanger-1.1.0-src.zip)
ATmega8 test program (test.hex)
Wiring diagram/Eagle 6.2.0 (mega8irRanger.sch)
Atmel ATmega8 Datasheet
Sharp GP2D02 Datasheet
Sharp GP2D120 Datasheet
Sharp GP2D120XJ00F Datasheet
MAXIM RS-232 Transceivers Datasheet
Fastron 11P 10ÁH Inductor Datasheet
Fastron 11P Inductors Product Codes


The information on this web site and the documents downloadable from here have been carefully reviewed and is believed to be reliable, but I do not assume any liability arising out of the application or use of any documents, programs or circuit described herein.
Furthermore I want to declare that I'm not responsible in any way for the content of other web pages, books and other sources I'm refering to.