535  Sensor Calibration Techniques

Learning Objectives

After completing this chapter, you will be able to:

  • Understand why sensor calibration is necessary
  • Perform one-point and two-point calibration
  • Implement calibration routines in code
  • Maintain calibration over time

535.1 Prerequisites

535.2 Sensor Calibration

~25 min | Intermediate | P06.C08.U06

535.3 Every Sensor Needs Calibration - Here’s Why

Even the best sensors have manufacturing variations. Two DHT22 sensors from the same batch might read 0.5C apart when measuring the same temperature. Calibration corrects these individual differences.

Types of Sensor Errors:

Error Type Description Fixable with Calibration?
Offset (Bias) Constant error at all values Yes - one-point calibration
Gain (Scale) Error proportional to reading Yes - two-point calibration
Nonlinearity Curved error across range Partially - multi-point calibration
Noise Random variation No - requires filtering
Drift Error changes over time Requires periodic recalibration

535.4 One-Point Calibration (Offset Correction)

When to use: Sensor reads consistently high or low by a fixed amount.

Method: 1. Measure a known reference value 2. Calculate the offset: offset = sensor_reading - true_value 3. Subtract offset from all future readings

# One-Point Calibration Example

# Step 1: Measure reference (ice water = 0C)
reference_temp = 0.0
sensor_reading = 2.3  # Sensor reads 2.3C in ice water

# Step 2: Calculate offset
offset = sensor_reading - reference_temp  # offset = 2.3

# Step 3: Apply correction
def read_calibrated_temp():
    raw = sensor.read()
    return raw - offset  # Subtract 2.3 from all readings

535.5 Two-Point Calibration (Offset + Gain Correction)

When to use: Sensor has both offset error AND scale error.

Method: 1. Measure two known reference values (low and high) 2. Calculate scale and offset 3. Apply linear correction: corrected = (raw - offset) * scale

# Two-Point Calibration Example

# Step 1: Reference measurements
ref_low = 0.0    # Ice water
ref_high = 100.0  # Boiling water

raw_at_low = 1.8   # Sensor reads 1.8 in ice water
raw_at_high = 98.5  # Sensor reads 98.5 in boiling water

# Step 2: Calculate calibration coefficients
scale = (ref_high - ref_low) / (raw_at_high - raw_at_low)
# scale = (100 - 0) / (98.5 - 1.8) = 1.034

offset = ref_low - (raw_at_low * scale)
# offset = 0 - (1.8 * 1.034) = -1.86

# Step 3: Apply correction
def read_calibrated_temp():
    raw = sensor.read()
    return raw * scale + offset

535.6 Practical Calibration References

Measurement Reference Accuracy Notes
Temperature Ice water (0C) +/-0.1C Use crushed ice, stir
Temperature Boiling water (100C*) +/-0.5C *Altitude dependent
Temperature Room thermometer +/-0.5C Use NIST-traceable reference
Humidity Saturated salt (75.3% RH) +/-0.5% Lithium chloride solution
Humidity Saturated salt (33% RH) +/-0.5% Magnesium chloride
Pressure Weather station data +/-1 hPa Compare with local airport
Distance Tape measure +/-1mm Fixed distance reference

535.7 Multi-Point Calibration

When to use: Sensor has nonlinear response.

# Multi-Point Calibration with Interpolation

import numpy as np

# Calibration points (reference, raw_reading)
calibration_points = [
    (0.0, 1.8),
    (25.0, 26.5),
    (50.0, 52.1),
    (75.0, 77.8),
    (100.0, 98.5)
]

# Fit polynomial (linear for simple case)
refs = [p[0] for p in calibration_points]
raws = [p[1] for p in calibration_points]
coefficients = np.polyfit(raws, refs, deg=2)  # Quadratic fit

def read_calibrated():
    raw = sensor.read()
    return np.polyval(coefficients, raw)

535.8 Storing Calibration Data

Best Practice: Store calibration coefficients in non-volatile memory (EEPROM/flash):

import json

# Save calibration to file
calibration = {
    "sensor_id": "DHT22_001",
    "date": "2026-01-19",
    "scale": 1.034,
    "offset": -1.86,
    "reference_instrument": "NIST_thermometer_123"
}

with open("calibration.json", "w") as f:
    json.dump(calibration, f)

# Load calibration at startup
with open("calibration.json", "r") as f:
    cal = json.load(f)
    scale = cal["scale"]
    offset = cal["offset"]

535.9 Best Practices for Sensor Calibration

TipCalibration Checklist
  1. Document everything: Date, reference used, environmental conditions
  2. Use stable conditions: Allow sensor to warm up, avoid drafts
  3. Multiple readings: Average several readings at each point
  4. Recalibrate periodically: Quarterly for most sensors
  5. Verify after calibration: Check against a third reference
  6. Store coefficients safely: Non-volatile memory, backup

535.10 Calibration Schedule Guidelines

Sensor Type Recommended Interval Trigger Events
Temperature Every 6 months After shipping, extreme temps
Humidity Every 3 months After high humidity exposure
Pressure Every 12 months After altitude changes
Gas sensors Every 3-6 months After contamination
pH sensors Before each use After storage
Load cells Every 12 months After overload events

535.11 Common Calibration Mistakes

WarningAvoid These Mistakes
  1. Calibrating during warm-up: Wait for sensor to stabilize (30 min for gas sensors)
  2. Using inaccurate references: Your reference must be more accurate than your sensor
  3. Not accounting for environment: Temperature affects many sensors
  4. Forgetting to document: You’ll forget what you did in 6 months
  5. Over-calibrating: Too many points can introduce errors (overfitting)

535.12 Summary

Key calibration takeaways:

  1. All sensors benefit from calibration - Even factory-calibrated ones
  2. One-point for offset - Simple constant correction
  3. Two-point for offset + gain - Linear correction
  4. Multi-point for nonlinearity - Polynomial or lookup table
  5. Recalibrate regularly - Sensors drift over time

535.13 What’s Next

Now that you understand calibration:

Continue to Reading Datasheets ->