Test and Calibrate Sharp GP2Y0A02YK0F Asian Clone proximity Sensor

The Sharp GP2Y0A02YK0F Proximity Proximity Sensor allows distance measurements between 20cm and 150cm. It is quite easy to get an Asian clone rated A02YK0 for less than €4. There are several Arduino libraries that support different versions of the Sharp sensor, but very often the returned measurement is totally incorrect for A02YK0 clones beyond 70cm. You will also be able to calibrate your sensor if the measurements are too random or not precise enough for your project.


We will see how to manually calibrate the proximity sensor to improve measurement accuracy. We will also see that it is possible to calibrate the sensor to measure a shorter distance below 120mm.

Measurement setup

The method consists in recording the analog signal returned by the sensor as a function of the distance with respect to a reference object (here a simple packaging box). A single meter will be enough.

Sharp GP2Y0A02YK0F montage etalonnage arduino

The Sharp GP2Y0A02YK0F outputs an analog signal proportional to the measured distance. Connect the analog output (yellow wire) to an analog input of the Arduino (A0 for example).

sharp a02yk0 arduino


Authentic Sharp GP2Y0A02YK0F Distance Measuring Sensor
2 Pack GP2Y0A02YK0F Infrared Proximity Sensor Detect 20-150cm Distance Long Range
Buy this item
amazon.com Amazon.com

Choice of the Arduino library

There are several libraries available from the Arduino IDE Library Manager. After having tested them all, I found that the zSharpIR library of zoubworldArduino (GitHub page) gave the best results

libraries sharp GP2Y0A02YK0F ide arduino

Code changes in the ZSharp library

The librarie ZSharp presents two errors that will have to be corrected manually before being able to use it. Open the Arduino -> Librarie -> ZSharp folder. Open the ZSharp.cpp file in a text editor (or the Arduino IDE) and make the following changes:

  • Rename WMath.h to Math.h
  • Comment the analogReadResolution (res) command at the end of the file

Save the changes

Proximity sensor test with ZSharp library

I made a series of measurements according to the distance. I simply measured the distance between the sensor and the wall in my office by varying the distance. To determine each point, I averaged 10 measurements at the frequency of one measurement per second.

Here is a small Arduino program to directly test your sensor using the ZSharp library. The sensor is connected to the A0 analog input of the Arduino or ESP8266.

Modify the model according to your sensor:

  • 1080, GP2Y0A21YK0F (10 to 80 cm)
  • GP2D12_24, 10cm to 80cm
  • 20150, GP2Y0A02YK0F (20 to 150cm)
  • 100500, GP2Y0A710K0F (100cm to 500cm)
  • 430, GP2Y0A41SK0F (or GP2D120, 4cm to 30cm)
#include <ZSharpIR.h>

#define ir A0
#define model 20150       //999 user type
#define DELAY_REFRESH     1000
ZSharpIR SharpIR(ir, model);

