This part has a page of its own, with a note on using it and a circuit to try: the part page.
The default is the GP2Y0A21YK0F, 10 to 80 cm; the model setting also offers the
long-range GP2Y0A02YK0F (20 to 150 cm), the short-range GP2Y0A41SK0F (4 to 30 cm) and
the very long-range GP2Y0A710K0F (100 to 550 cm). Aliases: GP2Y0A21YK0F, GP2Y0A02YK0F,
GP2Y0A41SK0F, GP2Y0A710K0F, Sharp IR sensor.
An infrared LED behind one lens throws a spot, and a detector behind the other sees where the reflected spot lands, which moves with the distance. So the color of the object hardly matters, and the output is a distance, not a yes or no.
Pins
| Pin | What it does |
|---|---|
VO |
The output voltage, the yellow wire. |
GND |
Ground, the black wire. |
VCC |
Supply, 4.5 V to 5.5 V, the red wire. |
Properties
model is which Sharp sensor it is: GP2Y0A21YK0F (the default), GP2Y0A02YK0F,
GP2Y0A41SK0F or GP2Y0A710K0F.
distance is how far away the object starts, in centimeters, from 1 to 150 on the
GP2Y0A21YK0F, 200 on the GP2Y0A02YK0F, 50 on the GP2Y0A41SK0F and 600 on the
GP2Y0A710K0F.
While it runs
The slider is the distance to the thing in front of the sensor. Its scale follows the model.
What the model gets right
From each model's Sharp datasheet:
| Model | Range | Typical output | Measures every |
|---|---|---|---|
| GP2Y0A21YK0F | 10 to 80 cm | 0.4 V at 80 cm, 2.3 V at 10 | 38.3 ms |
| GP2Y0A02YK0F | 20 to 150 cm | 0.4 V at 150 cm, 2.45 V at 20 | 38.3 ms |
| GP2Y0A41SK0F | 4 to 30 cm | 0.4 V at 30 cm, 2.65 V at 4 | 16.5 ms |
| GP2Y0A710K0F | 100 to 550 cm | 2.5 V at 100 cm, 1.8 V at 200, about 1.35 V at 550 | 16.5 ms |
The curve. The output falls as the inverse of the distance. The model is the
inverse curve through each datasheet's two typical points: 33.9 / (d + 4.74) volts
for the GP2Y0A21YK0F, 62.15 / (d + 5.37) for the GP2Y0A02YK0F and
12.25 / (d + 0.622) for the GP2Y0A41SK0F. The GP2Y0A710K0F's line does not pass
through zero, so it is 1.1 + 140 / d, which meets all three of its typical figures.
Too close. Under the near end the output rises a little and then falls again, so an object too close reads as one further away. On the GP2Y0A21YK0F 5 cm gives about what 25 cm does; the model keeps that trap, though not the exact shape under 10 cm. The other three follow the points of their datasheet's graph: the GP2Y0A41SK0F peaks at 3.04 V at 3 cm, the GP2Y0A02YK0F at 2.75 V at 15 cm, and the GP2Y0A710K0F at 3.09 V at 50 to 60 cm, falling to 1.55 V at 10 cm.
Timing. A new measurement every 38.3 or 16.5 ms by model. The output holds between them, and right after power-up it reads nothing until the first one.
What it does not model
The spread between parts (the far point may be 0.25 to 0.55 V, so calibrate a real one), the small effect of very dark or shiny surfaces, sunlight, the spread of the measuring cycle, and the current spikes of the LED, which is why the datasheets ask for a capacitor across the supply. Under 10 cm the GP2Y0A710K0F's graph stops; the model falls straight to 0 V there.
Libraries
SharpIR (SharpIR.h) turns the reading into centimeters:
SharpIR sensor(SharpIR::GP2Y0A21YK0F, A0); sensor.getDistance();. It knows the
GP2Y0A41SK0F, GP2Y0A21YK0F and GP2Y0A02YK0F; set the part's model to the one named
in the sketch. Outside the range it answers one centimeter past the end (9 or 81 on
the GP2Y0A21YK0F), as the real library does. It has no GP2Y0A710K0F; read that one
with analogRead() and the curve above.
Common mistakes
Holding the hand closer than the near end and reading "far". Mount it so nothing can come that close, or back it off behind the edge of the robot.
Naming one model in the sketch and wiring another: the library inverts the curve of the model it is told, so the centimeters come out wrong.
See it in action
Distance rainbow lights a WS2812 stick from green to red as a hand comes closer. Open it at /templates.
Related
- IR distance sensor, Sharp GP2Y0A21: the part page, with the pinout and key facts.