# Measuring distance with sharp sensor

**URL:** <https://forum.arduino.cc/t/measuring-distance-with-sharp-sensor/396565>\
**Category:** Sensors\
**Created:** [July 10, 2016, 8:58pm UTC](https://forum.arduino.cc/t/measuring-distance-with-sharp-sensor/396565 "2016-07-10T20:58:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jehgar](https://avatars.discourse-cdn.com/v4/letter/j/b3f665/32.png) [@jehgar](https://forum.arduino.cc/u/jehgar)\
**Post date:** [July 10, 2016, 8:58pm UTC](https://forum.arduino.cc/t/measuring-distance-with-sharp-sensor/396565/1 "2016-07-10T20:58:27Z")

</div>

When I output values of the sharp sensor into the serial monitor the distance only reaches 13 cm even though the the range is supposed to be up to 80cm.

> **[IR distance sensor includes cable (10cm-80cm)](https://www.adafruit.com/product/164)**
>
> This SHARP distance sensor bounces IR off objects to determine how far away they are. It returns an analog voltage that can be used to determine how close the nearest object is. Comes with ...

Any suggestions or ideas on why this may be would be greatly appreciated.

Thanks.

```cpp
#include <Adafruit_NeoPixel.h>
#include <DistanceGP2Y0A21YK.h>

#define PIN 6
#define PIXEL 16

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL, PIN, NEO_GRBW + NEO_KHZ800);

DistanceGP2Y0A21YK Dist;
int distance;
int R = 0;
int G = 0;
int B = 0;

void setup() {
  strip.begin();
  strip.show();
  strip.setBrightness(64);

  Serial.begin(9600);
  Dist.begin(0);
}

void loop()
{
  distance = Dist.getDistanceCentimeter();
  Serial.print("\nDistance in centimers: ");
  Serial.print(distance);
  delay(10); //make it readable

  if (distance > 9)
  {
    for (R && G && B; R < 150 && G < 150 && B < 150; R++ && G++ && B++) {
      for (int i = 0; i < strip.numPixels(); i++) {
        strip.setPixelColor(i, strip.Color(R, G, B));
        strip.show();
        delay(0);
      }
      delay(2);
    }

    // fade off
    for (R && G && B; R > -1 && G > -1 && B > -1; R-- && G-- && B--) {
      for (int j = 0; j < strip.numPixels(); j++) {
        strip.setPixelColor(j, strip.Color(R, G, B));
        strip.show();
        delay(0);
      }
      delay(1);
    }
  }
  else 
  {
    for (int j = 0; j < 256; j++) {
      for (int i = 0; i < strip.numPixels(); i++) {
        strip.setPixelColor(i, strip.Color(255, 0, 0));
        strip.show();
      }
      delay (1);

      //blink off
      for (int j = 64; j > 0; j--) {
        for (int i = 0; i < strip.numPixels(); i++) {
          strip.setPixelColor(i, strip.Color(j, 0, 0));
          strip.show();
        }
        delay (2);
      }
    }
  }
}

```

 ![IMG_2446.jpg](https://europe1.discourse-cdn.com/arduino/original/4X/c/5/4/c545ca8ca41f0e55d82dd395622bb10337c4905f.jpeg)

---

<div class="post-metadata">

**Author:** ![blimpyway](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/blimpyway/32/1100906_2.png) [@blimpyway](https://forum.arduino.cc/u/blimpyway)\
**Post date:** [July 10, 2016, 9:12pm UTC](https://forum.arduino.cc/t/measuring-distance-with-sharp-sensor/396565/2 "2016-07-10T21:12:32Z")

</div>

it seems this sensor uses analog input to read distance.

the begin() call should receive the identifier of the pin used to

instead of 0 (that's the serial RX pin on arduino)

```cpp
Dist.begin(0);

```

should have A0.. A5  
e.g.

```cpp
Dist.begin(A0);

```

---

<div class="post-metadata">

**Author:** ![jehgar](https://avatars.discourse-cdn.com/v4/letter/j/b3f665/32.png) [@jehgar](https://forum.arduino.cc/u/jehgar)\
**Post date:** [July 10, 2016, 10:25pm UTC](https://forum.arduino.cc/t/measuring-distance-with-sharp-sensor/396565/3 "2016-07-10T22:25:34Z")

</div>

I changed the little snippet of code, however I'm still receiving the same issue. After doing a little bit of research I learned that my sensor is model GP2Y0A21YK0F, and the library I'm using seems to only be able to work for GP2Y0A21YK and GP2Y0A02YK. I've been searching for a library to work with my sensor but have had no luck so far. Any more ideas?

---

<div class="post-metadata">

**Author:** ![blimpyway](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/blimpyway/32/1100906_2.png) [@blimpyway](https://forum.arduino.cc/u/blimpyway)\
**Post date:** [July 11, 2016, 8:31am UTC](https://forum.arduino.cc/t/measuring-distance-with-sharp-sensor/396565/4 "2016-07-11T08:31:17Z")

</div>

schema? how you connect everything together, which arduino to which sensor pins, power, ground.

Isolate the problem until you fix it - use a sketch that measure distances, without all that led blinking stuff.

Better yet, since it is an analog sensor (outputs a voltage level to represent measured distance) use only analogRead()

```cpp
void setup() { Serial.begin(9600); }

void loop() {
    Serial.println(analogRead(A0)); 
    delay(500);
}

```

Replace "A0" above with whichever analog input pin you actually connect to sensor's output .. A1, A2, up to A5

EDIT have you looked at datasheets for GP2Y0A21YK0F and GP2Y0A21YK? They look the same to me.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 10:14am UTC](https://forum.arduino.cc/t/measuring-distance-with-sharp-sensor/396565/5 "2021-05-06T10:14:10Z")

</div>

Closed on May 6, 2021, 10:14am UTC


