Capture two references and validate a calibration
Fit gain and offset from two potentiometer references, then check a fresh midpoint and reset the correction.

Physics Phoebe: I want you to fit the two references, then test a reading you did not use for the fit.
Predict the reading, then compare it with the measurement.
Wokwi ESP32
Third party ToolOpen the ESP32 editor, paste diagram.json, then paste sketch.ino.
Open Wokwi to paste in the files (new tab)Get the files
Use both prepared files. This is a paste-in setup; saving a project requires a Wokwi account.
sketch.ino
- Use the launch button above to open the ESP32 editor in Wokwi.
- Select the editor’s diagram.json tab and replace all its text with the supplied diagram.json.
- Select the sketch.ino tab, replace all its text with the supplied sketch.ino, then click Start Simulation.
Steps
Step 1
- Do
- In Wokwi's project editor, paste diagram.json and sketch.ino into a new Wokwi ESP32 project; inspect the potentiometer and status LED connections.
- You will see
- LED anode connects to ESP32 GPIO2 (diagram.json line 55). LED cathode returns through ledR to ESP32 ground (diagram.json lines 61, 67). The LED’s series resistor ledR is 220 Ω (diagram.json line 24). Power: potentiometer VCC connects to ESP32 3.3 V; GND returns to ground (diagram.json lines 73, 79). Potentiometer signal connects to ESP32 GPIO34 (diagram.json line 85).
- Why it matters
- GPIO34 supplies the raw ADC endpoints used later for calibration. The status LED on GPIO2 reports the procedure state through a current-limited path.

Step 1 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) Step 2
- Do
- In the Simulation toolbar, start the run; read steady midpoint values in Serial Monitor.
- You will see
- Input: supplied potentiometer midpoint. Observed: "RAW: 50.1% | FILTERED: 50.1% | CALIBRATED: 50.1% | CORRECTION: 0.00%". Observed: the same row repeats before calibration. Expected: uncalibrated midpoint has zero correction.
- Why it matters
- The raw and filtered readings agree at 50.1%. The calibrated value begins at the same value. The correction is zero before reference capture. This is the baseline for the next two points.

Step 2 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) Step 3
- Do
- In the Serial Monitor input field, send c to start the calibration procedure.
- You will see
- Input: send c. Observed: "CALIBRATION PROCEDURE STARTED". Observed: "Step 1: Set the potentiometer to the LOW reference point". Observed: "(This represents 10% actual value)".
- Why it matters
- The sketch enters its calibration procedure. It requests the low reference first. The declared actual value is 10%. A reference must be captured before the gain is computed.

Step 3 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) Step 4
- Do
- In the potentiometer control panel, set 153 of 1023; then send l in Serial Monitor.
- You will see
- Input: potentiometer control 153, then command l. Observed: "LOW POINT CAPTURED: Raw = 14.9% (Actual = 10%)". Observed: the monitor next requests the HIGH reference. Expected: store 14.9% raw as the 10% actual reference.
- Why it matters
- The sensor model reads above the declared actual low point. The command records both raw and actual values. The later high point is still needed for a two-point fit. The 14.9% value matches the real screenshot.

Step 4 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) Step 5
- Do
- In the potentiometer control panel, set 870 of 1023; then send h in Serial Monitor.
- You will see
- Input: potentiometer control 870, then command h. Observed: "HIGH POINT CAPTURED: Raw = 85.1% (Actual = 90%)". Observed: "Low point: Raw=14.9% -> Calibrated=10.0% (Expected: 10.0%)". Observed: "High point: Raw=85.1% -> Calibrated=90.0% (Expected: 90.0%)".
- Why it matters
- The high raw value falls below its declared actual reference. The two captured pairs define a linear correction. Verification maps the low reference to 10.0%. Verification maps the high reference to 90.0%.
Calibrated=10.0% (Expected: 10.0%) High point: Raw=85.1% -> Calibrated=90.0% (Expected: 90.0%). The ring marks the endpoint verification." loading="lazy">Step 5 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) Step 6
- Do
- In the potentiometer control panel, restore 512 and read settled Serial Monitor rows.
- You will see
- Input: potentiometer control restored to 512. Observed: "RAW: 50.1% | FILTERED: 50.1% | CALIBRATED: 50.1% | CORRECTION: 0.01%". Observed: the settled row repeats. Expected: midpoint returns with a small correction.
- Why it matters
- The raw and filtered readings settle together. The calibrated reading is 50.1%. The residual correction is 0.01%. This midpoint checks interpolation between references.

Step 6 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab) Step 7
- Do
- In the Serial Monitor input field, send r and read the next steady row.
- You will see
- Input: send r after midpoint has settled. Observed: "Calibration RESET to defaults (gain=1.0, offset=0.0)". Observed: "RAW: 50.1% | FILTERED: 50.1% | CALIBRATED: 50.1% | CORRECTION: 0.00%". Expected: default gain and offset remove the correction.
- Why it matters
- The gain returns to one. The offset returns to zero. The displayed correction returns to 0.00%. The midpoint remains the same controlled input.

Step 7 · Wokwi ESP32; numbered callout added to a real capture. Enlarge screenshot (new tab)
Chapter checks
These questions refer to the chapter’s examples. Use the return links to review their answers.
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 checkIn the lab code, why is the moving average filter applied BEFORE calibration, not after?
Return to the chapter’s knowledge check
Return to Sensor Calibration: Math and Validation · Browse Labs