Skip to content

Two-point sensor calibration and residual error

Fit two reference points, then use span validation to identify nonlinear calibration error.

Physics Phoebe, your practice guide

Physics Phoebe
Physics Phoebe: fit the references, then test an unused reading.
Predict the reading, then compare it with the measurement.

Python 3 in your browser (JupyterLite)

Python · no install

Tier 2 · Web · paste-in setup · No account

Last tested October 2026.

Open the notebook in your browser and run each Python cell; no install or account is needed.

Three ways to run: use JupyterLite here with no install; run main.py locally from the downloadable lab folder; or open the same notebook in Google Colab.

Open in your browser (new tab)

Steps

The tool may have moved on since the screenshots were taken — the text steps are the contract.

  1. 1 Step 1

    Do
    In the JupyterLite notebook, run step 1 to inspect the synthetic equation and the two reference readings.
    You will see

    Run the cell. You should see:

    • SYNTHETIC temperature fixture; seed=607; no random draws; raw(T)=1.4+0.88*T+0.0015*(T-50)^2; reference_C raw_units
    Why it matters
    The fixture and seed make the input reproducible.
    Code for step 1
    import platform
    import pyodide
    print(f"Runtime: Python {platform.python_version()} / Pyodide {pyodide.__version__}")
    
    """Two-point calibration on an explicitly synthetic, nonlinear sensor fixture."""
    import math
    
    SEED = 607  # fixed fixture identifier; formula contains no random draws
    REFERENCES_C = (0, 100)
    VALIDATION_C = tuple(range(0, 101, 10))
    
    
    def raw(celsius):
        return 1.4 + 0.88 * celsius + 0.0015 * (celsius - 50) ** 2
    
    
    def fit():
        low, high = REFERENCES_C
        gain = (high - low) / (raw(high) - raw(low))
        offset = low - gain * raw(low)
        return gain, offset
    
    
    def rows():
        gain, offset = fit()
        return [(t, raw(t), gain * raw(t) + offset, gain * raw(t) + offset - t)
                for t in VALIDATION_C]
    
    
    def run_step(n):
        gain, offset = fit()
        data = rows()
        if n == 1:
            lines = ["SYNTHETIC temperature fixture; seed=607; no random draws",
                     "raw(T)=1.4+0.88*T+0.0015*(T-50)^2",
                     "reference_C raw_units", *[f"{t:>11} {raw(t):>9.3f}" for t in REFERENCES_C],
                     "Endpoints are the only points used for fitting."]
        elif n == 2:
            lines = ["Two-point fit: corrected_C=gain*raw+offset",
                     f"raw span={raw(100)-raw(0):.3f}; reference span=100.000 C",
                     f"gain={gain:.6f} C/raw_unit; offset={offset:.6f} C",
                     f"at 0 C: corrected={gain*raw(0)+offset:.3f} C",
                     f"at 100 C: corrected={gain*raw(100)+offset:.3f} C"]
        elif n == 3:
            lines = ["Independent validation grid: endpoints were fit, interior was not",
                     "true_C raw corrected_C residual_C",
                     *[f"{t:>6} {r:>6.2f} {c:>11.2f} {e:>10.2f}" for t, r, c, e in data[::2]]]
        elif n == 4:
            peak = max(data, key=lambda row: abs(row[3]))
            lines = ["Residual error plot data (corrected minus true)",
                     "true_C residual_C plotted scale: each # ~0.5 C",
                     *[f"{t:>6} {e:>10.2f} {'#'*round(abs(e)*2)}" for t, _, _, e in data[::2]],
                     f"largest |residual|={abs(peak[3]):.2f} C at {peak[0]} C"]
        elif n == 5:
            interior = [abs(row[3]) for row in data[1:-1]]
            lines = ["RESULT CARD: two-point calibration on synthetic readings",
                     f"endpoints residuals={data[0][3]:.3f}, {data[-1][3]:.3f} C",
                     f"interior mean |error|={sum(interior)/len(interior):.3f} C",
                     f"interior max |error|={max(interior):.3f} C",
                     "Decision: validate interior points before accepting a linear correction.",
                     "This does not establish accuracy for a physical sensor."]
        else:
            raise ValueError(n)
        lines.append(f"STEP {n} complete")
        print("\n".join(lines))
    
    
    
    run_step(1)
    Real JupyterLite notebook output for step 1 of Two-point sensor calibration and residual error.
    Step 1 · Python 3 in your browser (JupyterLite); numbered callout added to a real capture.
  2. 2 Step 2

    Do
    In the JupyterLite notebook, run step 2 to fit gain and offset from the two endpoints.
    You will see

    Run the cell. You should see:

    • Two-point fit: corrected_C=gain*raw+offset; raw span=88.000; reference span=100.000 C; gain=1.136364 C/raw_unit; offset=-5.852273 C
    Why it matters
    A linear fit must interpolate the two references exactly.
    Code for step 2
    run_step(2)
    Real JupyterLite notebook output for step 2 of Two-point sensor calibration and residual error.
    Step 2 · Python 3 in your browser (JupyterLite); numbered callout added to a real capture.
  3. 3 Step 3

    Do
    In the JupyterLite notebook, run step 3 to test the corrected values across the span.
    You will see

    Run the cell. You should see:

    • Independent validation grid: endpoints were fit, interior was not; true_C raw corrected_C residual_C; 0 5.15 0.00 0.00
    Why it matters
    Unused interior points test whether the line generalizes.
    Code for step 3
    run_step(3)
    Real JupyterLite notebook output for step 3 of Two-point sensor calibration and residual error.
    Step 3 · Python 3 in your browser (JupyterLite); numbered callout added to a real capture.
  4. 4 Step 4

    Do
    In the JupyterLite notebook, run step 4 to plot residual error versus true temperature.
    You will see

    Run the cell. You should see:

    • Residual error plot data (corrected minus true); true_C residual_C plotted scale: each # ~0.5 C; 0 0.00
    Why it matters
    A residual plot reveals curvature hidden by endpoint checks.
    Code for step 4
    run_step(4)
    Real JupyterLite notebook output for step 4 of Two-point sensor calibration and residual error.
    Step 4 · Python 3 in your browser (JupyterLite); numbered callout added to a real capture.
  5. 5 Step 5

    Do
    In the JupyterLite notebook, run step 5 to compare interior and endpoint error.
    You will see

    Run the cell. You should see:

    • RESULT CARD: two-point calibration on synthetic readings; endpoints residuals=0.000, 0.000 C; interior mean |error|=3.125 C
    Why it matters
    Span validation is required before accepting a correction.
    Code for step 5
    run_step(5)
    Real JupyterLite notebook output for step 5 of Two-point sensor calibration and residual error.
    Step 5 · Python 3 in your browser (JupyterLite); numbered callout added to a real capture.

Chapter checks

These questions refer to the chapter’s examples. Use the return links to review their answers.

  1. A sensor reads 20% when the true value is 10%, and reads 80% when the true value is 90%. What is the gain (slope) coefficient?

    Return to the chapter’s knowledge check
  2. You need to calibrate a soil moisture sensor that will measure between 20% and 80% moisture in a greenhouse. Which reference points should you use?

    Return to the chapter’s knowledge check

Caution

Before you start

Read this before you start.

The seed 607 fixture is synthetic and uses no random draws. Its nonlinear readings do not establish accuracy for any physical sensor.

Technical details
Version tested
Version tested: Python 3.12.7 / Pyodide 0.27.6 in JupyterLite 0.6.4; Chromium 148.0.7778.96; captureSource playwright:jupyterlite. Date: 2026-10-08.
QR code that opens the Two-point sensor calibration and residual error lab page
Open this lab online: https://iotclass.org/labs/sensor-calibration-residuals.html

Return to Sensor Calibration: Math and Validation · Browse Labs