Skip to content
Sensorsproximity

Sharp GP2Y0A21 IR Distance Sensor

Analog IR distance sensor for obstacle detection at 10-80cm range — reliable and well-documented.

Wiring & code for

GP2Y0A21 practical reference

A Sharp analog infrared distance sensor commonly used in line-following robots and obstacle-avoidance builds. It combines an infrared emitter and a position-sensitive detector so that the angle of returned light (rather than just its intensity) determines distance, giving reasonably consistent readings across the 10-80cm range regardless of ambient light — a well-documented, reliable classic choice, though its non-linear analog output curve needs a lookup table or formula to convert to an actual distance value.

Key specifications

  • Sensor type: Sharp GP2Y0A21YK0F analog IR triangulation distance sensor
  • Operating voltage: 4.5V–5.5V DC
  • Operating current: ~30-33 mA typical
  • Measuring range: 10cm-80cm
  • Output: Analog voltage, inversely (non-linearly) related to distance — requires a lookup table or curve-fit formula to convert to centimeters
  • Update rate: ~16.5ms cycle time (roughly 60Hz)
  • Field of detection: Narrow beam, best for single-point distance measurement rather than wide-area sensing
  • Interface: Single analog output pin, read with analogRead()

Pinout and connection notes

The output voltage-to-distance relationship is non-linear (the sensor is much more sensitive at close range than far range), so most projects use either a published lookup table from the datasheet or an approximate inverse-power curve-fit formula in code to convert the raw analogRead() value into centimeters — a straight linear mapping will give inaccurate results, especially near the extremes of the 10-80cm range. The sensor also reports unreliable/erratic values for objects closer than its minimum 10cm range.

  • VCC: Power, 4.5–5.5V DC
  • GND: Ground
  • Vo (analog output): Analog voltage output — inversely related to distance, needs conversion to a usable distance value

Integration checklist

  1. Confirm the exact part number and breakout-board revision.
  2. Verify supply voltage, current demand, logic levels, and signal direction against the datasheet.
  3. Check controller pin assignments and bus address or interface mode against the example code.
  4. Power up with external loads disconnected, then test signals before full integration.
  5. If readings or communication fail, check power and ground first, then pin mapping, interface settings, and library initialization.

Troubleshooting and reproducibility

Record the board used, wiring, library/version, supply arrangement, and measured behavior. Use the selected board-specific wiring example as the source of truth. Never assume visually similar modules share identical voltage tolerance or pin order.

Board Integration

Library

None — built-in analogRead by —

No library needed; uses core Arduino functions only.

Wiring

Component pinArduino Uno pinNote
Vcc5V
GNDGND
VoA0Analog output, inversely related to distance (not linear).

Swipe sideways to see every column →

C++
  • Output is a non-linear voltage curve, not a straight distance-to-voltage mapping — the formula in the code is an approximation, not a datasheet-exact conversion.
  • Reliable range is roughly 10-80cm; outside that the curve folds back on itself and gives ambiguous readings (near and far distances can produce the same voltage).
  • For anything needing accurate distance, an HC-SR04 (ultrasonic) or VL53L0X (ToF) will serve you better — this sensor is best for simple "something is near/far" logic.
View full Arduino Uno guide