Projects / Two-point sensor calibration

Two-point sensor calibration

Intermediate45 minGuided buildReviewed

Build it step by step

0 of 8 done
  1. NANO

    Wire both sensors

    Place them touching each other.

  2. NANO

    Upload with the defaults

    The error columns show the raw difference.

  3. NANOTMP36DS18B2028.4°C

    Record the low point

    Note both readings at room temperature once settled.

  4. NANO

    Record the high point

    Put both sensors in a bag in warm water around 40 °C and note both readings.

  5. NANO

    Fill in the constants

    Enter RAW1, REF1, RAW2 and REF2 in the code.

  6. NANO

    Upload again

    The corrected column should now track the reference.

  7. NANOTMP36DS18B2028.4°C

    Check a middle value

    Test at a third temperature and compare error before and after.

  8. NANO

    Plot the error

    Chart error before and after in a spreadsheet.

In short

How do you build a two-point sensor calibration?

Calibrate a cheap sensor against a better one with offset and gain, and see the error fall. You need Arduino Nano, TMP36 temperature sensor, DS18B20 Temperature Sensor, Resistor 4.7 kΩ, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 8 steps and about 45 minutes.

Level
Intermediate
Time
About 45 min
Parts
6

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
TMP36 pin 1 (+Vs)Nano 5VPower
TMP36 pin 2 (Vout)Nano A0Analogue output, 10 mV per degree with 500 mV offset
TMP36 pin 3 (GND)Nano GNDGround
DS18B20 pin 1 (GND)Nano GNDGround
DS18B20 pin 2 (DQ)Nano D2 and 4.7 kΩ resistor to 5VOne-wire data
DS18B20 pin 3 (VDD)Nano 5VPower

Code

Calibration compares your sensor against a better reference at two points far apart, then fits a straight line: corrected = REF1 + (raw - RAW1) × (REF2 - REF1) ÷ (RAW2 - RAW1). The offset and gain together fix both a constant error and a slope error. The DS18B20 is accurate to about 0.5 °C out of the box, so it is a fair reference for the cheaper TMP36, which can be off by 1 to 2 °C. Put both sensors close together and wait for them to settle. Seal them in a thin plastic bag before putting them in water. Do not use boiling water; stay under about 50 °C with warm tap water. The result is only as good as the reference. Arduino IDE, board "Arduino Nano" (processor ATmega328P, or ATmega328P (Old Bootloader) for clones).

Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

// Libraries: OneWire and DallasTemperature.
#include <OneWire.h>
#include <DallasTemperature.h>

OneWire ow(2);
DallasTemperature ds(&ow);

// Fill these after recording two points (see the steps)
float RAW1 = 24.0, REF1 = 24.0;    // low point: TMP36 reading and DS18B20 reading
float RAW2 = 40.0, REF2 = 40.0;    // high point

float readTMP36() {
  long sum = 0;
  for (int i = 0; i < 64; i++) { sum += analogRead(A0); delay(2); }
  float mv = (sum / 64.0) * 5000.0 / 1023.0;
  return (mv - 500.0) / 10.0;
}

float correct(float raw) {
  float gain = (REF2 - REF1) / (RAW2 - RAW1);
  return REF1 + (raw - RAW1) * gain;
}

void setup() {
  Serial.begin(9600);
  ds.begin();
  Serial.println("ref_ds18b20, tmp36_raw, tmp36_corrected, error_before, error_after");
}

void loop() {
  ds.requestTemperatures();
  float ref = ds.getTempCByIndex(0);
  float raw = readTMP36();
  float cor = correct(raw);
  Serial.print(ref, 2); Serial.print(", ");
  Serial.print(raw, 2); Serial.print(", ");
  Serial.print(cor, 2); Serial.print(", ");
  Serial.print(raw - ref, 2); Serial.print(", ");
  Serial.println(cor - ref, 2);
  delay(1000);
}

What the serial monitor should show

Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual Arduino Nano with its sensors at their default values. Your numbers will follow your real readings, but the format should match.

ref_ds18b20, tmp36_raw, tmp36_corrected, error_before, error_after
24.50, 200.23, 200.23, 175.73, 175.73
24.50, 200.18, 200.18, 175.68, 175.68
24.50, 200.24, 200.24, 175.74, 175.74
24.50, 200.22, 200.22, 175.72, 175.72

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Before calibrationTMP36 off by one or more degrees
After two-point calibrationError within about 0.5 °C at the calibration points
At a third temperatureError smaller than before
Sensors not touchingLarger, drifting errors

If it does not work

DS18B20 reads -127

Check the 4.7 kΩ pull-up and wiring.

TMP36 noisy

Averaging is in the code; add a 100 nF capacitor on the output.

Corrected is worse

Check RAW and REF values are not swapped.

Water short circuit

Keep the sensors sealed in a bag.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the two-point sensor calibration?

Arduino Nano, TMP36 temperature sensor, DS18B20 Temperature Sensor, Resistor 4.7 kΩ, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).

How long does the two-point sensor calibration take to build?

About 45 minutes for a intermediate maker, in 8 steps.

How do I know the two-point sensor calibration works?

Before calibration: TMP36 off by one or more degrees. After two-point calibration: Error within about 0.5 °C at the calibration points. At a third temperature: Error smaller than before.

DS18B20 reads -127: what should I check?

Check the 4.7 kΩ pull-up and wiring.

TMP36 noisy: what should I check?

Averaging is in the code; add a 100 nF capacitor on the output.

Corrected is worse: what should I check?

Check RAW and REF values are not swapped.

Water short circuit: what should I check?

Keep the sensors sealed in a bag.