RESOLVED: need help with sharp GP2Y0D02YK0F

Happy New Year! to all

****** MORE CLEAR/explanation on PAGE 2

******** change pinMode(IRsensor1, INPUT) to pinMode(IRsensor1, INPUT_PULLUP)


I need help in getting proper readings from GP2Y0D02YK0F digital sensor

I connected it to arduino and im geting readings to screen but as I understand the sensor is too quick for Arduino digital read and it has some “false readings” so how I can code to make it more foolproof?

to take average from some readings? found some examples about analog sensor but not for the digital sensor.
Can anyone point to some examples? libraries?

Here is my basic code

// digital pin 2 digital IR sensor connected:
int IRsensor = 2;

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // make the IR sensor pin an input:
  pinMode(IRsensor, INPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input pin:
  int IRState = digitalRead(IRsensor);
  // print out the state of the sensor:
  Serial.println(IRState);
  delay(2);        // delay in between reads for stability
}

As far as I can see from the datasheet that device produces a digital HIGH or LOW depending on whether there is an object within or beyond 80 cm (or maybe only 20cm).

I suggest trying it with digitalRead().

...R

Robin2: As far as I can see from the datasheet that device produces a digital HIGH or LOW depending on whether there is an object within or beyond 80 cm (or maybe only 20cm).

I suggest trying it with digitalRead().

that I already used look my code...

issue is that digital read is too slow and readings have some error like : 0000000000000000(irem in)11111110000111100011111000011110...

so i need code to read let say 10 readings and make average from it and then print it... or what do you think?

ok found the problem (atleast i think) Issue was that the Serial.println() is to slow

changed code little bit and added led to pin 7 to show sensor value and // the serial part

all works but now i dont get print on screen :(

so any thoughts?

/*
  https://www.arduino.cc/en/Reference/DigitalRead

Reads a digital IR sensor input on pin 2,  and
 lights LED on pin 7 if sensor gets positive

 This example code is in the public domain.
 */


//
int IRsensor = 2;//digital IR sensor connected to  digital pin 2
int ledPin = 7; // LED connected to digital pin 13
int val = 0;     // variable to store the read value


void setup() {
// initialize serial communication at 9600 bits per second:
//Serial.begin(9600);

  // make the IR sensor pin an input:
  pinMode(IRsensor, INPUT);
  pinMode(ledPin, OUTPUT);
}

void loop() {
val = digitalRead(IRsensor); //reads IRsensor value and gives it to val
 digitalWrite(ledPin, val); //gives val to ledPin ie. turns led on/of

// print out the state of the sensor:
//delay(0);        // delay in between reads for stability
//Serial.println(val);

}

Serial.begin(9600); ==> Serial.begin(115200);

Could make a difference

tried did not help just made 0 and 1 change faster and like 30- "0" and 10-"1"

Why do you need to print same state again and again? Search de-bouncing library, it takes a button as input, but there is no difference if your sensor outputs only high and low.

issue is that digital read is too slow and readings have some error

No, this is incorrect. According to the datasheet, each measurement takes "38.3 ms ± 9.6 ms", so the Arduino is more than fast enough to keep up, even using 'digitalRead()'.

After each measurement is taken, there's a further "7.6 ± 1.9 ms" delay before the processed data is available, so data always lags the measurement by that amount.

Also, the sensor has inherent hysteresis, to avoid oscillation at the switching point, particularly with slow-moving "targets".

You really need to decide how often you want to read the sensor/update the data, then write your code accordingly. Are you trying to detect a fast-moving object when it hits the 80cm+-10cm threshold?

OldSteve: You really need to decide how often you want to read the sensor/update the data, then write your code accordingly. Are you trying to detect a fast-moving object when it hits the 80cm+-10cm threshold?

yes that i want. trying to build- sumo robot. currently i connected 4 sensors to LEDs and in first it looks like all (except println part) working but the LEDs are kinda flickering very fast and when i use print i see that there is some (errors)

To Magician: Thanks directing me I will look more deeply to that bounce thing. But in first glance it Returns true if the pin state changed (HIGH to LOW or LOW to HIGH). That is all nice, but is it possible to mitigate the false readings? (atleast println showed).

but as im beginner in arduino/electronics/coding all help is appreciated

Tatikas: yes that i want. trying to build- sumo robot. currently i connected 4 sensors to LEDs and in first it looks like all (except println part) working but the LEDs are kinda flickering very fast and when i use print i see that there is some (errors)

To Magician: Thanks directing me I will look more deeply to that bounce thing. But in first glance it Returns true if the pin state changed (HIGH to LOW or LOW to HIGH). That is all nice, but is it possible to mitigate the false readings? (atleast println showed).

but as im beginner in arduino/electronics/coding all help is appreciated

It's hard for us to see what are errors and what are normal results for the movement of the target. As you mave the target closer to the sensor, the output should go high at 70-80cm and stay high as the target gets even closer. Then as you move the target away, the sensor output should go low at 80-90 cm and stay low as the target moves further away. If that isn't happening, and you have followed the datasheet guidelines, either you have a problem with wiringor the sensor is no good. The reading should not flicker between 0 and 1 unless you are moving the target back and forth across the thresholds. It is definitely not a problem related to the Arduino's 'digitalRead()' being too slow.

A couple of points:- 1. Did you fit a minimum 10uF capacitor between the Vcc and ground pins of the sensor, as close as possible to the pins? 2. Are you ensuring that sunlight and ambient artificial lights aren't affecting your readings? 3. Are you sure that the surface you're reflecting the beam from is flat and smooth and directly across the path of the beam? A rough surface, or one at an angle, might cause false readings. (It definitely does with cheaper IR obstacle detection modules like those sold for hobby robotics.) You could try the same tests with a flat, white sheet at 90 degrees to the beam and see if the readings improve, to eliminate that possibility.

thanks for thinking with me,

answering you questions 1) i did not put capacitor- need to buy 10uF capacitors as I have currently only 0,1uF. here i found one article about that and i see that even 0,1uF gives significant benefit. http://www.robotroom.com/DistanceSensor3.html 2) no sunlight tested in dark room 3)the object size dont matter much as it gives same crap if its big gray wall or just a hand.

