Loading header...

Exercise: Calibrate a Sensor

This exercise builds a practical calibration workflow. You will collect two known points, compute scale and offset, convert raw readings into engineering units, and verify the result.

Learning Objectives

You will:

  • perform two-point calibration;
  • implement fixed-point conversion code;
  • check expected behavior at known points;
  • identify offset, gain, noise, and wiring faults;
  • document an extension challenge and explained solution.

Prerequisites

  • Basic ADC concepts.
  • Ability to compile C code on a host or embedded target.
  • A sensor or simulated voltage source with two known reference points.

Task

A linear displacement sensor outputs ADC codes. At 0 mm, the measured code is 820. At 100 mm, the measured code is 3270. Write code that converts any raw code to displacement in 0.01 mm units.

Implementation

#include <stdint.h>
#include <stdio.h>

#define CODE_ZERO 820
#define CODE_SPAN 3270
#define POS_ZERO_CENTI_MM 0
#define POS_SPAN_CENTI_MM 10000

static int32_t code_to_centi_mm(int32_t code) {
    if (code <= CODE_ZERO) return POS_ZERO_CENTI_MM;
    if (code >= CODE_SPAN) return POS_SPAN_CENTI_MM;
    return (code - CODE_ZERO) *
           (POS_SPAN_CENTI_MM - POS_ZERO_CENTI_MM) /
           (CODE_SPAN - CODE_ZERO);
}

int main(void) {
    int32_t tests[] = {820, 2045, 3270};
    for (unsigned i = 0; i < 3; ++i) {
        int32_t pos = code_to_centi_mm(tests[i]);
        printf("code=%ld position=%ld.%02ld mm\n",
               (long)tests[i], (long)(pos / 100), (long)(pos % 100));
    }
    return 0;
}

Expected Behavior

Raw code Expected position
820 0.00 mm
2045 50.00 mm
3270 100.00 mm

Verification Steps

  1. Measure and record the raw code at the zero reference.
  2. Measure and record the raw code at the span reference.
  3. Build and run the conversion code.
  4. Test at zero, midpoint, and span.
  5. Move the mechanism slowly and check monotonic behavior.
  6. Repeat measurements to estimate noise and repeatability.
  7. Save calibration constants with version and units.

Common Failure Symptoms

Symptom Likely cause
output offset at zero wrong zero code or mechanical preload
correct zero but wrong span wrong span point or gain calculation
jumps randomly noisy signal, loose connector, ADC reference noise
decreases when motion increases sensor wired backwards
saturates early input range or mechanical travel mismatch

Debugging Guidance

  • Print raw code before calibrated position.
  • Confirm ADC reference and sensor supply.
  • Check sensor output with a meter while moving slowly.
  • Average several readings only after confirming wiring is correct.
  • Recalibrate after mechanical mounting changes.

Extension Challenge

Add a third verification point at 75 mm. If the converted value is outside +/-0.5 mm, report a calibration warning. Then add EEPROM or flash storage for calibration constants.

Explained Solution

Two-point calibration uses a straight line. The zero code defines the offset. The difference between span and zero codes defines gain. The conversion subtracts the zero code, multiplies by the engineering-unit span, and divides by the code span. Clamping prevents readings outside the calibrated travel from producing unsafe positions.

Summary

Calibration turns raw sensor values into trustworthy engineering units. The workflow is measure known points, compute scale and offset, verify intermediate points, check repeatability, and preserve calibration constants with clear units.

Further Reading

  • NIST Engineering Statistics Handbook: calibration concepts.
  • Texas Instruments sensor calibration and linearization application notes.
  • IEC 60770 transmitter performance methods for industrial-process measurement.

Mind Map

mindmap root((Sensor Calibration)) Core task Two known points Offset and span Convert code to mm Verify midpoint Formula Position equals code minus zero Multiply by span units Divide by code span Clamp outside range Implementation Fixed point centi mm Store constants Print raw first Verification Zero point Midpoint Span point Repeatability Monotonic travel Common mistakes Wrong units Sensor reversed No raw logging Cal after mount change