Back to Sensors & Actuators Series

GP2Y0A21YK Sharp IR Distance Sensor

April 10, 2026 Wasil Zafar 7 min read

GP2Y0A21YK deep dive — IR triangulation principle, analog output, voltage-to-distance conversion, filtering, complete code, and robotics applications.

Contents

  1. Working Principle
  2. Electrical Characteristics
  3. Interfacing with MCU
  4. Calibration
  5. Code Example
  6. Real-World Applications
  7. Limitations

Working Principle

The Sharp GP2Y0A21YK is an infrared triangulation distance sensor. An IR LED projects a spot of light onto a target. The reflected light hits a linear Position-Sensitive Detector (PSD). As the target distance changes, the reflected spot shifts along the PSD, changing the output voltage — a principle called optical triangulation.

Analogy: Hold your thumb at arm's length and close one eye, then the other — your thumb appears to shift (parallax). The GP2Y0A21YK uses this same principle with an IR LED and a detector instead of two eyes.

Electrical Characteristics

ParameterValue
Supply Voltage4.5 V – 5.5 V
Detection Range10 – 80 cm
OutputAnalog voltage (inversely proportional to distance)
Output Voltage~0.4 V (80 cm) to ~3.1 V (10 cm)
Update Rate~26 ms per measurement (38 Hz)
Current~30 mA typical
Beam WidthNarrow (~5°)

Interfacing with an MCU

The sensor has a 3-pin JST connector: VCC (red), GND (black), and signal (yellow). Connect the signal wire to an ADC pin. A 10–47 µF bypass capacitor across VCC-GND reduces noise from the pulsed IR emitter.

Wiring (Arduino): VCC (red) → 5 V, GND (black) → GND, Signal (yellow) → A0. Add a 10 µF electrolytic capacitor close to the sensor pins.

Calibration

The output voltage is non-linear — inversely proportional to distance. Common linearisation methods:

  • Empirical formula: distance_cm = 27.86 / (voltage - 0.1) (approximate fit for 10–80 cm).
  • Lookup table: Measure voltage at 10, 20, 30, 40, 50, 60, 70, 80 cm; interpolate between samples.
  • Averaging: Take 5–10 ADC reads and median-filter to reject spikes from noisy power supplies.

Code Example

/*
 * GP2Y0A21YK Sharp IR Distance — Arduino
 * Wiring: Signal→A0, VCC→5V, GND→GND
 * Add 10µF cap across VCC-GND
 */
#define SENSOR_PIN A0

float readDistance() {
    int total = 0;
    for (int i = 0; i < 10; i++) {
        total += analogRead(SENSOR_PIN);
        delay(2);
    }
    float voltage = (total / 10.0) * (5.0 / 1023.0);
    if (voltage < 0.15) return -1;  /* Out of range */
    return 27.86 / (voltage - 0.1);  /* Empirical formula */
}

void setup() {
    Serial.begin(9600);
    Serial.println("GP2Y0A21YK Ready");
}

void loop() {
    float dist = readDistance();
    if (dist > 0 && dist <= 80) {
        Serial.print("Distance: ");
        Serial.print(dist, 1);
        Serial.println(" cm");
    } else {
        Serial.println("Out of range");
    }
    delay(250);
}

Real-World Applications

Robotics

Robot Obstacle Avoidance

Mobile robots mount multiple GP2Y0A21YK sensors at 45° intervals for 360° short-range scanning. The narrow beam and fast 38 Hz update rate let the robot detect walls, table legs, and obstacles from 10–80 cm, enabling reactive avoidance algorithms that complement ultrasonic sensors.

Obstacle AvoidanceMobile RobotProximity

Limitations

  • Dead zone: Targets closer than 10 cm give incorrect (lower) voltage — mount sensors back from the chassis edge.
  • Colour/surface dependence: Dark and absorptive surfaces reflect less IR, shortening the effective range.
  • Sunlight interference: Outdoor IR noise can degrade accuracy; use an IR-pass filter or shade the sensor.
  • Narrow view: The ~5° beam misses small obstacles between sensors; use multiple units or complement with ultrasonic.