Chapters

39 Temperature Sensor Labs: Comparing Sensor Types

sensors
sensor
temperature

39.1 Start With the Decision

Picture a freezer sensor showing 85 C just after power returns. The number is valid data but not a valid freezer reading.

39.2 Route Overview

This is part 1 of 2. Continue with Temperature Sensor Labs: Thermocouple Measurement.

39.3 Part Objectives

  • Compare temperature sensor types by range, accuracy, interface, and field limits.
  • Trace a DS18B20 reading through wiring, conversion, validation, and fault checks.

39.4 Start With the Measurement Story

Prove One Temperature Before Trusting the Display

Picture a freezer sensor showing 85 C just after power returns. The number is valid data but not a valid freezer reading. The lab must separate a real temperature from a startup or link fault.

A cyclic redundancy check, or CRC, is a short code used to detect changed data. General purpose input output, or GPIO, means a configurable pin on a small computer board. Both help prove the reading path.

Measure known cold, room, and warm points. Delay a read, remove the sensor, disturb the pull-up, restart the board, and record raw value, check result, address, time, unit, lag, and final decision.

This runway does not prove accuracy across every range or mounting condition. The deeper sections compare sensor types, wiring, conversion timing, calibration, error codes, and heat-flow limits.

A temperature lab is not just a wiring exercise. The useful story is heat moving through the sensor package, the reading lagging behind reality, and calibration evidence showing when the number can be trusted.

39.5 In 60 Seconds

The DS18B20 is a digital temperature sensor using a 1-Wire bus, allowing multiple sensors on a single GPIO pin with unique 64-bit addresses. The DHT22 measures both temperature and humidity but requires a 2-second minimum interval between reads. Thermocouples handle extreme temperatures using the Seebeck effect. Always use proper pull-up resistors (4.7k ohm for 1-Wire, 10k ohm for DHT) and watch for error values like 85.0C (conversion not ready) and -127C (CRC error).

Chapter Roadmap
  • Start With the Measurement Story
  • In 60 Seconds
  • Key Concepts
  • Prerequisites
  • For Beginners: Temperature Sensor Basics
  • Temperature Sensors Overview
  • DS18B20 (1-Wire Digital Temperature)
  • Checkpoint: DS18B20 Measurement Path

This lab moves from dependable first readings to field-ready evidence:

  1. First you compare the major sensor families and set up a safe DS18B20 measurement path.
  2. Then you decode 1-Wire raw values, timing, pull-ups, and the 85C and -127C failure signatures.
  3. Next you contrast thermocouples, DHT22 timing, humidity behavior, and multi-sensor lab exercises.
  4. Finally you choose a sensor for the application, test yourself with quizzes, and carry the thermistor/RTD material into the next chapter.

Checkpoints recap the operational decisions; deeper calculators and audits are support material when you need the arithmetic.

39.6 Key Concepts

Start with Thermistor: a temperature-sensitive resistor whose resistance changes predictably with temperature, available as NTC (negative temperature coefficient) or PTC (positive temperature coefficient) types. Then RTD: Resistance Temperature Detector — a precision temperature sensor using pure metal wire (platinum PT100/PT1000) whose resistance increases linearly with temperature. Next Thermocouple: a temperature sensor formed by joining two dissimilar metals that generate a voltage proportional to temperature difference via the Seebeck effect. After that ADC Resolution: the number of discrete output values an analog-to-digital converter can produce; higher resolution (more bits) means finer temperature discrimination. Continue by Voltage Divider: a circuit using two resistors in series that produces an output voltage proportional to the ratio of resistances, used to read thermistor values with a microcontroller. Continue by Calibration: the process of comparing sensor readings to known reference values and applying correction factors to improve measurement accuracy. Finally Signal Conditioning: amplifying, filtering, or converting a raw sensor signal into a form suitable for analog-to-digital conversion and microcontroller processing.

The mathematical gist. A 3.3 V, 12-bit converter steps by 0.806 mV. At the chapter’s 1.00 mA excitation, a Pt100 changes by 0.385 mV/C and therefore resolves only 2.09 C/count before amplification; a Pt1000 changes by 3.85 mV/C and reaches 0.209 C/count. The same current also causes I2RI^2R self-heating: 0.100 mW and 1.00 mW respectively.

