What it is: The Sharp GP2Y0A21YK0F is an analog infrared distance sensor with a 10–80 cm range. It uses triangulation, so it's immune to surface colour and works where ultrasonic struggles (soft, angled targets).
How it works: An IR LED and a position-sensitive detector triangulate the reflection; the sensor outputs an analog voltage that falls as distance grows (a non-linear curve). This sketch converts the voltage to centimetres with the standard power-law fit.
Wiring:
GP2Y0A21 VO -> GPIO0 (analog, yellow wire)
GP2Y0A21 VCC -> 5V (red wire — needs 5V)
GP2Y0A21 GND -> GND (black wire)
OLED VCC=3V3, GND=GND, SDA=GPIO4, SCL=GPIO5
Libraries: only "Adafruit SSD1306" and "Adafruit GFX Library".
On screen / Serial: distance in cm. Move a book toward and away between 10 and 80 cm.
Gotchas: Below ~10 cm the reading folds back and becomes ambiguous — keep targets beyond 10 cm. The output peaks around 3.1 V (safe for the 3.3 V ADC). Readings are noisy; the sketch averages several samples. Use the JST 3-pin cable that comes with the sensor.