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 () 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 range | 10 cm to 80 cm () |
| Output type | Non-linear Analog Voltage ( at 10 cm to at 80 cm) |
| Average supply current | 30 mA typical (peak pulse current ~200 mA) |
| Response update period | |
| Connector type | 3-pin JST-PH 2.0 mm connector |
Pinout
The sensor terminates in a 3-pin JST-PH connector (wired cable provided):
| Pin | Cable Color | Name | Type | Description |
|---|---|---|---|---|
| 1 | Red | VCC | Power | Power supply input (+4.5 V to +5.5 V DC) |
| 2 | Black | GND | Power | Ground reference (0 V) |
| 3 | Yellow / White | Vo / OUT | Analog Output | Non-linear analog distance voltage output |
Specifications
| Parameter | Symbol | Min | Typ | Max | Unit | Conditions |
|---|---|---|---|---|---|---|
| Supply Voltage | 4.5 | 5.0 | 5.5 | V | DC | |
| Average Current | — | 30 | 40 | mA | ||
| Peak Current | — | 200 | — | mA | During IR LED pulse bursts | |
| Distance Range | 10 | — | 80 | cm | Reliable range | |
| Output Voltage (at 10 cm) | 2.8 | 3.1 | 3.4 | V | ||
| Output Voltage (at 80 cm) | 0.25 | 0.4 | 0.55 | V | ||
| Update Period | 28.7 | 38.3 | 47.9 | ms | Optical measurement cycle |
Output Curve & Distance Math
The output voltage follows an inverse non-linear relationship:
- At (blind zone), voltage drops sharply back down to 0V (causing ambiguity!).
- At , output peaks at .
- At , output drops to .
Distance (in cm) can be calculated from 10-bit ADC voltage using empirical inverse linear approximation:
Wiring
| Sharp Sensor Pin | → | Arduino Uno | ESP32 (with divider) | Notes |
|---|---|---|---|---|
Red (VCC) | 5V | External 5V Power | Power from 5V rail | |
Black (GND) | GND | GND | Shared system ground | |
Yellow (Vo) | Analog A0 | VP / GPIO36 (ADC1) | Use voltage divider for 3.3V ADCs |
Pulsed Current & Blind Zone Hazards:
- The internal IR LED pulses at high current (~200 mA peak). Always place a to electrolytic capacitor across
VCCandGNDright 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
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 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
- datasheetdatasheet
- product-pagereference