Sharp GP2Y0A21YK0F Analog Distance Sensor 10-80cm

DESCRIPTION

28995
The GP2Y0A21 Sharp distance sensor is a great way to add obstacle avoidance or motion sensing to your robot or any other project. With a detection range of 4″ to 32″ (10 cm to 80 cm) and an analog voltage indicating the distance, this sensor is very easy to use.

0J1466.200

The Sharp distance sensors are a popular choice for many projects that require accurate distance measurements. This IR sensor is more economical than sonar rangefinders, yet it provides much better performance than other IR alternatives. Interfacing to most microcontrollers is straightforward: the single analog output can be connected to an analog-to-digital converter for taking distance measurements, or the output can be connected to a comparator for threshold detection. The detection range of this version is approximately 10 cm to 80 cm (4″ to 32″).
The GP2Y0A21 uses a 3-pin JST PH connector that works with our 3-pin JST PH cables for Sharp distance sensors (not included), as shown in the upper picture on the right. These cables have 3-pin JST connectors on one end and are available with pre-crimped male pins, pre-crimped female pins, and with unterminated wires on the other end. It is also possible to solder three wires to the sensor where the connector pins are mounted (see the lower picture to the right). When looking at the back, the three connections from left to right are power, ground, and the output signal.

We also carry an assortment of mounting brackets designed specifically for this sensor:
0J5937.175
0J5945.175
0J5934.175
0J5938.450
Family of brackets for Sharp GP2Y0A02, GP2Y0A21, and GP2Y0A41 Distance Sensors.

Feature

  1. Operating voltage: 4.5 V to 5.5 V
  2. Average current consumption: 30 mA (note: this sensor draws current in large, short bursts, and the manufacturer recommends putting a 10 µF capacitor or larger across power and ground close to the sensor to stabilize the power supply line)
  3. Distance measuring range: 10 cm to 80 cm (4″ to 32″)
  4. Output type: analog voltage
  5. Output voltage differential over distance range: 1.9 V (typical)
  6. Update period: 38 ± 10 ms
  7. Size: 44.5 mm × 18.9 mm × 13.5 mm (1.75″ × 0.75″ × 0.53″)
  8. Weight: 3.5 g (0.12 oz)

Linearizing the output

0J387.300
The relationship between the sensor’s output voltage and the inverse of the measured distance is approximately linear over the sensor’s usable range. The GP2Y0A21YK datasheet (374k pdf) contains a plot of analog output voltage as a function of the inverse of distance to a reflective object. You can use this plot to convert the sensor output voltage to an approximate distance by constructing a best-fit line that relates the inverse of the output voltage (V) to distance (cm). In its simplest form, the linearizing equation can be that the distance to the reflective object is approximately equal to a constant scale factor (~27 Vcm) divided by the sensor’s output voltage. Adding a constant distance offset and modifying the scale factor can improve the fit of this line.

Alternative Sharp distance sensors
We have a variety of Sharp distance sensors to choose from, including the shorter-range (4 – 30 cm) GP2Y0A41SK0Fand longer-range (20 – 150 cm) GP2Y0A02YK0F. These analog distance sensors have similar packages and identical pin-outs, making it easy to swap one version for another should your application requirements change. We also carry the newer Sharp GP2Y0A60SZ analog distance sensor (10 – 150 cm), which outperforms the other analog Sharp distance sensors in almost all respects, offering a low minimum detection distance, high maximum detection distance, wide 3 V output voltage differential, high 60 Hz sampling rate, operation down to 2.7 V, and optional enable control, all in a smaller package.
0J5788.470
Sharp GP2Y0A02YK0F Sensor 20-150cm (left) next to Pololu Carrier with Sharp GP2Y0A60SZLF Sensor 10-150cm (right).

For very short-range detection applications, we carry a 2-15 cm analog Sharp distance sensor. We also carry three digital Sharp distance sensors that have low minimum detection distances and a high 400 Hz update rate. They are available with a 5 cm, 10 cm, or 15 cm range and simply tell you if something is in their detection range, not how far away it is. They are smaller than the analog Sharp distance sensors and draw less current.
0J6051.600
A variety of Sharp distance sensors. From left to right: GP2Y0A02, GP2Y0A21 orGP2Y0A41, GP2Y0A51, and GP2Y0D8xx.

Note: The GP2Y0A21YK0F model is a lead-free, RoHS-compliant version of the Sharp GP2Y0A21 Distance Sensor. The manufacturer recommends you insert a bypass capacitor of 10 µF or more between Vcc and GND near this sensor to stabilize your power supply line.

GP2Y0A21YK0F datasheet (374k pdf)

Arduino library for Sharp distance sensors

Sharp IR Sensor (GP2Y0A41SK0F) - Arduino Tutorial

FFRQYMCIOOM1ZM5.MEDIUM
In this tutorial we will use the Sharp IR sensor (GP2Y0A41SK0F) to measure distance from an object.
IR Sensors work by using a specific light sensor to detect a select light wavelength in the Infra-Red (IR) spectrum. By using an LED which produces light at the same wavelength as what the sensor is looking for, you can look at the intensity of the received light. When an object is close to the sensor, the light from the LED bounces off the object and into the light sensor. This results in a large jump in the intensity, which we already know can be detected using a threshold.
Since the sensor works by looking for reflected light, it is possible to have a sensor that can return the value of the reflected light. This type of sensor can then be used to measure how "bright" the object is. This is useful for tasks like line tracking.
In this tutorial we will try to measure the distance from an object (4~30cm).
Let's get started!

Step 1: What You Will Need
For this tutorial you will need:

  1. Arduino uno
  2. Sharp IR Sensor (model GP2Y0A41SK0F)
  3. breadboard cables

The code will only work for model GP2Y0A41SK0F

Step 2: The Circuit
FFY8D0NIOOM20UV.MEDIUM
The connections are pretty easy, see the above image with the breadboard circuit schematic.

Step 3: The Code Using Codebender
Here's the code, embedded using Codebender!
Try downloading the Codebender plugin and clicking on the "Run on Arduino" button to program your Arduino board. And that's it, you've programmed your Arduino with this sketch!

// Sharp IR GP2Y0A41SK0F Distance Test
// http://tinkcore.com/sharp-ir-gp2y0a41-skf/
#define sensor A0 // Sharp IR GP2Y0A41SK0F (4-30cm, analog)
  void setup() {
  Serial.begin(9600); // start the serial port
}
void loop() {
    // 5v
  float volts = analogRead(sensor)*0.0048828125;  // value from sensor * (5/1024)
  int distance = 13*pow(volts, -1); // worked out from datasheet graph
  delay(1000); // slow down serial port 
    if (distance <= 30){
    Serial.println(distance);   // print the distance
  }
}

To program your Arduino from your browser, install the codebender plugin or app. Learn more.

You can keep playing with that by clicking the "Edit" button and start making your own modifications to the code.

Step 4: Serial Monitor - Testing
Press connect button on serial monitor window to start serial communication.

Step 5: Well Done!
You have successfully completed one more "How to" tutorial and you learned how to use the Sharp IR sensor with Arduino.