URGENT! Problem with Sharp GP2Y0A21YK IR Proximity Sensor Readings

Hi I am new to the arduino world and I am learning it for a class I am currently taking. I am working on a project where I planned to use the Sharp GP2Y0A21YK IR Proximity Sensor to detect objects or movement and according to the different distances to turn diff on colored LEDS. However, my readings on the serial monitor comes out all weird and I have no way of managing it. I uploaded this code that I found

int LEDgreen = 11;

void setup() 
{
  pinMode(LEDgreen, OUTPUT);
  Serial.begin(9600); 
}

void loop() 
{ 
  int sensorValue = analogRead(A0);

  Serial.println(sensorValue);
  delay (100);
  
  if (sensorValue < 500  )
  {
    digitalWrite(LEDgreen, HIGH); 
  }
  else 
  {
    digitalWrite(LEDgreen, LOW);
  }
}

However, the readings I get usually come out like this even without me doing anything to it. It’s just currently sitting on my desk. Even if I place my hand in front of it the readings come out not much different.

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
44
173
328
543
634
706
798
880
948
1004
1023
1023
1023
1023
1023
1003
940
872
806
752
706
667
623
573
497
493
490
482
481
388
0
0
0
0
0
0
0
0
0
0
0
0
0
0
49
214
428
686
1023
1023
1023
1023
1023
1023
1023
1023
1023
954
778
489
112
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

Does anyone have any suggestions how to fix this? I need to get this up running by Monday afternoon so help would be GREATLY appreciated. I don’t have any programming background so this is all rather difficult for me.

shouldn't the A0 be pin zero?

Have you hooked up the ground and power?

Is the sensor in zero?

Yes I hooked up everything according to this bildr Are we getting close? Proximity Sensors + Arduino - bildr. Readings are just all over the place.

did you try the code from that page?

It looks like it would work fine.

IR sensors are messed up by incandescent lighting and direct sun light, or any other IR source, be sure you have no interference.

I did try that code from the page and strangely enough all of a sudden it doesn’t output zeros as sets. I also tried to remove any sort of lighting interference. I even tried it in the dark as I was aiming to use this sensor attached to the front of a pair of glasses to sense objects in the dark.

looks more like this now, but I am still having problems measuring any sort of distance that will allow me to more accurately control 3 different LEDs at different distances:

141
160
188
193
196
219
284
386
452
439
445
478
864
987
726
132
95
259
78
0
0
0
13
0
23
60
106
232
508
708
871
765 < ------------- this is when I put an object near around 50cms
693
629
344
259
177
53
0
0
465
1023
1023 < ------------ this spike shows up after remove the object
1023
962
718
527
349
129
82
42
0
266
1023
990
818
794
881
837
806
764
705
651
587
526
512
464
429
404
397
379
361
340
335
320 < --------------- then slowly progresses back down to around 100-300 when i have nothing in front of it
317
299
303
295
282
276
268
250
248
236
232
219
214
192
194

I’m not sure what the readings should normally look like if it’s working properly…but it just looks very off to me.

I’ve read else where I should add a 100uf capacitor to improve the readings, and I am going to try that tomorrow when I go out to get some. But does anyone have suggestions how I can improve this? I really need to get this to work as well as I possibly can before Monday rolls around as I’m going to be getting graded.

You can do multiple readings (e.g. 32) and average them, should remove some of the noise.

How long is the cabling?

In addition to the cap I've read that you should ground the plastic case (which is apparently quite conductive).

robtillaart: how would I go about doing an average for multiple readings? I currently have the pigtails connected with some wired alligator clips to test. I am not sure if that has been making a difference with my readings.

Chagrin: I'm not sure what you mean by grounding the plastic case. I haven't read that anywhere yet.

I went out trying to find 100uf capacitor but unfortunately both the radioshack and fry's near me are out so as of now that's not an option anymore

chuyin:
Chagrin: I'm not sure what you mean by grounding the plastic case. I haven't read that anywhere yet.

From 404 (Page Not Found) Error - Ever feel like you're in the wrong place?

The case of the sensors is a conductive plastic material. Don't believe me, check it out with an ohm meter. To insure reliable, noise free measurements, make sure you ground the case. If the case is not grounded and there is no target, or the return signal is weak, the sensor sometimes "thinks" there is a target and that it is wandering around.

Averaging …

void loop() 
{ 
  int sensorValue = 0;
  for (int i=0; i< 32; i++) sensorValue += analogRead(A0);
  sensorValue /= 32;

  Serial.println(sensorValue);
  delay(1000);
}

or

int sensorValue = 0;

void loop() 
{ 
  sensorValue = (analogRead(A0) + 15 * sensorValue) / 16;   // 1x new and 15 times old makes 16 readings)

  Serial.println(sensorValue);
  delay(1000);
}