Skip to content
Tutorials Center
My learning

Topics

Lesson 9 of 36 ยท Sensors and Actuators

Signal Conditioning, Calibration and Filtering

Clean noisy sensor data with moving averages, exponential smoothing, median filters, debouncing and two-point calibration, with runnable simulations.

Raw sensor readings are rarely ready to use. They contain noise, spikes, offsets and unit conversions. Signal conditioning is everything between the physical sensor and the clean number your application trusts: amplifying, scaling, calibrating and filtering. Doing this on the device saves bandwidth, because you send one good value instead of hundreds of noisy ones.

This lesson covers the most useful techniques for microcontroller code: averaging, smoothing, median filtering, debouncing, calibration and sanity checks. All are small enough to run on the tiniest chip and you can try each here.

Hardware conditioning in brief

Some conditioning happens in analog circuitry before the ADC:

  • Amplification: an op-amp boosts a tiny signal (a thermocouple produces microvolts).
  • Voltage dividers and level shifting: bring the signal into the ADC range.
  • RC low-pass filter: a resistor and capacitor remove high-frequency noise and prevent aliasing.
  • Decoupling capacitors: placed near chips to keep the supply steady.
  • Shielded or twisted cables: reduce pickup of interference on long wires.

After that, software takes over.

A noisy signal to practise on

The examples below use a simulated signal: a slow temperature ramp plus noise and occasional spikes. We seed the random generator so results are repeatable.

Python Moving average: smoother, but delayed

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A moving average of the last N samples reduces random noise by about the square root of N but lags behind real changes. A larger window means smoother output and more delay. It also needs memory for N samples.

Exponential moving average (EMA)

The EMA needs only one stored number and one multiplication, perfect for tiny chips:

smoothed = alpha * new + (1 - alpha) * smoothed

A small alpha (0.05) smooths heavily; a large one (0.5) follows quickly.

Python EMA with different smoothing factors

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Trade-off again: the heavier the smoothing, the slower the response to a real change. Pick alpha from how fast the real quantity can change and how much noise you tolerate.

Median filter: removing spikes

Averages are bad against outliers: one wild reading of 999 drags a mean far. A median filter sorts the last N values and takes the middle one, which ignores isolated spikes entirely.

Python Median versus average against spikes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The mean jumps wildly when a spike appears; the median barely moves. For sensors that occasionally glitch (ultrasonic, DHT), a median of 3 to 5 samples is a cheap, strong defense. You can combine them: median first to kill spikes, then EMA for smoothness.

Debouncing switches

A mechanical button does not make a clean transition: the contacts bounce, producing many on-off pulses within a few milliseconds. Read naively, one press counts as ten. Debouncing ignores changes that do not stay stable for a short time (typically 20 to 50 ms), either with an RC circuit or in software.

Python Software debounce of a bouncy button

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Real code usually runs this in a loop on a timer or uses a library; interrupt-driven versions appear in GPIO, PWM and interrupts.

Calibration

Calibration corrects systematic errors (offset and gain) by comparing your sensor with a known reference.

  • One-point (offset) calibration: measure a known value, such as ice water at 0 C, and subtract the difference.
  • Two-point calibration: measure two known points (ice water and boiling water, adjusted for altitude), then fit a line: corrected = gain * raw + offset.
  • Multi-point or curve fit for non-linear sensors.
Python Two-point calibration of a thermometer

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Store the calibration constants in non-volatile memory (flash or EEPROM) so they survive a reboot, and record the date; calibration can expire as sensors drift.

Sanity checks and units

Before trusting a reading, check it:

  • Range check: is the value physically possible? A room temperature of -300 C is not.
  • Rate-of-change check: temperature rarely jumps 20 degrees in one second.
  • Stuck-value check: a sensor repeating the exact same value for hours may have failed.
  • Missing data: mark failed reads as invalid (NaN or a flag) instead of sending 0.

Always convert to standard units on the device or document them clearly in the message, and include a timestamp.

Python A reading validator for sensor data

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Putting it together on the device

A typical firmware pipeline for one reading: sample 5 times, take the median, convert units, apply calibration, validate, then send. Doing this on the device means the network carries clean values only.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the main cost of a larger moving-average window?

    Averaging more samples smooths more but reacts more slowly to genuine changes.
  2. 2. Which filter handles isolated spikes best?

    The median ignores extreme values at either end of the sorted window.
  3. 3. Why is a button debounced?

    Mechanical contacts chatter for a few milliseconds, so one press would otherwise register many times.
  4. 4. A thermometer reads 1.8 at the 0 C reference and 97.6 at 100 C. What does two-point calibration compute?

    Two known points define a line (gain and offset) used to correct all other readings.
  5. 5. Which EMA alpha smooths the most?

    A small alpha gives the new sample little weight, so the output changes slowly and smoothly.

Summary

  • Conditioning turns raw readings into clean values: amplify, scale, filter, calibrate, validate.
  • Moving averages and EMAs reduce noise at the cost of lag; medians remove spikes.
  • Debounce mechanical switches; calibrate with one or two reference points and store the constants.
  • Validate range, rate of change and stuck values before sending data.

Next: Microcontrollers vs Microprocessors