Sub topic 12
Ignoring Small Changes with a Deadband
Prerequisites and purpose
This chapter assumes that you can upload an Arduino sketch, use analogRead(), and view values in the Serial Monitor. It also assumes an Arduino Uno-style analog input that returns values from 0 to 1023.
A potentiometer can produce small changes even when you are not deliberately turning its knob. Electrical noise, contact movement, and the ADC’s limited resolution can make the reading jump between nearby numbers. If your program reacts to every reading, an LED, display, or motor may flicker.
A deadband is a range of small changes that your program ignores. Instead of responding to every new reading, the program responds only when the new value differs enough from the last accepted value.
How a change deadband works
Suppose the last reported reading is 500 and the threshold is 4:
- Readings from 496 through 504 are ignored.
- A reading of 505 or higher is reported.
- A reading of 495 or lower is reported.
- After reporting a new value, that value becomes the new comparison point.
This deadband limits unnecessary updates. It does not make the ADC measurement more accurate, and it does not replace averaging. Averaging reduces random variation before you use a value. A deadband decides whether a change is large enough to announce or act on.
Implementing a deadband
The following sketch prints a reading only when it changes by at least four ADC counts. The initial value of lastReported is -1, which cannot be a valid analogRead() result. This lets the first real reading pass through immediately.
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);
}abs() returns the absolute value of a number. Therefore, a change from 500 to 504 and a change from 500 to 496 both have a difference of 4.
The threshold is measured in ADC counts, not volts. On an Uno using a 5 V reference, one count is approximately:
| Setting | Meaning |
|---|---|
| `THRESHOLD = 4` | Ignore changes smaller than about 19.6 mV |
The approximate voltage per count is 5 V / 1023, or about 4.9 mV. A threshold of 4 therefore represents about 19.6 mV.
Combining a deadband with output control
You can use the accepted value to control another device. For example, map it to an LED’s PWM range:
const int POT_PIN = A0;
const int LED_PIN = 9;
const int THRESHOLD = 4;
int lastAccepted = -1;
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
int value = analogRead(POT_PIN);
if (lastAccepted < 0 ||
abs(value - lastAccepted) >= THRESHOLD) {
lastAccepted = value;
int brightness = map(lastAccepted, 0, 1023, 0, 255);
analogWrite(LED_PIN, brightness);
}
delay(20);
}This avoids repeatedly writing nearly identical PWM values. The LED still changes in steps determined by the threshold, so test the result and adjust it for your circuit.
Exercise
If lastAccepted is 600 and the threshold is 5, will a reading of 596 update the output? Why?
Show answer
No. The difference is 4, which is smaller than the threshold of 5, so the reading is ignored.