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
- Measure and record the raw code at the zero reference.
- Measure and record the raw code at the span reference.
- Build and run the conversion code.
- Test at zero, midpoint, and span.
- Move the mechanism slowly and check monotonic behavior.
- Repeat measurements to estimate noise and repeatability.
- 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.