Overview

The GP2Y0A21YK0F (often shortened to GP2Y0A21) is a popular analog infrared distance measuring sensor unit manufactured by Sharp. It incorporates an infrared LED emitter, a linear Position Sensitive Detector (PSD), and an internal signal processing circuit inside a black plastic package fitted with twin optical lenses.

Using optical triangulation, the sensor measures distance (10 cm to 80 cm10\text{ cm to }80\text{ cm}) by assessing the angle at which reflected IR light hits the PSD array. Because distance is determined by angle rather than reflected light intensity, readings remain largely immune to variations in target color, surface reflectivity, or ambient temperature. It is widely used for obstacle avoidance in mobile robotics and wall-following mini-sumo bots.

Quick reference

Operating voltage (VCC)4.5 V to 5.5 V DC (5.0 V nominal)
Measuring distance range10 cm to 80 cm (3.9" to 31.5"3.9\text{" to }31.5\text{"})
Output typeNon-linear Analog Voltage (3.1V3.1\text{V} at 10 cm to 0.4V0.4\text{V} at 80 cm)
Average supply current30 mA typical (peak pulse current ~200 mA)
Response update period38.3 ms±9.6 ms38.3\text{ ms} \pm 9.6\text{ ms}
Connector type3-pin JST-PH 2.0 mm connector

Pinout

The sensor terminates in a 3-pin JST-PH connector (wired cable provided):

PinCable ColorNameTypeDescription
1RedVCCPowerPower supply input (+4.5 V to +5.5 V DC)
2BlackGNDPowerGround reference (0 V)
3Yellow / WhiteVo / OUTAnalog OutputNon-linear analog distance voltage output

Specifications

ParameterSymbolMinTypMaxUnitConditions
Supply VoltageVCCV_{CC}4.55.05.5VDC
Average CurrentICCI_{CC}3040mAVCC=5.0 VV_{CC} = 5.0\text{ V}
Peak CurrentIpeakI_{peak}200mADuring IR LED pulse bursts
Distance RangeLL1080cmReliable range
Output Voltage (at 10 cm)V10V_{10}2.83.13.4VVCC=5.0 VV_{CC} = 5.0\text{ V}
Output Voltage (at 80 cm)V80V_{80}0.250.40.55VVCC=5.0 VV_{CC} = 5.0\text{ V}
Update PeriodTsampleT_{sample}28.738.347.9msOptical measurement cycle

Output Curve & Distance Math

The output voltage follows an inverse non-linear relationship:

  • At <10 cm<10\text{ cm} (blind zone), voltage drops sharply back down to 0V (causing ambiguity!).
  • At 10 cm10\text{ cm}, output peaks at 3.1V\approx 3.1\text{V}.
  • At 80 cm80\text{ cm}, output drops to 0.4V\approx 0.4\text{V}.

Distance DD (in cm) can be calculated from 10-bit ADC voltage VoutV_{out} using empirical inverse linear approximation:

D (cm)=27.86Vout0.1orD (cm)=4800ADCraw20D\text{ (cm)} = \frac{27.86}{V_{out} - 0.1} \quad \text{or} \quad D\text{ (cm)} = \frac{4800}{ADC_{raw} - 20}

Wiring

Sharp Sensor PinArduino UnoESP32 (with divider)Notes
Red (VCC)5VExternal 5V PowerPower from 5V rail
Black (GND)GNDGNDShared system ground
Yellow (Vo)Analog A0VP / GPIO36 (ADC1)Use voltage divider for 3.3V ADCs
Warning

Pulsed Current & Blind Zone Hazards:

  • The internal IR LED pulses at high current (~200 mA peak). Always place a 10 μF10\ \mu\text{F} to 47 μF47\ \mu\text{F} electrolytic capacitor across VCC and GND right at the sensor connector to stabilize the power rail and eliminate ADC voltage ripple.
  • Objects closer than 10 cm produce output voltages identical to objects at 30–80 cm. Ensure mechanical mounting recessed back from outer bumpers.

Example

cpp
const int sensorPin = A0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  int rawADC = analogRead(sensorPin);
  
  // Convert ADC reading (0-1023) to Voltage (0-5V)
  float voltage = rawADC * (5.0 / 1023.0);
  
  // Calculate distance in cm using empirical formula
  float distanceCM = 27.86 / (voltage - 0.1);

  if (distanceCM >= 10.0 && distanceCM <= 80.0) {
    Serial.print("Voltage: "); Serial.print(voltage);
    Serial.print(" V | Distance: "); Serial.print(distanceCM); Serial.println(" cm");
  } else {
    Serial.println("Target out of valid 10-80cm range");
  }

  delay(200);
}

Common mistakes

  • Operating within the <10 cm<10\text{ cm} blind zone: Causes false long-range readings when an obstacle is right against the lens.
  • Powering without decoupling capacitors: Inductive current pulses corrupt neighboring ADC measurements on the microcontroller board.

Notes

  • Sharp Distance Family: GP2Y0A21YK (10–80 cm), GP2Y0A02YK (20–150 cm), GP2Y0A41SK (4–30 cm).

Assets & downloads