Go Down

Topic: SHARP IR Distance Sensor - GP2D120XJ00F (Read 10499 times) previous topic - next topic


Actually, the measurements of a Sharp IR distance sensor are quite stable over time. If you notice jitter in the readings, it is most probably caused by a not sufficiently stable power supply. As I already remarked, these device draw a lot of current when they are firing. Be sure to have sufficient and good capacitors as close to the sensor as possible (ideally a big one for the large power spike, plus a 10nF one in parallel for the high-frequency components).

One the other hand, due to the principle these sensors work with (triangulation), they have a much greater resolution at short distances than on distances farther away. Very similar to human 3D vision which works best at close distances. Any noise you have in the system is basically magnified (distance-wise) if you are working at the upper edge of the distance range.

Note also that these IR sensors tend to work at distances greater than specified, but these measurements might not be as stable as those taken in the specified range.

In working with Sharp IR distance sensors, it is most important that you get the power supply to the sensors right and that you decouple these sensors sufficiently from the rest of your circuit, especially the Arduino. You might want to compare the readings you get with readings when you power the IR sensor with a separate battery.


Jun 29, 2011, 07:17 pm Last Edit: Jul 01, 2011, 04:28 pm by retrolefty Reason: 1
As already mentioned, a critical thing is to make sure you are measuring a value that is within the standard distance range of the IR sensor.

If you look at the datasheet that shows voltage output Vs distance of sensed object there are two key items to see. First the response is non-linear within the valid mesurement range (therefore a need for a linearizing calculation of the raw value) and second that below the minimum range and above the maximum range one gets 'illogical' and unstable voltage values. So one's program should try and keep track or figure out if the measurement is valid or not before using it.



Thanks for the info guys.
Some more information.

I have linearized the output from the sensor(s).
However, the raw values also fluctuate over time.

The measuring distance (25 cm) should be in range of the sensors capabilities (30 cm).

And I have capacitors in place to stabilize the voltage.
(One big and one small, based on the information in the data sheet)
There are other sensors on the same 5 volt line (flex sensors).
So, if my voltage supply was not stable enough for the IR Rangers,
(or destabilized by these sensors) would that not cause readings for the flex sensors to also jitter?

I was thinking that it maybe had something to do with the frequency at which I read them.
The datasheet http://www.sparkfun.com/datasheets/Sensors/Infrared/GP2D120XJ00F_SS.pdf specifies
a "timing chart". I cannot make much sense out of it, but does this imply I cannot read the voltage all the time?
Does the voltage from the sensor need to stabilize before it's ready to be read again?


If I read the timing chart - page 6 - correctly, a reading (worst case) is available after 38.3 + 9.6 + 5.0 ms = 52.9 ms  => 18.9 times per second.
Rob Tillaart

(Please do not PM for private consultancy)


So, I should only read the sensor 18 times per second at most?


actually, reading of the sensor value should occur at a precise time after you trigger the measurement. See the datasheet on how the voltage varies over time, there's a diagram somewhere. That time is the 38 msec robtillaart mentioned. You should not have any jitter in that timing. And yes, sampling with a higher frequency does not make too much sense, provided your powersupply is clean.


Jul 03, 2011, 04:56 pm Last Edit: Jul 03, 2011, 04:58 pm by Blue_Boy Reason: 1
Could you explain what you mean by "triggering the measurement"?
There's only a power, ground and "voltage to read" line coming out of this sensor.

Maybe the problem is still my power line, though.
I put the capacitors right next to the Arduino, and then wired all my sensors "behind" that.
(IR Rangers and Flex sensors)


oops... - sorry, I mixed up the Sharp IR distance sensors with the Sharp IR dust sensor (the GP2Y1010AU0F for example). The later is being triggered and has to be read out at a specific time after the trigger. Sorry for this confusion....

The Sharp IR distance sensors are indeed outputting the data continously, but that reading does change only at discrete time points.

Back to the issue of power line spikes: I would try to place the capacitors as close as possible to the sensor, not the Arduino. If you google, you will find people soldering a condensator directly to the pins of the IR sensor (for example: http://www.robotfreak.de/blog/en/robotics/how-to-improve-sharp-ir-sensors/450).

You might also try to power sensor and Arduino with different voltage supplies for a test (batteries for example).

Basically, if the power is not stable enough, the noise spikes created by the sensor kick through to the Arduino, especially the Aref voltage which the DAC uses to digitize the analog voltages. If that happens, the readings of the sensor will get noisy, even so the voltage the sensor is delivering is constant.

Time averaging will stabilize the readings, in any case.


Jul 04, 2011, 04:02 pm Last Edit: Jul 04, 2011, 04:09 pm by Blue_Boy Reason: 1
When I just check the RAW values from the sensors,
jitter seems pretty minimal now.
I added an extra capacitor, adding  even more
does not seem to improve the situation.

The sensor response is still not ideal however.
I do a time-averaged read of the sensors,
but improvements are possible.

I guess some smarter filtering could help.

(Analog pin 0 and 1 are connected to the IR rangers)
(The flex sensors a quite stable now)

Code: [Select]

/* PuppetMaster V5 Code                                */
/* Jim Bollansée | jim@jimboproductions.be   */
/* Creative Commons Attribution ShareAlike    */
/* GameHUB 2011                                           */

unsigned long previousMillis;
unsigned long currentMillis;

int numberOfSteps = 0;
int sensorValue[6];
long sensorValueTotal[6];

float tempFloat;

// Set calibrate to 255 to disable calibration
int calibrate = 255;
int sensorTop[] =    {25500, 27000, 510, 530, 530, 480};
int sensorBottom[] = {3500, 3500, 610, 650, 630, 600};

byte dataStart = 0xFF;

void setup()

void loop()
 numberOfSteps ++;
 currentMillis = millis();
   sensorValueTotal[i] = sensorValueTotal[i] + analogRead(i);
 if (currentMillis - previousMillis > 40)
   if (calibrate == 255) Serial.print(dataStart);
     tempFloat = (float) sensorValueTotal[i];
     tempFloat = tempFloat / numberOfSteps;
     sensorValue[i] = (int) tempFloat;
     sensorValueTotal[i] = 0;
     // IR Ranger specifics
     if (i < 2)
       tempFloat = (float) sensorValue[i];
       tempFloat = (2914 / (tempFloat + 5)) - 1;
       tempFloat = tempFloat  * 1000;
       sensorValue[i] = (int) tempFloat;
     if (calibrate == i) Serial.println(sensorValue[i]);
     if (calibrate == 255)
       /// Only map to 254 because 255 is start byte!
       sensorValue[i] = map(sensorValue[i], sensorBottom[i], sensorTop[i], 0, 254);
       sensorValue[i] = constrain(sensorValue[i], 0, 254);
   numberOfSteps = 0;
   previousMillis = millis();


FYI, in my case adding just the capacitor onto the sensor power pins (directly on the sensor, not Arduino) did not reduce the spikes in measured distance readings much. But adding a 10ohm resistor between Arduino +5V power and the capacitor improved things a lot. Here are the standard deviations of measured distance (about 10cm from sensor, measured every 7ms):

plain sensor connected to Arduino +5V/GND: 0.27cm standard deviation, mainly consisting of ca 1cm spikes occuring ca 20 times per sec

with 100uF cap on sensor power pins: 0.15cm

with 100uF cap+10ohm resistor: 0.03cm

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.

Thank you for subscribing!

via Egeo 16
Torino, 10131