Chapters

22 Sensor Calibration: Measurement Foundations

sensing
lab
calibration
wokwi

22.1 Start With the Decision

A sensor can be precise and still report the wrong value. Calibration begins with a reference, a raw reading, and a stated error.

22.2 Route Overview

This is part 1 of 3. Continue with Sensor Calibration: Two-Point Lab.

22.3 Part Objectives

  • Separate accuracy, precision, bias, and repeatability.
  • Plan calibration points from the expected measurement range.

22.4 Start With the Measurement Story

A calibration lab should feel like detective work: compare the raw reading to a known condition, identify the error, apply a correction, then test a new point to prove the sensor has become more trustworthy.

22.5 Learning Objectives

By completing this lab, you will be able to:

  1. Explain calibration fundamentals: Describe why sensors need calibration and how raw readings differ from true values
  2. Implement two-point calibration: Calculate offset and gain correction using low and high reference points
  3. Apply signal conditioning: Configure moving average filtering to reduce noise in sensor readings
  4. Evaluate raw vs calibrated data: Measure the impact of calibration on measurement accuracy
  5. Integrate calibration persistence: Deploy EEPROM-based coefficient storage for production systems
In 60 Seconds

This hands-on Wokwi lab walks through two-point calibration on an ESP32 — calculating gain and offset from two known reference points using calibrated = raw * gain + offset. You configure a moving average filter, persist calibration coefficients in EEPROM, and verify that calibrated readings track true values across the sensor’s full range. No physical hardware needed; everything runs in your browser.

The mathematical gist. A 12-bit converter splits this lab’s 50,000 g span into 4,096 bins, so the ideal step is 12.2 g/count and the measured 3,275-count span gives 15.27 g/count. Treating the resulting dither as independent noise gives erms(N)=q/(12N)e_{rms}(N)=q/(\sqrt{12}\sqrt{N}): the chapter’s 10-sample average reduces its measured 4.41 g RMS noise to 1.39 g, but cannot undo aliasing.

Math Bridge · guided foundationsHow does averaging recover useful ADC resolution?Let Phoebe unpack quantisation noise, square roots, and this lab's own 10-reading beehive average.
Key Concepts

Start with Two-Point Calibration: A procedure using two known reference values to calculate gain and offset correction coefficients, correcting sensitivity errors and zero-point errors simultaneously. Then Gain Coefficient: The slope m in calibrated = raw ** gain + offset; corrects proportional errors where the sensor reads too high or too low by a percentage of the measured value. Next Offset Coefficient: The intercept b in the calibration equation; corrects constant errors where the sensor always reads a fixed amount above or below the true value. After that Moving Average Filter: A digital filter replacing each reading with the average of the last N samples, reducing noise at the cost of slower response to rapid changes. Continue by EEPROM Persistence: Storing calibration coefficients in non-volatile memory so they survive power cycles — essential for production IoT deployments. Continue by Reference Standards: Known, accurate values used as the basis for calibration; their accuracy sets the ceiling for calibrated sensor accuracy. Continue by Calibration Drift: Gradual change in sensor response over time due to aging, contamination, or thermal stress, requiring periodic recalibration. Finally Wokwi Simulator: Browser-based ESP32 simulation environment allowing firmware development and testing without physical hardware.

22.6 Most Valuable Understanding (MVU)

Two-point calibration corrects sensor errors by calculating a simple linear equation: calibrated = raw ** gain + offset, where gain fixes sensitivity errors and offset fixes zero-point errors.

This is the single most important concept in this lab. Every real sensor has manufacturing variations that cause its readings to deviate from the true value. Two-point calibration uses two known reference points to calculate correction coefficients that map inaccurate raw readings to accurate calibrated values. The formula y = mx + b (from basic algebra) is the foundation - gain is the slope (m) and offset is the y-intercept (b).

Remember: Calibration accuracy depends entirely on your reference standards. Use the most accurate references you can obtain, and bracket your expected measurement range (calibrate at 10% and 90%, not both at 50%).

Related Chapters

This is part of a series on Sensor Interfacing:

  1. Sensor Data Processing - Theory behind filtering and calibration
  2. Sensor Calibration Lab (this chapter) - Hands-on calibration workshop
  3. Sensor Communication Protocols - I2C, SPI interfaces
  4. Sensor Applications - Real-world implementation examples

Related Topics:

22.7 Introduction

In this hands-on lab, you will build a complete sensor calibration system using an ESP32 microcontroller in the Wokwi browser-based simulator. You will wire a potentiometer to simulate a sensor with offset and gain errors, implement two-point calibration to correct those errors, and apply a moving average filter for noise reduction. The lab takes approximately 45-60 minutes to complete and requires no physical hardware — everything runs in your browser.

By the end of this lab, you will have working firmware that interactively captures reference points, calculates calibration coefficients, and applies real-time correction to sensor readings.

Chapter Roadmap
  • Start With the Measurement Story
  • In 60 Seconds
  • Key Concepts
  • Most Valuable Understanding (MVU)
  • Related Chapters
  • Introduction
  • Prerequisites
  • Interactive Browser-Based Lab
  • Calibration Matters
  • Teaching Sensors Truth
  • Calibration Matters
  • Folded Error And Quality Metrics Notes

22.8 Prerequisites

  • Basic understanding of Arduino/C++ programming
  • Familiarity with analog inputs and ADC concepts
  • Completion of the Sensor Data Processing chapter (recommended)

22.8.1 Learning Path

1. Before This Lab Review ADC readings, Arduino/ESP32 serial output, and basic sensor data processing.