Math Bridge · guided foundationsWhy does excitation current help and heat an RTD?Let Phoebe connect resistance slope, ADC count size, and self-heating for Pt100 and Pt1000.

39.7 Learning Objectives

By the end of this chapter, you will be able to:

  • Interface DS18B20 sensors: Configure 1-Wire bus and read multiple temperature sensors on a single pin
  • Analyse thermocouple operation: Explain how thermocouples generate temperature-dependent voltages via the Seebeck effect and calculate output voltages using the Seebeck coefficient
  • Evaluate humidity sensors: Differentiate capacitive, resistive, and thermal conductivity sensing methods based on accuracy, response time, and application suitability
  • Construct temperature monitoring systems: Wire and program robust temperature acquisition circuits with correct pull-up resistors and multi-sensor addressing
  • Diagnose common sensor errors: Interpret the meaning of 85C, -127C, and NaN readings and apply targeted fixes for each failure mode

39.8 Prerequisites

Required Knowledge:

Hardware Requirements:

  • Arduino Uno/Nano or ESP32 development board
  • Breadboard and jumper wires
  • DS18B20 temperature sensor (or DHT22)
  • 4.7kOhm pull-up resistor (for DS18B20)
  • USB cable for programming

Software Requirements:

  • Arduino IDE installed and configured
  • Required libraries: OneWire, DallasTemperature (for DS18B20), DHT sensor library (for DHT22)

Skills Checklist:

  • Can upload code to Arduino/ESP32
  • Can read basic circuit diagrams
  • Can use serial monitor for debugging
  • Can install Arduino libraries via Library Manager

39.9 For Beginners: Temperature Sensor Basics

Temperature sensors are among the most common sensors in IoT applications. Here is what makes them special:

Why Temperature Matters in IoT:

  • HVAC systems adjust heating/cooling based on readings
  • Cold chain monitoring ensures vaccine/food safety
  • Industrial processes require precise temperature control
  • Weather stations track environmental conditions

Common Temperature Sensor Types:

SensorInterfaceAccuracyBest For
DS18B201-Wire digital+/-0.5C (in -10 to +85C range)Multi-sensor chains
DHT22Proprietary digital+/-0.5CTemp + humidity combo
ThermistorAnalog+/-1C (with calibration)Low cost, fast response
ThermocoupleAnalog (amplifier needed)+/-2CExtreme temperatures

Tip: DS18B20 is great for beginners because each sensor has a unique ID, allowing multiple sensors on one wire!

39.10 Temperature Sensors Overview

Implementing a temperature sensor project follows a structured workflow. Use Figure 39.1 to see why deployment is the end of an evidence chain rather than the first time a reading is trusted. Follow the main path first, then inspect the debug loops that send failed checks back to an earlier decision.

Six-step sensor implementation workflow from research through wiring, coding, testing, calibration to deployment with debug feedback loop
Figure 39.1: Sensor Implementation Workflow: From Research to Deployment with Debug Loops

Read Figure 39.1 from research through wiring, code, test, calibration, and deployment. A failed electrical check returns to wiring; an implausible or biased value returns to code, reference testing, or calibration. Those loops matter because firmware cannot compensate for every wiring fault, and calibration cannot legitimise an unstable interface. The workflow supplies the running structure for each device lab below.

Before applying power, turn that workflow into a concrete inspection with Figure 39.2. Read the pre-power column first, then the first-test checks, and finally the symptom-to-cause troubleshooting path.

Sensor lab preparation checklist with pre-power checks, first test steps, and troubleshooting guide for common failures like no response means wiring issue, wrong values indicate voltage or timing problems, unstable readings need filtering, and drift requires calibration.
Figure 39.2: Lab checklist view: Before powering on, verify voltage levels (3.3V vs 5V), ground connections, correct GPIO pins, and required pull-up resistors. First test should read raw values and check against known reference (ice water = 0C). Common failures have specific fixes: no response means wiring issue, wrong values indicate voltage/timing problems, unstable readings need filtering, and drift requires calibration.

In Figure 39.2, voltage, ground, GPIO choice, and pull-up presence are acceptance conditions before energising the circuit. The first reading is then compared with a known reference rather than judged by plausibility alone. Finally, no response, wrong values, instability, and drift point to different investigations. This checklist turns the overview’s broad loop into evidence you can record for every sensor in the chapter.

The overview gives you the workflow. The first detailed device is the DS18B20 because it turns wiring, addressing, timing, and error handling into one compact lab.

