

The GP2Y0A21YK0F is an infrared distance sensor capable of measuring distances between 10 cm and 80 cm. It operates by emitting infrared light through an LED and detecting the reflected light using a phototransistor. The sensor outputs an analog voltage that corresponds to the distance of the detected object, making it ideal for applications requiring non-contact distance measurement.








Below are the key technical details of the GP2Y0A21YK0F sensor:
| Parameter | Value |
|---|---|
| Operating Voltage | 4.5 V to 5.5 V |
| Average Current Consumption | 30 mA (typical) |
| Output Voltage Range | 0.4 V to 2.8 V (analog) |
| Measuring Range | 10 cm to 80 cm |
| Response Time | 38 ms |
| Operating Temperature Range | -10°C to +60°C |
| Dimensions | 29.5 mm × 13 mm × 21.6 mm |
The GP2Y0A21YK0F has a 3-pin JST connector. The pinout is as follows:
| Pin | Name | Description |
|---|---|---|
| 1 | VCC | Power supply input (4.5 V to 5.5 V) |
| 2 | GND | Ground connection |
| 3 | VOUT | Analog voltage output proportional to distance |
Below is an example of how to use the GP2Y0A21YK0F with an Arduino UNO to measure distance:
// Define the analog pin connected to the sensor's VOUT pin
const int sensorPin = A0;
void setup() {
Serial.begin(9600); // Initialize serial communication at 9600 baud
}
void loop() {
int sensorValue = analogRead(sensorPin); // Read the analog value from the sensor
float voltage = sensorValue * (5.0 / 1023.0); // Convert the analog value to voltage
// Approximate distance calculation based on sensor's characteristics
float distance = 27.86 / (voltage - 0.42); // Example formula for distance in cm
// Print the distance to the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(100); // Wait for 100 ms before the next reading
}
Note: The formula used in the code is an approximation. For more accurate results, refer to the sensor's datasheet and use a calibration curve.
No Output or Incorrect Readings
Fluctuating Output
Inaccurate Distance Measurements
Interference from Ambient Light
Q1: Can the GP2Y0A21YK0F detect transparent objects?
A1: No, the sensor may not reliably detect transparent or highly reflective objects due to insufficient reflected infrared light.
Q2: How do I improve the accuracy of distance measurements?
A2: Use a calibration curve specific to your application and environment. Avoid using the sensor in extreme lighting conditions.
Q3: Can I use this sensor with a 3.3 V microcontroller?
A3: The sensor requires a 5 V power supply, but the output voltage can be read by a 3.3 V microcontroller if the analog input pin supports it. Use a level shifter if necessary.
Q4: What is the response time of the sensor?
A4: The sensor has a response time of approximately 38 ms, making it suitable for real-time applications.