Using Sharp IR GP2Y0A51SK0F Distance Sensor with Arduino (2cm to 15cm)

Project Video
Project Details

In this tutorial we learn how to use Sharp IR GP2Y0A51SK0F distance meter to measure distance from 2cm to 15cm. Code is provided for multiple sensor.

Project Code

This is Arduino code to measure the distnace using Sharp Infrared Distance
sensor GP2Y0A51SK0F to measure from 2 to 14cm

See datasheet
 * Watch Video instrution for this code:

 * Written by Ahmad Shamshiri on July 09 2020 at in Ajax, Ontario, Canada
 * in Ajax, Ontario, Canada.

 * Get this code and other Arduino codes from
Learn Arduino step by step in structured course with all material, wiring diagram and library
all in once place. Purchase My course on

If you found this tutorial helpful, please support me so I can continue creating 
content like this. You can support me on Patreon

or make donation using PayPal
Related videos:
Introduction to MAX6675 K-Type:
Using MAX6675 K-Type thermocouple whit LED display:
Using MAX6675 K-Type thermocouplewith LCD1602-I2C:
Using 2 or more MAX6675 K-Type thermocouple:
Using MAX6675 K-Type thermocouple as Heater or Coller controller: [this video]
 *  * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.* 
 * This code has been download from
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see .


//33.9 + -69.5x + 62.3x^2 + -25.4x^3 + 3.83x^4

const int sensorPin[] = {A0};
float distance[1];//watch video for details
const int AVERAGE_OF =50;
const float MCU_VOLTAGE = 5.0;

void setup()
 // code for sharp IR sensor 
  Serial.println("Robojax Sharp GP2Y0A51SK0F demo");
 // code for sharp IR sensor 
}//setup ends here

void loop(){
   // code for sharp IR sensor 
   readDistance(0);//read sensor 1
   // readDistance(1);//read sensor 2
   if(distance[0] >8.5)
    //if distance is greator than 8.5 do something here

  // print out the value you read:
  Serial.print("d1 =");// 
 // code for sharp IR sensor 
}//loop end

 *  readDistance()
 * @brief reads the the distance from sharp sensor
 * and updates the apropriate sensor's value
 * @param "sensor" is integer

 * @return none
 * Written by Ahmad Shamshiri for
 * on July 09, 2020 in Ajax, Ontario, Canada
void readDistance(int sensor)
  // code for sharp IR sensor 
      float voltage_temp_average=0;
      for(int i=0; i < AVERAGE_OF; i++)
      int sensorValue = analogRead(sensorPin[sensor] );
      voltage_temp_average +=sensorValue * (MCU_VOLTAGE / 1023.0);

     voltage_temp_average /= AVERAGE_OF;

  // eqution of the fitting curve
  ////33.9 + -69.5x + 62.3x^2 + -25.4x^3 + 3.83x^4
  distance[sensor] = 33.9 + -69.5*(voltage_temp_average) + 62.3*pow(voltage_temp_average,2) + -25.4*pow(voltage_temp_average,3) + 3.83*pow(voltage_temp_average,4);
     // code for sharp IR sensor