16 September 2006

Distance Measurement Sensor

GP2Y0A21YK is General Purpose Type Distance Measuring Sensors from Sharp. (see the product page). Sharp Infrared (IR) radiation Distance Measuring Sensor use Infrared signal to measure object distance from 10 to 80 cm with analog output.


  1. Less influence on the color of reflective objects, reflectivity

  2. Line-up of distance output/distance judgement type

    Distance output type (analog voltage) : GP2Y0A21YK

    Detecting distance : 10 to 80cm

    Distance judgement type : GP2Y0D21YK

    Judgement distance : 24cm

    (Adjustable within the range of 10 to 80cm [Optionally available])

  3. External control circuit is unnecessary

  4. Low cost


  1. TVs

  2. Personal computers

  3. Cars

  4. Copiers

  5. Robots

The output of this sensor is analog therefore it need Analog to Digital Converter (ADC) to interface the 8051. I use ads7841 as ADC (see this article).

The graph of Analog voltage output versus distance to reflective object are shown in Fig.1. When I plot Analog voltage output versus inverse number distance, I found the linear relation shown in Fig.2.

Fig. 1 Analog voltage output (V) versus distance to reflective object (cm).

Fig. 2 Analog voltage output (V) versus inverse number distance (1/cm).

The linear equation is y = 20.99x + 0.19, where y is voltage output and x invert distance. Notice that the voltage output I can trust are about 0.4 to 2.8 V.

This is an example code how to interface the distance sensor to 8051. I use SDCC as C Compiler.

#include <8051.h>

#include "ads7841.h"

void main()


float x,y; // Distance and Analog voltage output

y = analog(0); // Read volt out if the sensor connect to channel 0 of ADS7841

y = 0.00122*y; // Convert BCD to DEC by multiply voltage by 5/4096

if ( (y > 0.4) && (y < 2.8) ) {

x = (y-0.19)/20.99; // Solve the linear equation

x = 1/x; // Inverse back get distance in cm.



characteristic.txt (For plot with Spreadsheet)

Related Links

GP2Y0A21YK/GP2Y0D21YK Datasheet
Analog to Digital (ADS7841) Interfacing

No comments: