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
- Confirm the exact part number and breakout-board revision.
- Verify supply voltage, current demand, logic levels, and signal direction against the datasheet.
- Check controller pin assignments and bus address or interface mode against the example code.
- Power up with external loads disconnected, then test signals before full integration.
- 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.