Sub topic 11
Why readings jitter and how to average them
This chapter assumes that you can write a basic Arduino sketch, upload it, open the Serial Monitor, and wire a potentiometer between power, ground, and an analog input such as A0. You should also know how analogRead() returns a number and how setup() and loop() work.
Why an analog reading changes
A potentiometer produces a changing voltage, but the Arduino does not measure a perfectly continuous value. Its analog-to-digital converter (ADC) samples the voltage and converts it to a number. Small changes can come from several sources:
- Electrical noise from the power supply or nearby wires.
- Tiny changes in the potentiometer's wiper contact.
- Noise on the reference voltage used by the ADC.
- ADC quantization, where nearby voltages become different integer readings.
- Mechanical movement while you turn the knob.
For example, a stationary potentiometer might produce readings such as 511, 512, 510, 512. The knob has not moved meaningfully. The measured voltage is simply varying slightly around the same value.
A program that reacts to every raw reading may make an LED flicker, a motor change speed, or a menu selection jump between values.
Averaging several readings
A simple way to reduce random jitter is averaging. An average is the sum of several readings divided by the number of readings. Positive and negative variations partly cancel each other.
const int POT_PIN = A0;
const int NUM_SAMPLES = 10;
int readAveraged() {
long sum = 0;
for (int i = 0; i < NUM_SAMPLES; i++) {
sum += analogRead(POT_PIN);
delay(2);
}
return sum / NUM_SAMPLES;
}
void setup() {
Serial.begin(9600);
}
void loop() {
int value = readAveraged();
Serial.println(value);
delay(50);
}The long variable is important. Ten readings of up to 1023 fit in an int on an Uno, but using long gives the sum more room and keeps the function safe if you increase NUM_SAMPLES or use a board with a larger ADC range.
This example takes ten samples over about 20 milliseconds. It then prints one averaged result. Increasing the number of samples usually makes the value steadier, but it also makes the response slower. A control that must respond immediately should use fewer samples or a different filter.
| Number of samples | Stability | Response |
|---|---|---|
| 1 | Lowest | Fastest |
| 10 | Better | Slightly slower |
| 50 | Highest for this method | Noticeably slower |
Using a deadband
A deadband is a range of small changes that a program ignores. Instead of printing or using every new value, compare it with the last accepted value.
const int POT_PIN = A0;
const int THRESHOLD = 4;
int lastReported = -1;
void setup() {
Serial.begin(9600);
}
void loop() {
int value = analogRead(POT_PIN);
if (lastReported < 0 ||
abs(value - lastReported) >= THRESHOLD) {
lastReported = value;
Serial.println(value);
}
delay(20);
}With a threshold of 4, a new reading must differ by at least four counts before the program reports it. The first reading is always accepted because lastReported starts at -1.
A deadband does not make the measurement more accurate. It makes the output less sensitive to small changes. Averaging reduces noise before a decision; a deadband prevents insignificant changes from triggering a decision.
Exercise
A potentiometer produces 498, 500, 499, 501, and 502. What is the average, and why might averaging help?
Show answer
The average is 500. Averaging combines nearby readings so small random variations have less effect on the value used by the program.