void setup() {


void loop() {
  // put your main code here, to run repeatedly:

As you can see on the graph, the sensor, or at least the measurement returned by the ZSharp library, is correct up to 700mm. Beyond that, the measure drifts totally and becomes unusable.

sharp GP2Y0A02YK0F zwharp librarie arduino

Manual Calibration of Sharp GP2Y0A02YK0F

Let’s see now if it is possible to improve things.

Create a new Arduino sketch and paste the following code. It reads and displays on the serial monitor the analog signal returned by the proximity sensor. Vary the distance between an object (a wall for example) and the sensor every 100mm by noting the analog signal each time. Wait a few seconds for the signal to stabilize before proceeding to the next measurement.

void setup() {

void loop() {

Here is the signal reading according to the distance as well as the graphic representation of the latter. As you can see, the signal increases to an asymptote and then decreases again. This can be problematic in some projects. In this case, it would be very risky to use the sensor in a robotic project or autonomous car. It must be certain that the measured distance remains in the measuring range of the sensor.

Distance (mm)Signal

Using a spreadsheet (LibreOffice Calc for example), draw the curve of the distance according to the signal. Double click on the graph to edit it. Click on a point on the curve to add a regression curve.

Select the power type.

calibrate sharp power regression

That is, we now have an equation that allows us to estimate whatever the analog signal is the distance in millimeters that separates the object from the sensor.

sharp GP2Y0A02YK0F calibration libreoffice calc


The measurement error can be estimated by theoretically calculating the distance that the equation returns as a function of the signal. Correct measurement should be obtained up to 1300mm (1.3m). Beyond, the signal is too weak for the equation to be precise.

Distance (mm)SignalEvalDeltaErr
1500701 795295,319,7%
1400801 566166,011,9%
1300901 38888,26,8%
1200951 313113,49,5%
11001121 1109,80,9%
10001161 07170,77,1%

Open ZSharp.cpp again and go right after the line

// Sort it 

Add the following code, replacing the coefficient and the power corresponding to your curve

if (_model==999){
    distanceMM=(int)(138773.464825 * pow(ir_val[0],-1.0233470));

Save and replace the model with 999 in the Arduino code before uploading.

Here is what I got by reproducing the previous tests. As one could sense, beyond 1300mm, the measurement is very random.

sharp GP2Y0A02YK0F calibrated arduino

Here’s what it gives in numbers.

Distance (mm)Sharp A02YK0 (calibrated)DeltaStandart deviation
10001 090,590,5133,90
11001 131,631,6128,11
12001 196,0-4,0127,00
13001 221,7-78,331,91
14001 385,0-15,0123,49
15001 412,3-87,784,00

Calibration test below 200mm

I then had fun to see if it was possible to calibrate the sensor for use below 200mm. The signal is exploitable up to 120mm then collapses.

Sharp GP2Y0A02YK0F calibration etalonnage arduino measuresSharp GP2Y0A02YK0F calibration etalonnage arduino

Here, the sensor is sensitive from 20 to 120mm.

We will do the same as before but this time by choosing a polynomial curve of the second degree

sharp GP2Y0A02YK0F calibration under 200mm


Now we only have to write a small function that we can call each time we want to make a distance measurement with the Sharp sensor. Depending on the spreadsheet used, you can obtain a written equation according to the scientific notation. If you are angry with maths, you just have to shift the comma as many times as the number indicated on the power (here 10-4, so 4).

Then, we will just replace the x2 by x * x in the Arduino code which gives

1.936428309 · 10-4 x2 + 6.987226424 · 10-2 x – 14.32575223

therefore becomes

0.0001936428309 * x * X + 0.06987226424 * x – 14.32575223

Parentheses are not required in the Arduino code.

And that’s the finished function

float getDistanceSharp(){
  int a0 = analogRead(PIN_GP2Y0A02YK0F);
  float dist = 0.0001936428309 * a0 *a0 + 0.06987226424 * a0 - 14.32575223;
  if ( dist < 20 ) {
    dist = 20;
  return dist;

That’s it, now you can integrate this Asian clone of Sharp GP2Y0A02YK0F into your robotic projects. As you have seen, the sensor tested has more limited features than the original version. Announced between 20 and 150cm, the signal becomes difficult to process beyond 130cm. Given the size similar to an ultrasound sensor HC-SR04, the interest of this clone Sharp GP2Y0A02YK0F is quite limited. In the next article, we will compare the Sharp A02YK0 with the ultra-sound HC-SR04 and VL53L0X which determines the distance by measuring the flight time of a laser.

Authentic Sharp GP2Y0A02YK0F Distance Measuring Sensor
2 Pack GP2Y0A02YK0F Infrared Proximity Sensor Detect 20-150cm Distance Long Range
Buy this item
amazon.com Amazon.com


Click to rate this post!
[Total: 0 Average: 0]
We will be happy to hear your thoughts

      Leave a Reply

      DIY Projects
      DIY Projects