39.11 DS18B20 (1-Wire Digital Temperature)

Time: ~25 min | Level: Intermediate | Code: P06.C10.U01

The DS18B20 is one of the most popular digital temperature sensors for IoT projects. It uses the 1-Wire protocol, which means multiple sensors can share a single GPIO pin — each sensor has a factory-programmed unique 64-bit address that distinguishes it on the bus.

Specifications:

  • Temperature Range: -55C to 125C (+/-0.5C accuracy in -10C to +85C range; +/-2C outside that range)
  • Interface: 1-Wire (multiple sensors on one pin)
  • Resolution: 9-12 bit configurable (93.75 ms to 750 ms conversion time)
  • Power: 3.0-5.5V, parasitic power mode available

Before placing multiple devices on one GPIO, inspect Figure to connect the small three-lead package with its internal digital identity. The shared wire works because addressing and conversion timing are explicit parts of the protocol.

A black TO-92 package DS18B20 temperature sensor with three metal leads, next to a short length of cable
A DS18B20 in its TO-92 package. Each one of these has a factory-programmed unique 64-bit address baked in, which is exactly what lets several of them share the single 1-Wire GPIO pin described above without colliding. Photo: SparkFun Electronics, CC BY 2.0

Read Figure, identify power, ground, and data at the package, then connect the single data lead to the unique 64-bit address stored inside each sensor. That relationship explains how discovery, conversion delay, and per-device reads must be verified before several DS18B20s can share the bus reliably.

What to Observe Before Coding:

The DS18B20 is useful because many sensors can share one GPIO pin. The first behavior to check is discovery: how many devices are on the 1-Wire bus, and does each one have a unique address?

39.11.1 1-Wire Discovery Flow

  1. Start the 1-Wire bus.
  2. Count connected temperature sensors.
  3. Print each unique sensor address.
  4. Request temperatures.
  5. Reject disconnected-sensor error values.

39.11.2 Optional ESP32 Implementation

#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 4  // GPIO4

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

// Store number of devices
int numberOfDevices;
DeviceAddress tempDeviceAddress;

void setup() {
  Serial.begin(115200);

  sensors.begin();

  // Get number of devices on the bus
  numberOfDevices = sensors.getDeviceCount();
  Serial.print("Found ");
  Serial.print(numberOfDevices);
  Serial.println(" temperature sensors");

  // Print addresses
  for(int i=0; i<numberOfDevices; i++) {
    if(sensors.getAddress(tempDeviceAddress, i)) {
      Serial.print("Sensor ");
      Serial.print(i);
      Serial.print(" Address: ");
      printAddress(tempDeviceAddress);
      Serial.println();
    }
  }
}

void loop() {
  sensors.requestTemperatures();

  for(int i=0; i<numberOfDevices; i++) {
    if(sensors.getAddress(tempDeviceAddress, i)) {
      float tempC = sensors.getTempC(tempDeviceAddress);

      // Check for error values before printing
      if(tempC == DEVICE_DISCONNECTED_C) {
        Serial.print("Sensor ");
        Serial.print(i);
        Serial.println(": ERROR - disconnected");
      } else {
        Serial.print("Sensor ");
        Serial.print(i);
        Serial.print(": ");
        Serial.print(tempC);
        Serial.println("C");
      }
    }
  }

  delay(1000);
}

void printAddress(DeviceAddress deviceAddress) {
  for (uint8_t i = 0; i < 8; i++) {
    if (deviceAddress[i] < 16) Serial.print("0");
    Serial.print(deviceAddress[i], HEX);
  }
}

39.11.3 Putting Numbers to It

The DS18B20 stores temperature as a 16-bit signed integer in 12-bit resolution mode, where the lower 4 bits represent the fractional part and each LSB represents 0.0625C.

Tcelsius=raw_value16T_{\text{celsius}} = \frac{\text{raw\_value}}{16}

Worked example: The DS18B20 returns raw bytes 0xD0 0x01 (little-endian: LSB first, MSB second). Calculate the temperature:

  1. Combine bytes (MSB:LSB): 0x01D0 = 464 decimal
  2. Apply formula: T=46416=29.0°CT = \frac{464}{16} = 29.0°C

