Sharp Sensor readings spiking.

I have two Sharp GP2D120 sensors hooked up to my arduino, and I have noticed that the readings have random large spikes. I don't know what's causing them, and the spikes occur on both sensors so I don't think it's faulty sensors. At the moment there is nothing else connected to the arduino except for some empty links to a breadboard, as I have yet to connect the other components I wish to use.

I've tried putting a 100uF 10v capacitor linking V and G, like I've seen mentioned in other threads, but it makes absolutely no difference whatsoever. Everything else I've read didn't make a whole lot of sense to me, so if you could make things idiot friendly then I'd appreciate it. :D

Here's a link to my Google Spreadsheet of my readings, taken at 115200 baud

What can I do to get rid of these spikes? I don't know what to do next :(


Try the digitalSmoothing() function

I altered it for my use and I cleaned up my signal.

It's better to simulate floating point math, which I added:

intAvg = total/k; modAvg = total%k;

//this is simulating floating point math //takes the divisor, multiples the modulus by # of samples and divides it by how //many samples it is based on return ((intAvg * filterSamples) + ((modAvg*filterSamples)/(k)));

Tell me this, is the sensor facing towards a light, laptop IR port, or other things that may emit light periodically? If you have a cheap webcam, point towards your laptop IR port, you can see it pulse every so often. Could be the source of the spikes. Put labels on your axes.

I don’t want to hijack this thread. I’ll go ahead and post in the other thread and provide a link here.

My bad, I now realize that this Sharp sensor is actually an IR sensor. I thought you were referring to the sensor I was using. Ugh.

Richard Crowley

  1. I’ve just ordered some 0.1uF capacitors as I don’t have any. I have no capacitors at all other than the 100uF ones, actually. When they arrive, I’ll try this.

  2. I’ve removed the extra wiring, and it hasn’t made any noticible difference (it’s the “100uF Capacitor (new)” sheet). The nearest high-power item would probably be my subwoofer (6ft away) and my A/V reciever (2ft away) so I don’t think they would be the cause. I am on the top floor (not counting the attic) but the nearest airport is 12 miles away. Whether that’s in rage, I haven’t a clue. Also, I have no oscilloscope.

  3. I’d prefer to solve the problem electronically rather than with code (if that makes sense), but if all else fails then I’ll certainly try it :slight_smile:


The digitalSmoothing() function looks like it could be useful (even if you were using it for a different thing after all). I’ll try this after all electronic solutions have been exhausted, same as 3) from Richard.


The sensor is not pointing towards a light, but the lighting in the room is a fluroescent light, so just in case that makes a difference I’ve made another reading with no capacitors and the lights off. I’m pretty sure that my laptop doesn’t have an IR port (either that or it’s very well hidden). My sensors are currently pointing downwards into covered Lekman boxes from IKEA (, so any external IR is probably diffused to insignificance anyway. Finally, I’ve added lables to the axes, but I don’t know what the units are so I just described them.

New information

I did some new readings with the lights off, and I noticed that my newest no-100uF readings had several long dips in the readings where the old no-100uF readings only had one long dip. However, when I re-did the readings with the capacitor it remained dip free. Either something else occured which is causing these dips, or I managed to somehow get a dip-free set of readings with no capacitors the first time. Either way, the problem isn’t there when I add the 100uF capacitors, so it’s not really a problem.

Thanks for the quick replies, sorry for the slow reply myself; I went capacitor hunting but utterly failed at getting to the shops on time. :slight_smile:


My capacitors have arrived, and I have just tried using a 0.1uF capacitor in combination with the 100uF ones I already have. The spikes on my graph have shrunk by about 50% so progress is definitely being made down the electronic side of things :)

I've also got some 0.01uF capacitors, and I have just tried them but they don't seem to have made any difference. In fact, when I tried it with no capacitors again, the spikes stayed at about 50% of the heights I measured at the very beginning. Other than the readings I took with the 0.1uF capacitor, none of these newest readings are on my Google Doc; how do you think I should organise my data into graphs? I'm beginning to get confused as to what's what myself. :/

Although progress appears to have been made, I have no idea what as actually happened. Your insights are greatly appreciated :D