I am having way too much fun with my Uno starter kit. Have worked my way up to controlling the servo provided in the kit, first with a pot as shown in the exercises, then with a phototransistor for grins.
One thing I notice with all the analog inputs I have tried so far -- which is a grand total of 2, the thermo sensor and the photo sensor -- is that they are rather jittery. When controlling a servo, this leads to a fair amount of jiggle and jitter in the servo. I have been reading here and there about this problem, and the consensus I seem to be discovering is that running my Uno off USB is not a great idea: several posts I have read suggest (or outright state) that the USB 5v is noisy and induces jitter.
I have ordered an AC adapter so I can try running the Uno on wall power instead. I have also tried putting capacitors across my pots and servo motor, putting a pulldown resistor on the photo transistor, but these feeble attempts at de-noising and de-bouncing have had little effect on the jitter issue.
Would the experienced hands here agree that USB power is the main source of jitter? Am I wasting my time trying to filter it out with resistors and capacitors?
How much jitter are we talking about here?
In theory any A/D converter will always give you a jitter of one, or more precisely one least significant bit. You can't get better than that.
Any more than that it is down to two factors, not having a steady referance and not having a steady input.
Noise is generated in lots of places but it could be that an AC adaptor could produce a more noisy than USB. In my experience I have not noticed much difference. The Arduino is first of all a digital device and the switching of the digital bits will put interference to the analogue side.
You can get rock steady ADC performance on the ATmega chips with a battery supply, at the expense
of having an uncalibrated output! I've done some remote devices that monitor their own battery
voltage against the ~1.1V reference over time, and the output value changes by one count only, oscillating
a bit between adjacent counts for a while, then settling to constant for a longer while, before eventually
getting to the next count transition. Never seen such clean behaviour on a power supply though, there
is usually some 10's of mV of noise zinging around then.
A little bit of noise in the readings allows more accurate averaged readings, note, because you can
get sub-LSB resolution. In general the answer to noise is to average or bandwidth limit in some way.
The thermo sensor itself should be reasonably stable because the thermal mass prevents the temperature from changing instantly. (That's ignoring all of those sources of electrical noise.)
The light sensor can change "instantly" and the readings can jump around quite a bit. A photodiode or phototransistor is fast enough to "see" the flicker of a fluorescent bulb (which is too fast for your eye to see).
For whatever is causing the noise, you can try some [u]smoothing[/u]. You can modify the smoothing algorithm to take 1 readings per second, or 10 readings per second, etc., depending on how fast you need to update your readings, or you can increase the number of samples in the moving average, etc.
With a little more programming, you can add some [u]hysteresis[/u], so for example, your motors won't move unless the inputs change by 3 or 4 counts (or whatever works for you).
(int) aveLen is the number of samples over which to average . All else are floats . This was for an altimeter based on a BMP280, but would work for anything.