Im trying to describe situation:

Serial.begin(9600);

when there is obstacle in front of sensor the output to COM is following ....00000110000010000010000011000001000001100000100000100000010.. ......10000011000001000001000000100000100000010000010000010000001... its from arbitrary place not exact beginning (i hope it is correct English terminology) and the led connected to pin 7 is [u]flickering.[/u]

when i put Serial.begin(115200); i get following( same distance: 00000000000000111111111110100000000000000000000000000000000000000000000000000000000000000000011111111111000000000000000000000000000000000000000000000000000000000000000000010111111111110....

and the led is flickering much faster.

when there are no obstacle its outputs constant 00000..

so what is making those 000 when there is obstacle?

In the above linked test there was used analog sensor, so changes to sensor input voltage reduced power supply noise. But I use digital sensor and obstacle is not in max distance, so is it still valid? anyway I will try to get these 10uF capacitors. But what kind 10uF capacitors i should buy?

Tatikas: so what is making those 000 when there is obstacle?

I don't know. Are you sure your connections are good? Can you show a photo of your setup? And despite the fact that you say it makes no difference whether it's your hand or a hard surface, for your testing it's probably best to use a hard, flat surface that's at 90 degrees to the beam, to ensure that the target reflects well. When it's working properly you can then try other surfaces.

In the above linked test there was used analog sensor, so changes to sensor input voltage reduced power supply noise. But I use digital sensor and obstacle is not in max distance, so is it still valid?

Is what still valid? If you mean the suggestion to use a 10uF capacitor across the supply pins, then yes. Their testing shows an 8V spike on the 5V supply lines. That would affect either the digital or analogue output. And keep in mind that the tests that you linked to were done using a different sensor, the Sharp GP2D12, not the GP2Y0D02YK0F.

anyway I will try to get these 10uF capacitors. But what kind 10uF capacitors i should buy?

I'd say that a normal aluminium electrolytic capacitor should be fine, but a tantalum capacitor would probably be even better, since it will have a lower ESR. 10uF 16V should do, though it could have a higher voltage rating or capacitance. The datasheet says ≥10uF, if I remember correctly.

Let's just hope that the capacitor makes a difference.

ok bought these 10uF capacitors and installed on one sensor

all setup is following

distance to wall is 70 cm one sensor with capacitor other without both LEDs are flickering very fast

serial print for both sensors are almost identical. 111000011000011000011100001100001110000110000110000011000011000011100001100001100000110001100001110000110000110000011000011000011100001100001100000110000110000111000011000011000001100001100001110000110000111000011000011

so without serial print non of the LEDs are flickering

Grounds for your sensors and Arduino need to be tied together.

distance to wall is 70 cm one sensor with capacitor other without both LEDs are flickering very fast

so without serial print non of the LEDs are flickering

I don't understand this.

And your pictures are too small for me to see properly - I have bad eyesight. At this point I don't know what to suggest. This simply should not be this hard.

Here is Fritzing i hope its better view |500x363

And code is following

/
int IRsensor1 = 2;//digital IR sensor connected to  digital pin 2
int IRsensor2 = 6;//
int ledPin1 = 7; // LED connected to digital pin 7
int ledPin2 = 10;// LED connected to digital pin 10
int val1 = 0;     // variable to store the read value
int val2 = 0;

void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);

  // make the IR sensor pin an input:
  pinMode(IRsensor1, INPUT);
  pinMode(ledPin1, OUTPUT);
 pinMode(IRsensor2, INPUT);
  pinMode(ledPin2, OUTPUT);}

// the loop routine runs over and over again forever:
void loop() {
val1 = digitalRead(IRsensor1); //reads IRsensor value and gives it to val
val2 = digitalRead(IRsensor2);
 digitalWrite(ledPin1, val1); //gives val to ledPin ie. turns led on/of
digitalWrite(ledPin2, val2); 
// print out the state of the sensor:
//delay(0);
Serial.println(val1);

}

No, fritzing is not really a better view. Just draw what you have on paper and take a photo of that. But even so, what is shown in the fritzing is not the same as what you have in your photo.

Ok to draw on paper i dont know .. im not electronic so i dont have that knowledge

in my photo there was some extra wires and other crap what did not affect the issue as i made new scheme described on Fritzing and still the issue continues better photo here |500x281 picture share

Well, to quote OldSteve:

OldSteve: At this point I don't know what to suggest. This simply should not be this hard.

It should work.

Can you try just driving the LED with the sensor, no Arduino at all (but you could use it for power, I suppose.)

issue is that it works nicely (without flickering) if i dont use Serial.println in code But if i put it in it makes leds to flicker very fast.

As I understand the issue is not in hardware side but kinda connected to Serial.begin(9600) or Serial.println()

or because sensor has one speed and serial other speed?

Like can it be that as sensor has slower speed and between the "pings" of sensor there is no value given to arduino pin so serialprint what is faster reads during that time and gets value "0"?

but as i understand the sensor datasheet it supposed to give constant value if sensor detects obstacle and it does that aslong i dont put serialprin in code. :( confused