Skip to content

Sensors & Signal Processing

How to calibrate a sensor with two known points

Two-point calibration turns a sensor's raw readings into real units. Take one reading at a known low value, like an empty scale, and one at a known high value, like a 100 kg test weight. The first gives the offset, the reading at zero; the two together give the gain, how many counts each unit adds. Then every reading converts with one subtraction and one division.

Offset and gain

Most sensors are close to a straight line: raw reading = offset + gain × value. A load cell might read 87 counts with nothing on it and 817 with 100 kg. So:

gain = 817 − 87 over 100 kg = 7.3 counts per kg

To convert any reading, take off the offset and divide by the gain:

value = reading − 87 over 7.3

A reading of 379 is (379 − 87) ÷ 7.3 = 40 kg.

In code

Measure the two raw readings on the real sensor, and let the code work out the rest:

const int zeroRaw = 87;    // reading with nothing on it
const int spanRaw = 817;   // reading with the 100 kg weight

float kilograms(int reading) {
  float gain = (spanRaw - zeroRaw) / 100.0;   // counts per kg
  return (reading - zeroRaw) / gain;
}

Live experiment

Counts into kilograms

A load cell reads 87 counts empty and 7.3 more for every kilogram, with a little noise. Run the code with the offset only, the gain only, whole-number division, or both done properly, through an empty scale, the 100 kg test weight and then 40 kg.

Try it yourself: free lesson

“Calibration” is lesson 1 of 5 in Dither Delta, Sensors & Signal Processing World 2. Free with an account. Lessons unlock in order, so you’ll do the 7 before it first.

Both numbers, every sensor

Fixing only one part leaves an error. Take off the offset but forget the gain, and 100 kg reads 730, the raw counts. Divide by the gain but forget the offset, and an empty scale reads about 11.9 kg. And the numbers belong to one sensor: another load cell of the same model has its own zero and its own counts per kilogram, so each one is calibrated on its own.

A tare button sets just the offset, from whatever's on the scale now, so a bowl reads as zero. The gain stays as the factory set it with a known weight.

When a line isn't enough

Two points fix a straight line, so two-point calibration is only as good as the sensor is straight. A load cell or a strain gauge is very close. A thermistor curves, so a line through two temperatures is right at those two and drifts away elsewhere. For curved sensors, use more points: a lookup table of known readings, with a straight line between each neighbouring pair, or the sensor's own equation. And calibrate with averaged readings, so noise doesn't sneak into the offset and gain themselves.

Free lesson

Now build it yourself

Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.

Try it yourself: free lesson

“Calibration” is lesson 1 of 5 in Dither Delta, Sensors & Signal Processing World 2. Free with an account. Lessons unlock in order, so you’ll do the 7 before it first.

Or start from the very first lesson, no account needed

Practice questions

Three quick questions from the lessons. Pick an answer to check it.

  1. Question 1 of 3Want a smaller error from the same table method? Add more points…
    Show the answer

    Answer: Where the curve bends most. Straight-line interpolation is worst where the curve is most curved. Extra points there buy the most accuracy.

    • Why not “It makes no difference”? More points shorten each straight piece, so the error does shrink, most of all where the curve bends.
    • Why not “Only at the ends”? The ends aren't where a straight line is worst; the error is biggest where the curve bends most.
    • Why not “Evenly, anywhere”? Evenly spread points help, but some are wasted on straight stretches where a line already fits well.
  2. Question 2 of 3Typing the last cell's 87 and 7.3 into the code…
    Show the answer

    Answer: Is wrong for this cell: every cell has its own offset and gain. That's the point of calibrating: the code takes its numbers from zeroRaw and spanRaw, measured on this cell.

    • Why not “Is only a bit off”? The error isn't small: a different offset and gain can be off by many kilograms.
    • Why not “Works for every cell”? Every load cell has its own zero reading and counts per kilogram, so one cell's numbers don't fit another.
    • Why not “Doesn't compile”? It compiles fine. The numbers are simply wrong for this cell, which is why each one is calibrated.
  3. Question 3 of 3Two-point calibration finds…
    Show the answer

    Answer: The offset and the gain. Two points fix a straight line: where it starts and how steep it is.

    • Why not “The noise”? Noise is reduced by averaging, not by calibration.
    • Why not “The curve's shape”? Two points fix a straight line. A curve's shape needs more points.
    • Why not “Only the offset”? Two points give the slope as well as the start. One point alone would give only the offset.
Keep practising: free lesson

“Calibration” is lesson 1 of 5 in Dither Delta, Sensors & Signal Processing World 2. Free with an account. Lessons unlock in order, so you’ll do the 7 before it first.

Questions people ask

What is two-point calibration?

Measuring a sensor's raw reading at two known values, like an empty scale and a known weight, and using them to fix a straight line from reading to real value. The two points give the offset, where the line starts, and the gain, how steep it is.

What is the difference between offset and gain error?

An offset error shifts every reading by the same amount, like a scale that shows 2 kg with nothing on it. A gain error scales readings by the wrong factor, so the error grows with the value, like a scale that reads 110 kg for 100.

What does the tare button on a scale do?

It sets the offset: it records the current reading, with the bowl on the scale, and subtracts it from every reading after. It doesn't change the gain, which was set at the factory with a known weight.

When is two-point calibration not enough?

When the sensor's response is curved, like a thermistor. A straight line through two points is right at those points and drifts away from the curve elsewhere. Use more points with a lookup table, or the sensor's own equation.

Every topic