proximity
Interfacing GP2Y0A21 with Arduino Uno
What is the GP2Y0A21?
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.
Setting Up GP2Y0A21 with Arduino Uno
Library
None — built-in analogRead by —
No library needed; uses core Arduino functions only.
Wiring
| Component pin | Arduino Uno pin | Note |
|---|---|---|
| Vcc | 5V | |
| GND | GND | |
| Vo | A0 | Analog output, inversely related to distance (not linear). |
Swipe sideways to see every column →
- 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.
Related Components
A3144
Digital Hall-effect switch for contactless magnetic detection
ACS712
Measure AC or DC current with an analog Hall-effect sensor
ADXL345
Low-power 3-axis accelerometer with tap/double-tap and freefall detection for wearables.
IR Obstacle Sensor
Short-range IR proximity sensor with an adjustable threshold — perfect for line-following robots.
Related Guides & Projects
Arduino Uno R4 Minima Pinout: Every GPIO, ADC, DAC, and Op-Amp Pin Explained
A complete Arduino Uno R4 Minima pinout reference — digital I/O, ADC, DAC, op-amp, CAN, dual UART, and exactly what it leaves out compared to the WiFi variant.
Mastering PWM on Arduino: Dimming LEDs and Controlling Motors
Understand what PWM actually is on Arduino, dim an LED smoothly, and safely control a DC motor's speed with a driver chip -- plus the mistakes (like wiring a motor straight to a pin) that damage boards.
4-Digit 7-Segment Counter with TM1637 & Arduino
Drive a TM1637-based 4-digit 7-segment display with Arduino Uno to show a real-time up/down counter, elapsed timer, and scrolling text. Covers library setup, brightness control, and custom segment mapping.
Arduino Uno R4 Minima vs WiFi: Which One Should You Choose?
Same processor, same memory, different price. Here's what the WiFi variant's extra hardware actually gets you, and when the Minima is the smarter buy.