From an analog input to a digital output. Concept illustration, not a wiring diagram.
An ADC code is a measurement of a voltage relative to a reference, not automatically a temperature or distance. Resolution is the size of a code step. Precision describes repeatability. Accuracy describes agreement with a trusted reference. Averaging may improve repeatability but cannot remove a wrong scale factor or offset.
On the Uno R3, analogRead normally returns 0–1023. Dividing by 1023 is a convenient endpoint mapping; the ideal 10-bit quantization step is Vref / 1024. The default reference follows the supply and is not guaranteed to be exactly 5.000 V. Measure it when voltage accuracy matters and state your measurement assumptions.
For a two-point linear calibration, record rawLow and rawHigh at two known reference values. Estimate value = lowValue + (raw − rawLow) × (highValue − lowValue) / (rawHigh − rawLow). Reject equal raw endpoints, and validate a third point not used in calibration. This cannot correct arbitrary nonlinear sensors.
Filter noise without hiding response time
Use a potentiometer wired to 5V/GND/A0. This complete sketch prints raw and filtered readings for Serial Plotter at 9600 baud.
Arduino C++
unsigned long lastSample =0;
float filtered =0;
bool initialized = false;
voidsetup() { Serial.begin(9600); }
voidloop() {
const unsigned long now =millis();
if (now - lastSample < 50) return;
lastSample = now;
constint raw =analogRead(A0);
if (!initialized) { filtered = raw; initialized = true; }
filtered +=0.2f * (raw - filtered);
Serial.print(F("raw:")); Serial.print(raw);
Serial.print(F(",filtered:")); Serial.println(filtered);
}
The exponential moving average uses little memory. A smaller coefficient smooths more but responds more slowly. Initializing from the first sample avoids a fictitious ramp from zero at startup. The filter assumes a roughly fixed sampling interval; changing that interval changes its behavior in time.
Experiment: Step the knob quickly and compare raw and filtered curves. Try coefficients 1.0, 0.2, and 0.05. Record both noise and settling time. Do not filter away fault flags or treat a missing sensor as a genuine zero.
Clean Code: Use descriptive constants instead of magic numbers
Instead of calling digitalWrite(13, HIGH) directly in your code, define descriptive constants like const int LED_PIN = 13; at the top of your file. This makes your code self-documenting and extremely easy to update if you change your wiring.