Negative temperature example: The sensor returns 0x90 0xFF. Calculate the temperature:

  1. Combine bytes: 0xFF90 = -112 decimal (signed 16-bit, two’s complement)
  2. Apply formula: T=11216=7.0°CT = \frac{-112}{16} = -7.0°C

Power-on default: The sensor stores 0x0550 hex = 1360 decimal at power-on: T=136016=85.0°CT = \frac{1360}{16} = 85.0°C (the infamous “conversion not ready” value)

CRC error marker: The DallasTemperature library returns -127C (DEVICE_DISCONNECTED_C) when data integrity fails or the sensor is unreachable.

39.11.4 Interactive: DS18B20 Raw Value Converter

Use this calculator to convert DS18B20 raw register values to temperature, or enter a temperature to see its raw encoding.

39.11.5 DS18B20 1-Wire Communication

The DS18B20 uses a single data line for bidirectional communication with strict timing requirements:

Start with Initialization — Master pulls line LOW for 480us minimum, then releases. The bus pull-up resistor returns the line HIGH. Then Presence pulse — Sensor responds by pulling the line LOW for 60-240us, signalling “I am here.”. Next ROM command — Master sends a ROM command (e.g., 0x55 Match ROM to address a specific sensor, or 0xCC Skip ROM for single-sensor setups). After that Function command — Master sends 0x44 (Convert T) to start temperature conversion. Continue by Wait for conversion — The internal ADC converts the analog temperature to digital. This takes up to 750 ms at 12-bit resolution, 375 ms at 11-bit, 187.5 ms at 10-bit, or 93.75 ms at 9-bit. Continue by Read scratchpad — Master sends 0xBE (Read Scratchpad) and reads 9 bytes: temperature LSB, temperature MSB, T_H alarm register, T_L alarm register, configuration register, reserved bytes, and CRC. Finally CRC check — Verify all 8 data bytes against the 9th byte (CRC) using the 1-Wire CRC8 algorithm.

Why 85C appears: The DS18B20’s scratchpad power-on reset value is 0x0550 = 85C. If you read before conversion completes, you get this default value.

Why -127C appears: The DallasTemperature library detects a CRC mismatch (bad wiring, electromagnetic interference, or cable too long) and returns DEVICE_DISCONNECTED_C (-127) as an error code.

39.11.6 Learning Points: DS18B20

Key Advantages:

Start with Unique 64-bit address: Each sensor has a factory-programmed ROM code (family code + 48-bit serial + CRC). Then Multi-sensor bus: Connect dozens of sensors on a single GPIO pin with one 4.7kOhm pull-up. Next Digital output: No ADC needed on the microcontroller side; noise-immune over long cables (up to 100m with proper wiring). Finally Parasitic power: Can operate with only 2 wires (data + ground) by drawing power from the data line.

Common Issues and Fixes:

ProblemLikely CauseSolution
No sensors foundMissing pull-up resistorAdd 4.7kOhm between data and VCC
Reads 85CConversion incompleteWait 750 ms after requestTemperatures() at 12-bit
Reads -127CCRC errorCheck wiring, reduce cable length, add decoupling capacitor
Intermittent readingsLong cables or EMIUse lower resolution (9-bit = 93.75 ms) or shielded cable
All sensors same addressCounterfeit sensorsVerify vendor; genuine DS18B20 has unique addresses

Before applying the specification, inspect the real thermocouple probe (k/j/t/e-type) below: its package, terminals, scale, and installation context are part of the engineering evidence.

Real photograph of thermocouple probe (k/j/t/e-type)
This real example (ThermocoupleProbe) shows a physical form of thermocouple probe (k/j/t/e-type). Use the visible package, interfaces, scale, mounting, and surrounding context as evidence; a catalogue label alone does not establish deployment fit. Photo: User:Harke; CC BY-SA 3.0

Carry those visible constraints into the surrounding analysis; the abstract symbol or capability name does not capture mounting, wiring, protection, or service access.

Physics PhoebeCheckpoint: DS18B20 Measurement Path

You now know:

  • A single GPIO pin can host many DS18B20 sensors because each device has a unique 64-bit address.
  • The 12-bit conversion can take 750 ms, and the raw register still divides by 16 when you decode it.
  • 85C means conversion is not ready; -127C points to a CRC or disconnection fault.

39.12 Continue to the Next Part

Carry this evidence into Temperature Sensor Labs: Thermocouple Measurement, which begins with Thermocouple-Based Temperature Sensors.