2

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:
  Serial.begin(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.print(distance_cm);
  Serial.println(" cm");

  delay(1000);
}
toolic
  • 5,637
  • 5
  • 20
  • 33
melonade
  • 23
  • 2
  • 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? – Andy aka Mar 28 '23 at 10:26
  • 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. – Puffafish Mar 28 '23 at 10:31

1 Answers1

5

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

jonathanjo
  • 12,049
  • 3
  • 27
  • 60