I have written a program that records the temperature of my workshop and moves a servo to a position according to the temperature, it refreshes every 1 second but using serial monitor I have noticed every so often the reading is duff for example:-
I have added a couple of smoothing capacitors one on the servo and one across the +- terminals this has really helped a lot but the issue still exists. I have tried to write a for loop to just take an average from all the reading, but because the loop is so quick each variable stores the same data.
Anything else I can do?
That is normal sensor noise. All sensors are noisy, and averaging in one form or another is all you can do. Of course, noise from external sources, such as power supply voltage variations, can affect sensor readings as well.
No, its unlikely to be sensor noise, its power supply noise probably - unless you have a separate
analog voltage rail properly filtered its hard to prevent supply noise affecting readings of a few
millivolts. Given a pure supply that sensor probably outputs << 1mV of noise. Try running the Arduino
off a battery supply (not USB) so the power rail is much cleaner.
Alternatively amplify the sensor’s voltage before reading it with an opamp, so that the power rail
noise is less significant.
OP has not told us what sensor he is using. It could simply be that the internal accuracy of the device is the cause.
To remove any odd readings, you could compare all readings and to the average of the previous 5 readings and if the new reading is greater than your selected error from the reading, it can be ignored.
The datasheet (page 7) shows the Wideband Output Noise Voltage spanning about 20mV.
You could try connecting a series 1K resistor to the sensor’s output, then at the analog input connect a 1uF capacitor to GND.
Another method sometimes used in software is to just take 2 readings in sequence, thereby always discarding the first and allowing the internal ADC capacitance to fully charge to the signal.