Sharp IR Sensor - GP2Y0A21YK0F - Just getting started with Arduino

Hello,

I am very new to Arduino, but have done the projects in this package (Starter Kit)

and I also purchased this (Sharp IR sensor)

http://www.amazon.com/gp/product/B00IMOSEJA/ref=oh_details_o04_s00_i00?ie=UTF8&psc=1

I am trying to test the sensor I received and I am using the code below which I found here at the Arduino forums. When I have the sensor hooked up to AO and open the serial monitor, it returns the following values no matter where I place the sensor. Does anyone have any help they can offer? Thank you!

Results returned every time.
mean distance = 10 cm
time = 14 ms

Code
#include <SharpIR.h>

#define ir A0
#define model 1080

boolean done=false;

SharpIR sharp(0, 25, 93, 1080);

// ir: the pin where your sensor is attached
// 25: the number of readings the library will make before calculating a mean distance
// 93: the difference between two consecutive measurements to be taken as valid
// model: an int that determines your sensor: 1080 for GP2Y0A21Y
// 20150 for GP2Y0A02Y
// (working distance range according to the datasheets)

void setup(){

Serial.begin(9600);
pinMode (ir, INPUT);

}

void loop(){

delay(10000); // it gives you time to open the serial monitor after you upload the sketch

if (done==false){ // it only runs the loop once

unsigned long pepe1=millis(); // takes the time before the loop on the library begins

int dis=sharp.distance(); // this returns the distance to the object you're measuring

Serial.print("Mean distance: "); // returns it to the serial monitor
Serial.println(dis);

unsigned long pepe2=millis()-pepe1; // the following gives you the time taken to get the measurement
Serial.print("Time taken (ms): ");
Serial.println(pepe2);

done=true;

}

}

Your sensor is not attached on pin 0, as it is in your code..

Try this one and let me know what happens ...

#include <SharpIR.h>

#define ir A0
#define model 1080

boolean done=false;

SharpIR sharp(A0, 25, 93, 1080);

// ir: the pin where your sensor is attached
// 25: the number of readings the library will make before calculating a mean distance
// 93: the difference between two consecutive measurements to be taken as valid
// model: an int that determines your sensor:  1080 for GP2Y0A21Y
//                                            20150 for GP2Y0A02Y
//                                            (working distance range according to the datasheets)

void setup(){
 
  Serial.begin(9600);
  pinMode (ir, INPUT);
 
}

void loop(){

  delay(10000);    // it gives you time to open the serial monitor after you upload the sketch
 
 if (done==false){  // it only runs the loop once
 
  unsigned long pepe1=millis();  // takes the time before the loop on the library begins
 
  int dis=sharp.distance();  // this returns the distance to the object you're measuring


  Serial.print("Mean distance: ");  // returns it to the serial monitor
  Serial.println(dis);
 
  unsigned long pepe2=millis()-pepe1;  // the following gives you the time taken to get the measurement
  Serial.print("Time taken (ms): ");
  Serial.println(pepe2); 

done=true;
 
}

}
  1. If you want something to run only once put it in setup() and leave loop() empty, Simple!.

  2. if you want to see what a sensor does under different circumstances put the code in loop() and let it loop. use a delay(1000) at the end of loop() so that you don't get to much data in the serial monitor.

  3. the sharp IR sensor use light, so there is no point in trying to "time" its responses - it just to fast!

  4. drop the lib and class, is over kill - you read the sensor using analogRead(pin) the result can be used directly (see the graph in the datasheet) or you * by a magic nimber to get inches, cm, cuibits,spans or what ever!.

  5. To see how it works don't change it position, put your hand or a bit of paper in front of it and then move your hand slowly you should see the reading change.

  6. The wiring is gnd to gnd, power to +5v and the other wire (normal white) to an analog input eg A0.

So your code should be

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly: 
  Serial.println(analogRead(A0));
  delay(1000);
}

Mark

Zaxarias: I tried to use you code (I noticed the change from 0 to A0). I am still having the same problem.

I will try to rewrite this code and see if I can get it to work.

Holmes,

I tried using your method and it just gives a bunch of numbers that don't seem to bear any resemblance to an actual distance.

This was the code I used Holmes. It seems to vary slightly (both numbers), but I am trying to move it from approximately 20 cm to 50 cm back and forth and it is not registering.

#include <SharpIR.h>

#define ir A0
#define model 1080

SharpIR sharp(A0, 25, 93, 1080);

void setup() {
// put your setup code here, to run once:
pinMode (ir, INPUT);
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:
int dis=sharp.distance(); // this returns the distance to the object you're measuring

Serial.print("Mean distance: "); // returns it to the serial monitor
Serial.println(dis);

Serial.print("Holmes4 number: "); // Holmes output
Serial.println(analogRead(A0));
delay(1000);
}

So what values did you get?

At 8cm it should read just over 600 and at 60cm just over 100 by my method.

Mark