Chapters

23 Sensor Calibration: Math and Validation

sensing
lab
calibration
wokwi

23.1 Start With the Decision

This section: Start With the Decision — A calibration equation is a rule for correcting a sensor reading.

A calibration equation is a rule for correcting a sensor reading. It is only a claim until fresh readings test it. Compute the corrected value, the reading after that rule, and the residual error, how far it still is from a known reference. Compare that error with the pass limit, the largest allowed error.

Check your understanding: Use the questions below to check the key idea

23.2 Route Overview

This section: Route Overview — This is part 3 of 3.

This is part 3 of 3. Review Sensor Calibration: Two-Point Lab for the preceding evidence.

23.3 Learning Objectives

This section: Learning Objectives

  • Apply a calibration equation to raw sensor readings.
  • Validate corrected results with error and quality metrics.

23.4 Chapter Roadmap

This section: Chapter Roadmap

  • Putting Numbers to It
  • Checkpoint: Calibration Math
  • Part 5: Challenge Exercises
  • Challenge 1: Three-Point Calibration
  • Challenge 2: EEPROM Calibration Storage
  • Automatic Drift Compensation
  • Key Calibration Concepts Summary
  • Best Practices for Sensor Calibration
  • Checkpoint: Production Calibration
  • Calibration Formulas
  • Span Validation
  • Next Calibration Practice
  • Summary
  • Knowledge Check
  • See Also
  • What’s Next
Putting Numbers to It

Two-Point Calibration Calculation: A load cell for beehive monitoring reads 410 raw ADC counts at 0 kg and 3685 counts at 50 kg (using a 12-bit ADC with 0-4095 range). Calculate gain and offset to convert raw ADC to kilograms.

Given reference points:

  • Low: (410 counts,0 kg)(410\text{ counts}, 0\text{ kg})
  • High: (3685 counts,50 kg)(3685\text{ counts}, 50\text{ kg})

Step 1: Calculate gain (slope of the line):

Gain=y2−y1x2−x1=50 kg−0 kg3685−410=503275=0.01527 kg/count\text{Gain} = \frac{y_2 - y_1}{x_2 - x_1} = \frac{50\text{ kg} - 0\text{ kg}}{3685 - 410} = \frac{50}{3275} = 0.01527\text{ kg/count}

Step 2: Calculate offset (y-intercept):

Offset=y1−(x1×Gain)=0−(410×0.01527)=−6.26 kg\text{Offset} = y_1 - (x_1 \times \text{Gain}) = 0 - (410 \times 0.01527) = -6.26\text{ kg}

Step 3: Calibration equation:

Weight (kg)=(Raw ADC counts×0.01527)−6.26\text{Weight (kg)} = (\text{Raw ADC counts} \times 0.01527) - 6.26

Verification:

  • At 410 counts: (410×0.01527)−6.26=6.26−6.26=0 kg(410 \times 0.01527) - 6.26 = 6.26 - 6.26 = 0\text{ kg} ✓
  • At 3685 counts: (3685×0.01527)−6.26=56.27−6.26=50.01 kg≈50 kg(3685 \times 0.01527) - 6.26 = 56.27 - 6.26 = 50.01\text{ kg} \approx 50\text{ kg} ✓

Resolution calculation:

Resolution=Gain=0.01527 kg/count≈15 grams/count\text{Resolution} = \text{Gain} = 0.01527\text{ kg/count} \approx 15\text{ grams/count}

With a 12-bit ADC (4096 levels) spanning the full 50 kg range, theoretical resolution would be 50/4096 = 12.2 g. Our actual resolution of 15 g is slightly worse because the load cell does not use the full ADC range (3275 out of 4096 counts, or about 80%). Adjusting amplification to use more of the ADC range would improve resolution.

This section:

Physics Phoebe, Physics and measurement-foundations expertCheckpoint: Calibration Math

You now know:

  • Two reference points define the line: gain equals (actual_high - actual_low) / (raw_high - raw_low).
  • The load-cell example maps 410 counts to 0 kg and 3685 counts to 50 kg, giving a gain of 0.01527 kg/count and an offset of -6.26 kg.
  • A 12-bit ADC has 4096 levels, but usable resolution depends on how much of that range the sensor actually spans.

23.4.1 Interactive ADC Resolution Calculator

Explore how ADC bit depth and measurement range affect the smallest detectable change (resolution per count).



23.4.2 Knowledge Check: Calibration Math

Check your understanding: Use the questions below to check the key idea Check your understanding: Use the questions below to check the key idea
Practise

Hands-on lab · 45 min · Wokwi ESP32

Capture two references and validate a calibration

I have explained calibration and bias; now let's capture two reference points and test the corrected readings.

Open the lab

23.4.3 Moving Average Filter Explorer

A moving average filter smooths noisy data by averaging a window of recent samples. The window size (N) controls the tradeoff between smoothing and responsiveness. Larger windows produce smoother output but react more slowly to real changes.



The math and filter sections answer “what coefficients should I calculate?” The challenges below ask the production question: how do you keep those coefficients trustworthy after the first successful run?

23.5 Part 5: Challenge Exercises

This section: Part 5: Challenge Exercises

23.6 Challenge 1: Three-Point Calibration

This section: Challenge 1: Three-Point Calibration — Run it: Before you code the three-point version, compare the methods in the reference animation below.

Run it: Before you code the three-point version, compare the methods in the reference animation below. Select 2-point and then Multi-point on the same sensor type and watch how a straight two-point line leaves error in a bending response while multi-point interpolation follows the curve. Use it to predict how much accuracy the added midpoint should buy you before you measure it in the simulator.

Goal: Extend the calibration to use three reference points for improved accuracy across the range.

Tasks:

  1. Add a third reference point at 50% (midpoint)
  2. Capture three points: low (10%), mid (50%), high (90%)
  3. Use piecewise linear interpolation:
    • For values < 50%: use low-to-mid segment
    • For values >= 50%: use mid-to-high segment
  4. Compare accuracy against two-point calibration

Hint: Store two sets of gain/offset coefficients and select based on input value.

23.7 Challenge 2: EEPROM Calibration Storage

This section: Challenge 2: EEPROM Calibration Storage

Goal: Persist calibration coefficients so they survive power cycles.

Tasks:

Start by add EEPROM library and save calibration after calculation. Then load calibration automatically at startup. Next add validity check (magic number) to detect uncalibrated state. Finally add a ‘w’ command to write calibration and ‘e’ command to erase.

#include <EEPROM.h>

// EEPROM addresses
const int EEPROM_MAGIC_ADDR = 0;
const int EEPROM_GAIN_ADDR = 4;
const int EEPROM_OFFSET_ADDR = 8;
const int EEPROM_MAGIC_VALUE = 0xCAFE;

void saveCalibrationToEEPROM() {
    EEPROM.begin(64);
    EEPROM.put(EEPROM_MAGIC_ADDR, EEPROM_MAGIC_VALUE);
    EEPROM.put(EEPROM_GAIN_ADDR, calGain);
    EEPROM.put(EEPROM_OFFSET_ADDR, calOffset);
    EEPROM.commit();
    Serial.println("Calibration saved to EEPROM!");
}

void loadCalibrationFromEEPROM() {
    EEPROM.begin(64);
    int magic;
    EEPROM.get(EEPROM_MAGIC_ADDR, magic);

    if (magic == EEPROM_MAGIC_VALUE) {
        EEPROM.get(EEPROM_GAIN_ADDR, calGain);
        EEPROM.get(EEPROM_OFFSET_ADDR, calOffset);
        Serial.println("Calibration loaded from EEPROM");
    } else {
        Serial.println("No valid calibration found, using defaults");
        calGain = 1.0;
        calOffset = 0.0;
    }
}

23.8 Automatic Drift Compensation

This section: Automatic Drift Compensation

Goal: Implement automatic baseline drift correction for long-term deployments.

Background: Sensors drift over time due to aging, temperature changes, and contamination. Automatic Baseline Correction (ABC) can compensate by assuming the sensor occasionally sees a known reference (e.g., CO2 sensors assume 400ppm outdoor air).

Tasks:

  1. Track the minimum reading over a 24-hour window
  2. Assume this minimum represents the “baseline” reference value
  3. Automatically adjust offset to correct drift
  4. Add drift alarm if correction exceeds threshold

23.9 Key Calibration Concepts Summary

This section: Key Calibration Concepts Summary — To consolidate the lab into a repeatable workflow, inspect Figure 23.1 before moving from calculation to field use.

To consolidate the lab into a repeatable workflow, inspect Figure 23.1 before moving from calculation to field use. The map connects error types, calibration methods, validation, and maintenance so none is mistaken for the whole job.

Shows: Figure 23.1: Mind map of key sensor calibration concepts covered in this lab
Sensor calibration proceeds through Diagnose, Fit, Validate and Maintain. Gain and offset need reference conditions, residual error and a recorded recalibration trigger.
Figure 23.1: Mind map of key sensor calibration concepts covered in this lab

Read Figure 23.1, begin with the error branch, then follow the method and validation branches before finishing at maintenance. That order shows why a fitted gain and offset are only useful when their reference conditions, residual error, and recalibration trigger remain recorded.

ConceptDescriptionWhen to Use
Offset ErrorSensor reads non-zero when true value is zeroAlways needs correction
Gain ErrorSensor’s sensitivity differs from specificationWhen readings scale incorrectly
Two-Point CalibrationUses two reference points to calculate offset and gainLinear sensors (most common)
Multi-Point CalibrationUses 3+ reference points with interpolationNon-linear sensors (thermistors, pH)
Moving Average FilterAverages N recent readings to reduce noiseNoisy environments, slow-changing signals
EEPROM StoragePersists calibration across power cyclesProduction deployments

Best Practices for Sensor Calibration
  • Use reference standards that bracket your expected measurement range.
  • Allow sensor warm-up time before calibration (typically 5-30 minutes).
  • Document environmental conditions during calibration (temperature, humidity).
  • Recalibrate periodically based on manufacturer recommendations.
  • Store calibration metadata including date, conditions, and number of points.
  • Validate calibration by checking known reference values after applying coefficients.
Physics Phoebe, Physics and measurement-foundations expertCheckpoint: Production Calibration

You now know:

  • Multi-point calibration adds 3 or more reference points when a sensor curve is not linear.
  • EEPROM storage needs a validity marker such as 0xCAFE so startup code can reject uninitialized memory.
  • Automatic Baseline Correction tracks a 24-hour minimum only when the sensor is expected to see a known baseline condition.
Calibration Formulas

Two-Point Calibration Formula:

StepFormulaDescription
1gain = (actual_high - actual_low) / (raw_high - raw_low)Calculate slope
2offset = actual_low - (raw_low × gain)Calculate y-intercept
3calibrated = raw × gain + offsetApply correction

See the Two-Point Calibration Calculator above for a hands-on tool. The worked load-cell example in Part 4 shows the same calculation with real numbers.

23.9.1 Production Considerations

Check your understanding: Use the questions below to check the key idea Check your understanding: Use the questions below to check the key idea

Choose calibration references that bracket the measurement range the application must validate. If the release test also checks just beyond the operating endpoints, include those limits in the reference plan.

Check your understanding: Use the questions below to check the key idea

23.10 Span Validation

This section: Span Validation — The core lab above shows how to build and store a two-point calibration.

The core lab above shows how to build and store a two-point calibration. The companion page focuses on the failure modes that determine whether those coefficients are trustworthy in production.

Next Calibration Practice

Continue with Calibration Span Error and Validation to test reference-point spacing, residual checks, range guards, and release evidence for calibrated sensors.

Practise

Hands-on lab · 30 min · Wokwi ESP32

Convert an NTC ADC reading to temperature

I have explained calibration math; now let's convert an ADC reading into resistance and temperature.

Open the lab
Practise

Hands-on lab · 35 min · Python 3 in your browser (JupyterLite)

Two-point sensor calibration and residual error

I have explained calibration span; now let’s fit two points and plot error at unused temperatures.

Open the lab

23.11 Summary

This section: Summary — This lab provided hands-on experience with essential sensor calibration techniques used in production IoT systems.

This lab provided hands-on experience with essential sensor calibration techniques used in production IoT systems.

23.11.1 Key Takeaways

ConceptWhat You LearnedWhen to Apply
Two-Point CalibrationCalculate gain and offset from two known reference pointsAll linear sensors (temperature, pressure, light)
Moving Average FilterSmooth noisy readings by averaging N recent samplesNoisy environments, before capturing calibration references
State Machine DesignGuide users through multi-step processesAny interactive calibration or configuration procedure
Calibration Formulacalibrated = raw * gain + offsetApply correction to every raw sensor reading
EEPROM PersistenceStore calibration across power cycles with magic number validationProduction deployments, field-calibrated devices
Automatic Drift CompensationTrack minimum over time window to correct sensor driftLong-term deployments, sensors prone to aging

23.11.2 Skills You Practiced

  • Circuit building: Connecting potentiometer and LED to ESP32.
  • Serial communication: Interactive command interface for calibration.
  • Mathematical modeling: Applying linear algebra to sensor correction.
  • Firmware architecture: State machine design for multi-step workflows.
  • Data persistence: Using EEPROM for non-volatile storage.

23.11.3 Common Pitfalls to Avoid

Before treating a calibration run as complete, inspect Figure 23.2 to turn four seemingly sensible but invalidating shortcuts into checks applied before measurements are accepted.

Shows: Figure 23.2: Common calibration pitfalls to avoid
Diagram showing four common calibration pitfalls: reference points too close together, no sensor warm-up time, not filtering before capture, and assuming calibration lasts forever, with fixes for each
Figure 23.2: Common calibration pitfalls to avoid

Read Figure 23.2 by pairing each error with its fix. Use reference points that are far enough apart. Let the sensor warm up. Make the samples stable before capture. Plan another calibration instead of assuming the first one lasts forever. Wide reference points protect the fitted slope. Warm-up and filtering protect the data used for that fit. The final reminder carries the same care into service. The lab ends with evidence that can be renewed, not values that are trusted without another test.

PitfallWhy It’s BadSolution
Reference points too close togetherSmall errors in reference measurement cause large errors in calculated gainUse 10% and 90%, not 45% and 55%
Forgetting sensor warm-up timeSensors drift significantly in first few minutes after power-onAllow 5-30 minutes before calibration
Not filtering before captureSingle noisy sample can corrupt entire calibrationApply moving average filter before capturing reference
Assuming calibration lasts foreverSensors drift over time due to aging and environmentRecalibrate periodically based on manufacturer guidance

23.12 Knowledge CheckCheck your understanding: 23.12 Knowledge Check

This section: Knowledge Check

23.12.1 Quiz: Sensor Calibration

Inspect the real sensor calibration laboratory below.

Real photograph of sensor calibration laboratory
This real example (SOAR staff working on the Calibration Wavefront Sensor assembly (20210624152355-IMG-6439-1-CC2)) shows a physical form of sensor calibration laboratory. Use the visible package, interfaces, scale, mounting, and surrounding context as evidence; a catalogue label alone does not establish deployment fit. Photo: CTIO/NOIRLab/SOAR/NSF/AURA; CC BY 4.0
Check your understanding: Use the questions below to check the key idea

Match each calibration concept with its correct definition:

Arrange the two-point calibration procedure steps in the correct order:

23.13 See Also

This section: See Also — For the full list of related chapters, see the “Related Chapters” section at the top of this page.

For the full list of related chapters, see the “Related Chapters” section at the top of this page. Additional resources:

23.14 What’s Next

This section: What’s Next

If you want to…Read this
Understand the theory behind filtering and calibrationSensor Data Processing
Learn I2C and SPI sensor communication protocolsSensor Communication Protocols
Apply calibration to specific sensor types in depthSensor Types: Calibration
Practice more sensor labs on ESP32 with WokwiSensor Labs: Implementation and Review

23.15 Continue Your Route

This section: Continue Your Route — This is the last part of this chapter.

This is the last part of this chapter. Return to Sensor Calibration: Two-Point Lab or continue from the sensors module index.

Find help

3 reviewed answers.

Where should I start in a module?

Open the module page from the sidebar to see its chapters in order. Use the module map to return to a topic you need.

Modules and paths

Reviewed

What can I try when a project does not work?

Check the symptom, then work through the diagnostic steps. Keep the error message and the smallest example that reproduces it.

When you are stuck

Reviewed

How do interactive examples work?

Use the interactive guide to check how examples and simulations work before changing your project.

Interactive examples guide

Reviewed