2. In This Lab Build the circuit, capture low/high references, calculate gain and offset, then smooth noisy readings.

3. After This Lab Apply multi-point calibration, sensor fusion, EEPROM persistence, and production validation.

Interactive Browser-Based Lab

This lab uses Wokwi, a free online electronics simulator. No physical hardware required! You can experiment with sensor calibration techniques directly in your browser.

Calibration Matters

Think of calibration like adjusting a musical instrument. Even a brand-new guitar needs to be tuned before it plays the right notes. Sensors are similar - they need to be “tuned” to give accurate readings.

Why do sensors need calibration?

  1. Manufacturing variations: No two sensors are exactly identical, just like no two guitars are perfectly tuned from the factory
  2. Environmental factors: Temperature, humidity, and age can cause sensors to drift over time
  3. Component tolerances: The electronic parts inside sensors have slight variations

Real-world example: Imagine you buy a cheap thermometer that always reads 2 degrees too hot. You could either:

  • Buy an expensive, perfectly calibrated thermometer ($$$)
  • Or calibrate your cheap thermometer by noting “always subtract 2 degrees” (FREE!)

The mathematical approach: Instead of just “subtract 2”, calibration gives us a formula: corrected = raw × gain + offset

  • Offset fixes constant errors (like always being 2 degrees off)
  • Gain fixes scaling errors (like reading 50% when it should be 55%)

This lab teaches you how to find those correction values using two known reference points - just like tuning a guitar by comparing it to a tuner at two different notes!


Teaching Sensors Truth

Hey there, young scientist! Let’s learn about sensor calibration with the Sensor Squad!

Temperature Terry has a problem - when the room is actually warm (like 25 degrees), Sammy says “It’s 28 degrees!” And when it’s cold (like 10 degrees), Sammy says “It’s 13 degrees!” Sammy is not lying - Sammy was just built a little differently than other sensors! Sammy always reads too high - that is an offset error (always adding a constant). But notice something: at 10 degrees Sammy is off by 3, and at 25 degrees Sammy is off by 3 too. That constant +3 shift is a pure offset error.

Think of it like a bathroom scale: Imagine your bathroom scale always shows 3 pounds more than your real weight. That is an offset error - it is always off by the same amount! You could fix it by subtracting 3 from every reading.

But what if the scale also stretches the numbers - showing 11 pounds when you are really 10? That is a gain error - the scale reads proportionally too high!

Sammy’s Calibration Adventure:

  1. Step 1 - Find a “known cold” reference: Sammy measures ice water (which we KNOW is 0 degrees). Sammy says “It’s 3 degrees!” Oops - that is Sammy’s offset error! (0 + 3 = 3)

  2. Step 2 - Find a “known hot” reference: Sammy measures boiling water (which we KNOW is 100 degrees). Sammy says “It’s 103 degrees!” Still 3 degrees too high - the offset is consistent! (100 + 3 = 103)

  3. Step 3 - Do the math magic: Using both reference points, we calculate the correction formula: subtract 3 from every reading. Now whenever Sammy gives a reading, we fix it automatically!

Lila the Light explains: “It’s like being a translator! Sammy speaks ‘Sammy language’ and we translate it to ‘real temperature language’ using our special formula!”

Max the Motor adds: “I need calibration too! When someone tells me ‘go 50% speed’, I might actually go 55% without calibration. That could make robots bump into walls!”

Fun Experiment: Ask a grown-up if you can calibrate a kitchen thermometer! Put it in ice water (should read 0C or 32F) and see if it’s accurate. Many thermometers are off by a few degrees!

Remember: Calibration is like teaching your sensor to tell the truth by giving it a “cheat sheet” of corrections!


22.9 Calibration Matters

Before collecting reference points, inspect Figure 22.1 to see why calibration is a measurement process rather than a cosmetic adjustment. The visual separates the imperfect observation from the evidence used to correct it.

Flowchart showing calibration process: raw sensor readings with errors on left, calibration process using reference points in middle, accurate calibrated readings on right, illustrating how calibration transforms inaccurate data into precise measurements.
Figure 22.1: Calibration transforms inaccurate raw sensor readings into precise, reliable measurements

Read Figure 22.1, read from the biased raw reading through comparison with known references and into the corrected result. The transition shows what the rest of the lab must prove: correction coefficients come from traceable observations, and their accuracy must be checked on data not merely assumed.

Real sensors have manufacturing variations that cause:

  • Offset errors: Sensor reads non-zero when it should read zero
  • Gain errors: Sensor’s sensitivity differs from the ideal specification
  • Non-linearity: Response curve deviates from expected linear relationship

Two-point calibration corrects both offset and gain errors by measuring at two known reference points.

22.10 Folded Error And Quality Metrics Notes

Offset and gain errors behave differently in the calibration record. Offset shifts the whole response by a constant amount, so a zero reference can be wrong even when the slope is usable. Gain changes the slope, so error grows as the true value moves away from the reference point. A two-point calibration should therefore record both reference points, the calculated slope and offset, and at least one verification point between or beyond the references.

After calibration, do not report only “it works.” Compare measured values with actual reference values and record metrics that match the claim:

  • Maximum absolute error shows the worst observed miss in the checked range.
  • RMSE summarizes typical error across several verification points.
  • R-squared helps detect whether the calibrated response is close to linear, but it does not replace endpoint error checks.

These metrics are evidence for the tested range only. If the reference points are too close together, small reference mistakes can amplify at the extremes, so span and verification evidence matter as much as the equation.

22.11 Continue to the Next Part

Carry this evidence into Sensor Calibration: Two-Point Lab, which begins with Two-Point Calibration Limits.