Sharp GP2D120 IR Sensor

HI All,

When I connect a Sharp GP2D120 IR Sensor to my board and read it through the serial port the values seem to spike randomly.
They will float a bit from 60 to 70 then spike to 113. Below is the code:

analogValue1 = analogRead(analogPin1);
analogValue1 = analogValue1 / 4;
Serial.println(analogValue1, DEC);
delay(10);

I’m trying to set a range, so that when your in-between, a pin will be set high or low. If it spikes…this doesn’t work…

// turn output 1 on/off

if (analogValue1 > 85 && analogValue1 < 135)
{
digitalWrite(ledPin1, HIGH);
}
else
{
digitalWrite(ledPin1, LOW);
}

Any help will be appreciated!

BILL

hi

this code slows things down considerably... try taking out the delay and using 115200 for serial speed. :

  Serial.println(analogValue1, DEC);
  delay(10);

hello

what I tend to do is average the reading from the sensor a bit to smooth out small variations

i sum the reading 10 times and divide by 10

works pretty well

hi

I just meant to point out that the delay() function and the Serial.print function take perhaps 12ms to execute, during which time you won't get any readings.. so the processor is taking readings for 10 or 20 microseconds, then sending serial for a couple milliseconds, and then nothing for 10milliseconds. Eliminating the delay() gets the readings to you without a processor delay.

D

I believe that adding a 100uf cap over the power helps. GND----100uf-------5v | | | | | | | A G V

Hi everybody i have some problems with the sharp gp2d120. I can t find a code that can return me a value in centimeters from the sensor s reading. I ve looked in the datasheet but again i can t understand how to obtain a good measure because for example

2.2v ------- 2 cm or 7 cm :'(