Language: EN

arduino-sharp-gp2y0a02yk0f

Measuring distances with Arduino and Sharp GP2Y0A02YK0F sensor

What is a Sharp GP2Y0A02YK0F sensor?

The Sharp GP2Y0A02YK0F is an optical distance sensor. It consists of an infrared LED along with a position-sensitive device (PSD) and an integrated processor responsible for distance calculation.

The sensor continuously scans objects located in front of it and provides output using an analog voltage reference, so we can use the analog inputs of Arduino to read the distance value.

The measurement range is from 20 to 150 cm, maintaining a high degree of precision throughout the interval. The supply voltage is 4.5 to 5.5V and the current consumption is 33mA. The refresh interval between measurements is about 80ms.

The GP2Y0A02YK0F sensor is easy to connect and use. However, it should be noted that it incorporates a JST (Japanese Solderless Terminal) connector for its connection, so we will have to use a connector of this type or solder the terminals directly to the board.

The sensor is not very sensitive to the color and reflectivity of the detected object, nor to the ambient temperature. However, it can be affected by ambient light if it receives a high degree of direct luminosity, especially when used outdoors. It can also provide incorrect measurements when detecting transparent or very bright objects, especially glass and mirrors.

The Sharp sensor is more accurate than ultrasonic sensors like the HC-SR04, especially at medium and long distances, where ultrasonic sensors are affected by the rebounds and echoes produced by the geometries of the environment.

However, it cannot operate at short distances (<20cm) where the HC-SR04 can at least make measurements with low precision.

When mounting these sensors on a vehicle or robot, try to mount them on the rear of the vehicle even though the first tendency is to mount them on the front. This way, you gain the width of the vehicle as distance, and you can measure shorter distances.

On the other hand, the Sharp sensor has a narrower measurement area than the HC-SR04, which allows for greater measurements in front of the object. On the other hand, it may ignore objects that the HC-SR04 would detect due to its wider measurement angle (for example, table legs).

Finally, when using more than one sensor, interferences can occur between measurements if both beams interfere. However, the problem is much less than when combining HC-SR04 sensors, where we can have interferences even without the beams interfering, due to the rebounds and secondary echoes generated by the HC-SR04 itself.

We can use the GP2Y0A02YK0F in our robots for distance measurements in medium and long ranges in a narrow beam. Given its different characteristics, we can combine them with ultrasonic sensors like HC-SR04 for short or wide ranges, or obstacle detection sensors for very short distances.

The Sharp sensor can also be used in other types of applications such as touch-less systems, lighting control, position sensors, limit switches, or object detection in a specific area.

Price

The Sharp GP2Y0A02YK0F sensor is more expensive than an HC-SR04 ultrasonic sensor. We can find Sharp GP2Y0A02YK0F sensors for 3.70€ from international sellers on eBay or AliExpress.

arduino-sharp-gp2y0a02yk0f1-componente

How does the Sharp GP2Y0A02YK0F work?

The infrared LED emitter emits a pulsed infrared light beam with a wavelength of 850nm +/-70nm. Pulsed light is sent to minimize the influence of ambient light and the color of the object on the measurement.

The position-sensitive detection (PSD) detector is actually a small linear CCD sensor that receives the light reflected from any object in the path of the beam. The sensor uses triangulation to determine the distance from the sensor to the objects located in front of the beam.

arduino-sharp-gp2y0a02yk0f1-funcionamiento

The analog output has a value of 2.5V at 20 cm, and 0.4 at 150cm. However, as we have mentioned, the response is non-linear, so it is necessary to interpolate the value to obtain an adequate level of precision.

arduino-sharp-GP2Y0A02YK0F1-precision

Assembly diagram

The assembly diagram is simple. On one side, we power the sensor using Vcc and GND, connecting them, respectively, to 5V and GND of Arduino.

arduino-sharp-gp2y0a02yk0f1-esquema

Finally, we connect the signal pin to one of the analog inputs of Arduino.

arduino-sharp-GP2Y0A02YK0F1-conexion

Code examples

The following code measures the output of the sensor using the analog inputs of Arduino. Then, it interpolates to obtain the distance in centimeters.

Finally, it displays the values on the screen using the serial port. In a real setup, we would use this area to perform the necessary actions, such as stopping a robot or activating a mechanism.

const int sensorPin = A0;
const long referenceMv = 5000;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  //reading the voltage
  int val = analogRead(sensorPin);
  int mV = (val * referenceMv) / 1023;
  int cm = getDistance(mV);

  
  //display values on the screen
  Serial.print(mV);
  Serial.print(",");
  Serial.println(cm);

  delay(1000);
}

//interpolation of distance at 250mV intervals
const int TABLE_ENTRIES = 12;
const int INTERVAL  = 250;
static int distance[TABLE_ENTRIES] = {150,140,130,100,60,50,40,35,30,25,20,15};

int getDistance(int mV) {
  if (mV > INTERVAL * TABLE_ENTRIES - 1)      return distance[TABLE_ENTRIES - 1];
  else {
    int index = mV / INTERVAL;
    float frac = (mV % 250) / (float)INTERVAL;
    return distance[index] - ((distance[index] - distance[index + 1]) * frac);
  }
}

Download the code

All the code from this post is available for download on Github. github-full