How to Measure Distances with Arduino and Sharp GP2Y0A02YK0F Sensor

Introduction

Today we learn How to Measure Distances with Arduino and Sharp GP2Y0A02YK0F Sensor, The Sharp GP2Y0A02YK0F sensor is an optical distance sensor designed for measuring distances in the range of 20 to 150 cm. It utilizes infrared technology and triangulation principles to determine the distance between the sensor and objects in its field of view. With its integrated processor and analog output, the sensor is compatible with platforms like Arduino, providing a simple plug-and-play solution for distance sensing applications.

Hardware Required

You will require the following Hardware Components for interfacing the Infrared Distance Sharp GP2Y0A02YK0F Sensor with Arduino.

Components#Buy From Amazon
Arduino UNO1Buy Link
Sharp GP2Y0A02YK0F Sensor1Buy Link
9v DC Adapter (Optional)1Buy Link
Jumper Wires3Buy Link
Breadboard1Buy Link

What is a Sharp GP2Y0A02YK0F sensor?

The sensor emits a beam of pulsed infrared light with a wavelength of 850nm towards the target object. The reflected light is then detected by a position-sensitive detection (PSD) device, essentially a small linear CCD sensor. Triangulation is employed to determine the distance based on the position of the reflected light on the PSD. The analog output voltage varies with distance, ranging from 2.5V at 20 cm to 0.4V at 150 cm. It’s important to note that the response is non-linear, requiring interpolation for precise distance measurements.

How does the Sharp GP2Y0A02YK0F work?

The sensor emits a beam of pulsed infrared light with a wavelength of 850nm towards the target object. The reflected light is then detected by a position-sensitive detection (PSD) device, essentially a small linear CCD sensor. Triangulation is employed to determine the distance based on the position of the reflected light on the PSD. The analog output voltage varies with distance, ranging from 2.5V at 20 cm to 0.4V at 150 cm. It’s important to note that the response is non-linear, requiring interpolation for precise distance measurements.

Pinout

Infrared-Distance-Sharp-GP2Y0A02YK0F-Sensor-Pinout

Pin Configuration

Pin NamePin Type
+5VModule Power Supply – 5V
GNDGround Pin
OutThe output of the Module which is an analog voltage

Circuit Diagram

The following circuit shows you the connection of How to Measure Distances with Arduino and Sharp GP2Y0A02YK0F Sensor, Please make the connection carefully

How-to-Measure-distances-with-Arduino-and-Sharp-GP2Y0A02YK0F-Sensor-Circuit-Connecttion

Installing Arduino IDE Software

First, you will require to Download the updated version of Arduino IDE Software and Install it on your PC or laptop. if you Learn How to install the Arduino step-by-step guide then click on how to install Arduino Button given Blow

Code

The following code Measures the sensor output through Arduino’s analog inputs. Subsequently, it conducts interpolation to derive the distance in centimeters. The results are then displayed on the screen through the serial port. In a practical application, this information would be utilized to trigger essential actions, such as halting a robot or initiating a mechanism.

//For more Projects: www.arduinocircuit.com


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

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

void loop() {
  //lectura de la tensión
  int val = analogRead(sensorPin);
  int mV = (val * referenceMv) / 1023;
  int cm = getDistance(mV);

  
  //mostrar valores por pantalla
  Serial.print(mV);
  Serial.print(",");
  Serial.println(cm);

  delay(1000);
}

//interpolación de la distancia a intervalos de 250mV
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);
  }
}

Applications

  1. Robotics and Navigation: The Sharp GP2Y0A02YK0F sensor is commonly used in robotics for navigation and obstacle avoidance. Its ability to measure medium to long distances accurately makes it suitable for guiding robots through dynamic environments.
  2. Touch-Less Systems: With its precision in distance measurement, the sensor can be employed in touch-less systems, where the distance between a user’s hand or body and a surface is crucial.
  3. Lighting Control and Position Sensing: The sensor’s capability to measure distances within a narrow beam makes it useful in applications such as lighting control, where adjusting light intensity based on the presence or distance of objects is desired.

Leave a Comment


error: