23 Sensor Calibration: Math and Validation
More
Depth: Practitioner
Progress saves as you read. Mark sections complete as you go; your progress appears in the sidebar.
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.
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
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:
- High:
Step 1: Calculate gain (slope of the line):
Step 2: Calculate offset (y-intercept):
Step 3: Calibration equation:
Verification:
- At 410 counts: ✓
- At 3685 counts: ✓
Resolution calculation:
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:
Checkpoint: 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
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:
- Add a third reference point at 50% (midpoint)
- Capture three points: low (10%), mid (50%), high (90%)
- Use piecewise linear interpolation:
- For values < 50%: use low-to-mid segment
- For values >= 50%: use mid-to-high segment
- 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:
- Track the minimum reading over a 24-hour window
- Assume this minimum represents the “baseline” reference value
- Automatically adjust offset to correct drift
- 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.
Sensor calibration proceeds through Diagnose, Fit, Validate and Maintain. Gain and offset need reference conditions, residual error and a recorded recalibration trigger.
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.
| Concept | Description | When to Use |
|---|---|---|
| Offset Error | Sensor reads non-zero when true value is zero | Always needs correction |
| Gain Error | Sensor’s sensitivity differs from specification | When readings scale incorrectly |
| Two-Point Calibration | Uses two reference points to calculate offset and gain | Linear sensors (most common) |
| Multi-Point Calibration | Uses 3+ reference points with interpolation | Non-linear sensors (thermistors, pH) |
| Moving Average Filter | Averages N recent readings to reduce noise | Noisy environments, slow-changing signals |
| EEPROM Storage | Persists calibration across power cycles | Production deployments |
- 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.
Checkpoint: 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
0xCAFEso 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.
Two-Point Calibration Formula:
| Step | Formula | Description |
|---|---|---|
| 1 | gain = (actual_high - actual_low) / (raw_high - raw_low) | Calculate slope |
| 2 | offset = actual_low - (raw_low × gain) | Calculate y-intercept |
| 3 | calibrated = raw × gain + offset | Apply 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
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.
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.
Continue with Calibration Span Error and Validation to test reference-point spacing, residual checks, range guards, and release evidence for calibrated sensors.
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
| Concept | What You Learned | When to Apply |
|---|---|---|
| Two-Point Calibration | Calculate gain and offset from two known reference points | All linear sensors (temperature, pressure, light) |
| Moving Average Filter | Smooth noisy readings by averaging N recent samples | Noisy environments, before capturing calibration references |
| State Machine Design | Guide users through multi-step processes | Any interactive calibration or configuration procedure |
| Calibration Formula | calibrated = raw * gain + offset | Apply correction to every raw sensor reading |
| EEPROM Persistence | Store calibration across power cycles with magic number validation | Production deployments, field-calibrated devices |
| Automatic Drift Compensation | Track minimum over time window to correct sensor drift | Long-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.
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
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.
| Pitfall | Why It’s Bad | Solution |
|---|---|---|
| Reference points too close together | Small errors in reference measurement cause large errors in calculated gain | Use 10% and 90%, not 45% and 55% |
| Forgetting sensor warm-up time | Sensors drift significantly in first few minutes after power-on | Allow 5-30 minutes before calibration |
| Not filtering before capture | Single noisy sample can corrupt entire calibration | Apply moving average filter before capturing reference |
| Assuming calibration lasts forever | Sensors drift over time due to aging and environment | Recalibrate 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
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:
- Multi-Sensor Data Fusion - Combining calibrated data from multiple sensors
- Sensor Labs: Implementation and Review - Hardware and browser-lab workflow patterns
23.14 What’s Next
This section: What’s Next
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 pathsReviewed
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 stuckReviewed
How do interactive examples work?
Use the interactive guide to check how examples and simulations work before changing your project.
Interactive examples guideReviewed
