Sharp IR Sensor Fluctuation
ChrisL8
Posts: 129
I'm using the Sharp IR Sensors from Parallax with the MCP3208 ADC. I find that when they have something to look at they are very stable and relatively accurate. They don't fluctuate more than a centimeter in either direction as long as there is something within, or even a little outside of, their range.
However, if the Sharp IR sensor is looking out into "open space" the readings fluctuate all over the place, including all of the way up to the highest reading.
The problem is that I cannot tell the difference between a sudden obstacle and these random fluctuations.
If the path is 100% clear and I suddenly get a 14cm reading, is that just my sensor's randomness or a cat*?
I've tried to solve this with code, but the reading for the sensor really is identical between a sudden obstacle and this fluctuation.
I could require "obstacles" to remain in place for a certain period of time before responding to them, but then I risk running over the "cat", or whatever it was that got in the way.
What would cause the Sharp IR sensors to give random near readings when they have nothing to look at, and yet never do this if they have an object in range?
*No cats were harmed during my testing . . . unfortunately.
However, if the Sharp IR sensor is looking out into "open space" the readings fluctuate all over the place, including all of the way up to the highest reading.
The problem is that I cannot tell the difference between a sudden obstacle and these random fluctuations.
If the path is 100% clear and I suddenly get a 14cm reading, is that just my sensor's randomness or a cat*?
I've tried to solve this with code, but the reading for the sensor really is identical between a sudden obstacle and this fluctuation.
I could require "obstacles" to remain in place for a certain period of time before responding to them, but then I risk running over the "cat", or whatever it was that got in the way.
What would cause the Sharp IR sensors to give random near readings when they have nothing to look at, and yet never do this if they have an object in range?
*No cats were harmed during my testing . . . unfortunately.
Comments
How are you looking at the Data? ... Just the Raw output? ...or are you looking at the average?
It might be beneficial to look at the first derivative by looking at the Delta (or change) from one reading to the next, and then perhaps an average of that.
A slow average (several samples) vs. a fast average (a few samples) over the same data set might be an intuitive way to look at the data also by comparing the difference between the two averages. This method has a tendency to migrate over any subtle slow changes that can occur over long periods of time due to temperature, humidity, ambient sun light , etc.
The first column is the reading from the ADC, the second is the voltage calculated from that, and the third is the centimeters calculated from the voltage.
I am certainly open to the idea of using math to weed out the garbage, I'm just not proficient enough to imagine the solution on my own.
I've tried averaging, but averaging garbage results in garbage.
I need a way to say, "If the reading is steady, or trending upward or downward at a reasonable rate, then accept the value, but if it is wildly fluctuating, ignore it."
I can imagine how to only accept stable values:
Take X readings, compare them, if all X are not within Y of each other, discard them.
However, I think that would also throw out values obtained while in motion, since each reading would be higher or lower than the previous.
Here is a new set of output in a darkened room with no artificial lighting and the curtains closed.
It still fluctuates a lot, but it looks different in the graph now.
Still, perhaps this is proving the limits of what I can rely on the IR sensors for.
Here is the results with an object at 49cm:
Lights OFF:
Lights ON:
The results are VERY stable when it has something to look at. Even the florescent light issue is very easy to ignore.
It would be nice if they were steadily "out of range" when there is nothing to look at, instead of fluctuating wildly, causing random hits in the near range.
I thought maybe this was something wrong with my setup, but if it is just the nature of the beast, then I will have to find some formula (math and/or programming function) to deal with it.
It is running off of the Activity Board's 5 volt line. The Activity Board is running off of the Arlo Power distribution board's 6.5V output.
I did find this article that suggests capacitors not only on the power supply line, but perhaps right at the IR sensor's power line and also on the signal line:
http://www.robotroom.com/DistanceSensor3.html
I don't have enough capacitors on hand to experiment with that, but if you think it may make a difference I will go buy some at Radio Shack today and try them.
I had assumed originally that since the reading was so stable when there was something in range, that I had no noise issues, but perhaps I am wrong?
Are you sure you're setting the correct channel for reading? What about the correct mode? If I recall the MCP3208 can be configured for differential mode, which could cause some interesting readings if you're using one channel.
If that doesn't solve your problem, temporarily try a separate clean 5v source for your sensor and see the results.
Good call. Or even simpler, use a battery or two, since the problem only shows up at low input voltages. I haven't tried that particular ADC myself.
What about the input side of the ADC?
I've had issues with bad readings on channels adjacent to floating inputs. How do you have your input wired to the ADC?
This is how I have mine setup
I took your advice and tried running from other power sources and various capacitor placements.
Here is what I found over the weekend:
I. A single Sharp IR Sensor GP2Y0A21YK0F connected to the Activity Board's ADC via the built in breadboard, with no resistors or capacitors, with the Activity Board powered from a computer USB port works perfectly. Virtually no ripple, and a constant low voltage for out of range values and steady values for in range distances.
So the issue seems to be with power, noise and interference.
Here is how I got the IR Sensors working satisfactorily on the ArloBot:
1. Power the MCP3208, the reference voltage, and all of the IR sensors (in other words, all of it) from the 5V output on the Arlo Power Distribution board and not the Activity Board.
This alone made a huge difference.
2. Add a 10uF capacitor across the power input and ground.
1uF was recommended in some diagrams, but those were generic for the MCP3208 usage. The IR Sensor data sheet does actually recommend a 10uF capacitor or larger, so I guess that is just a matter of reading all of the fine print, and knowing enough about things to be looking for it.
This brought things to a very stable level.
However, I found no benefit from adding any capacitor to the sensor signal line. I tried both 0.1uF and 0.047uF capacitors both at the MCP3208 end and right at the sensor itself, and I did not see any benefit to the output either with in range or out of range signals. If anything it seemed to make things worse by making any transient "spikes" last longer.
3. Finally, there was still that intermittent "beat", even with no fluorescent lights. The culprit is the other sensors. I have 5 across the front. Most of the time they cause no problem. However, if one of them is seeing "nothing", and another IS seeing something, at just the right angle, then the one seeing nothing will display a steady low voltage with a periodic spike at regular intervals. This assumes everything is very still, if you are moving obviously this gets very complex.
This is easy to prove when it happens by just covering up the one sensor that sees something and the "beat" goes away immediately and consistently.
Note that this is very dependent on what angle the other sensor sees something at, so it isn't easy to reproduce on purpose.
This is just something to be aware of in programming. I will obviously have to be aware of various IR tendencies in my testing and programming logic, such as how they respond to florescent light, and how they pick up interference from each other.
For background, I did test the IR sensors before mounting them, but I see now what I missed.
1. I tested all 5 sensors on a flat board, but they interfere with each other not when they both look straight ahead, but when one is at an angle to the other and sees something at an angle and the other sees "nothing". This isn't a scenario I had tested, nor is it easy to reproduce on purpose.
2. I tested with the Activity Board on AA battery power. Things are obviously different when running from a voltage converter that in turn comes from a 12v battery.
3. I did a lot of testing for steady values within range, but I didn't pay any attention to out of range values. I had assumed low voltage was low voltage. I had not considered the possibility of noise causing confusing when the path was clear.
Again, thank you for all of the help. I understand all of this a lot better, I learned a lot, and I have some good tools (spreadsheets, formulas, capacitor location ideas and values, pictures of breadboards) to use in future debugging of this and similar systems!
Thank you for all of the time you spend documenting your findings on numerous items here. You're a one-man development tour de force and you have singlehandedly raised the bar for everyone to aspire to your level of detail to help others avoid the pitfalls you discover.
You're an excellent writer and communicator as well. Grammar & spelling are not everyone's strong point, even some very technically bright people I know. I'm guessing you're young and energetic. If you're not a technical writer professionally, you sure could be. Consider writing some articles for SERVO magazine. I have found them to be very receptive, organized, and downright pleasant to work with. They also pay better than average, so it's a great way to subsidize your hobby. Contact Robin Lemieux<robin@nutsvolts.com> and get to work!
Ken, any openings for a skilled tech writer?
Wow, thank you for the encouragement erco! It really means a lot to me. Careful though, this is the kind of praise that will make a man give up his day job!