I'm building a self-navigating robot that uses IR sensor (GP2Y0A21YK0F model) with Arduino to navigate through a terrain, however during testing phase when I put my hand closer than 7cm it bugs and doesn't detect if anything can be closer than that distance as you can see on the screenshot.

Reading image

The code I am using for distance measurement:

#include <SharpIR.h>

// Define model and input pin:
#define IRPin A0
#define model 1080

// Create variable to store the distance:
int distance_cm;

/* Model :
  GP2Y0A02YK0F --> 20150
  GP2Y0A21YK0F --> 1080
  GP2Y0A710K0F --> 100500
  GP2YA41SK0F --> 430

// Create a new instance of the SharpIR class:
SharpIR mySensor = SharpIR(IRPin, model);

void setup() {
  // Begin serial communication at a baudrate of 9600:

void loop() {
  // Get a distance measurement and store it as distance_cm:
  distance_cm = mySensor.distance();

  // Print the measured distance to the serial monitor:
  Serial.print("Mean distance: ");
  Serial.println(" cm");

  • \$\begingroup\$ Please link the data sheet and include a circuit of your interface. That's a circuit and not a bunch of words. Why does your top image jump from 7 cm to 12 cm? \$\endgroup\$
    – Andy aka
    Mar 28 at 10:26
  • \$\begingroup\$ FIrst page of the datasheet says: "Distance measuring range : 10 to 80 cm" so the reason it doesn't work is a limit of the device used. \$\endgroup\$
    – Puffafish
    Mar 28 at 10:31

1 Answer 1


The sensor is rated from 10cm to 80cm; reading the graphs in the datasheet shows that you won't get usable results outside this range.

enter image description here


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.