The DS18B20 Sensing Principle and Digital Output

At its core, the Analog Devices (formerly Maxim) DS18B20 relies on an integrated bandgap temperature sensor. A bandgap circuit generates a voltage proportional to absolute temperature (PTAT) and compares it against a temperature-independent reference. This analog differential is fed directly into an internal, factory-trimmed analog-to-digital converter (ADC) housed within the same silicon die, eliminating the noise and voltage drop issues inherent in remote thermistors or analog RTDs.

Critically, the output of the sensor DS18B20 is strictly digital, communicated via the 1-Wire protocol. It does not output a variable voltage or current. Conflating this with analog sensors like the TMP36 or NTC thermistors is a common beginner mistake that leads to fried GPIO pins or garbage ADC readings. When you read from a DS18B20, you are not measuring a voltage level; you are clocking in a 16-bit two's complement binary register over a single bidirectional data line.

Hardware Interfacing: Pinout, Power Modes, and Pull-Up Math

Whether you are using the bare TO-92 transistor package or the stainless-steel waterproof probe, the internal silicon is identical. The sensor operates on a supply range of 3.0V to 5.5V, making it native-compatible with both 5V Arduinos and 3.3V ESP32/Raspberry Pi Pico boards.

DS18B20 TO-92 Pinout and Wiring Specifications
Pin Name Function Wiring Requirement
1 GND Ground Reference Connect to MCU GND. Must share common ground with pull-up supply.
2 DQ 1-Wire Data Input/Output Requires a 4.7kΩ pull-up resistor to VDD. Connect to MCU GPIO.
3 VDD Power Supply (3.0V - 5.5V) Connect to 3.3V or 5V. (Tie to GND only if using Parasitic Power mode).
Bench Tip: The Pull-Up Resistor is Non-Negotiable
The 1-Wire protocol uses open-drain communication. The MCU and the sensor can only pull the line LOW; neither can drive it HIGH. The 4.7kΩ pull-up resistor is what actually provides the HIGH state. If you omit it, the data line will float, resulting in random noise or a permanent LOW state that crashes your microcontroller's 1-Wire library.

External vs. Parasitic Power Mode

In External Power Mode (recommended), you wire VDD to your 3.3V/5V rail and GND to ground. This provides ample current for the internal ADC during temperature conversions. In Parasitic Power Mode, VDD is tied to GND, and the sensor steals power from the DQ line via an internal capacitor during the conversion phase. While parasitic mode saves a wire, it is highly susceptible to voltage sag on long cable runs and is the root cause of 90% of the "stuck at 85°C" errors seen in hobbyist projects.

From Raw Hex to Celsius: The Output Signal Math

Because the output is digital, no hardware scaling or voltage-divider math is required. The sensor handles the ADC conversion and returns a 16-bit two's complement value. The math to convert this raw register into physical units (Celsius) is straightforward.

The 16-bit register is structured with the sign bit at Bit 15. Bits 11 through 4 represent the integer Celsius value, and Bits 3 through 0 represent the fractional part in increments of 0.0625°C. To get the final temperature in Celsius, you simply divide the raw 16-bit integer by 16.0 (assuming the default 12-bit resolution).

  • +25.0625°C = Raw Hex 0x0191 (Decimal 401) → 401 / 16.0 = 25.0625
  • -10.125°C = Raw Hex 0xFF5E (Decimal -162 in two's complement) → -162 / 16.0 = -10.125
  • +85.0°C = Raw Hex 0x0550 (Decimal 1360) → 1360 / 16.0 = 85.0 (Power-On Reset Default)

Calibration and Software Scaling

The DS18B20 is factory-calibrated to an accuracy of ±0.5°C between -10°C and +85°C. No hardware trimming or user calibration is required or possible. If your specific application demands tighter tolerances, you must apply a software offset in your code after reading the value, comparing it against a known NIST-traceable reference thermometer in a controlled bath.

Complete ESP32/Arduino Implementation

Below is a robust implementation using the industry-standard Paul Stoffregen OneWire and DallasTemperature libraries. It includes critical error handling for disconnected sensors and power-up defaults.

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

// Define the GPIO pin connected to the DQ line
#define ONE_WIRE_BUS 4

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

void setup() {
  Serial.begin(115200);
  sensors.begin();
  // Force 12-bit resolution for maximum precision (0.0625°C steps)
  sensors.setResolution(12);
}

void loop() {
  sensors.requestTemperatures(); 
  float tempC = sensors.getTempCByIndex(0);
  
  // Error handling: -127C means disconnected/shorted
  if (tempC == -127.0) {
    Serial.println("Error: Sensor disconnected or wiring fault.");
  } 
  // Error handling: 85C is the power-on reset value (parasitic power starvation)
  else if (tempC == 85.0) {
    Serial.println("Warning: Read 85C. Check pull-up resistor or switch to external power.");
  } 
  else {
    Serial.print("Temperature: ");
    Serial.print(tempC, 2);
    Serial.println(" °C");
  }
  
  delay(1000);
}

Troubleshooting Long Runs and Common Interference Sources

While the 1-Wire protocol is robust, it was not designed for industrial RS-485 style distances without proper engineering. When deploying the sensor DS18B20 in environments like deep freezers, outdoor weather stations, or large hydroponic setups, three main interference sources will corrupt your data:

  1. Bus Capacitance: Long wires act as a capacitor. If the cable exceeds 10 meters using standard 24 AWG wire, the parasitic capacitance slows down the rising edge of the digital signal. The MCU reads the slow rise as a logic LOW, causing CRC (Cyclic Redundancy Check) failures. Fix: Lower the pull-up resistor to 2.2kΩ or 1.0kΩ to source more current and charge the line faster, or use Cat5e cable and dedicate multiple strands to the DQ line.
  2. Parasitic Power Starvation: During a 12-bit temperature conversion, the sensor draws up to 1.5mA. If running in parasitic mode over long, high-resistance wires, the voltage on the internal storage capacitor droops below the brownout threshold before the conversion finishes. The sensor resets and returns the default 85°C. Fix: Always use External Power Mode (3 wires) for runs over 3 meters.
  3. EMI and Ground Loops: Routing 1-Wire cables parallel to AC mains or inverter PWM lines induces voltage spikes that mimic 1-Wire timing pulses. Fix: Keep sensor cables physically separated from high-voltage AC lines, and ensure the MCU and the sensor share a single, star-grounded reference point.
Safety Note on Waterproof Probes
The stainless steel waterproof DS18B20 probes are sealed with epoxy. Do not submerge the bare wire junction or the exposed crimped end of the probe in water, as capillary action will eventually wick moisture up inside the silicone jacket and short the DQ line to ground, destroying the sensor and potentially back-feeding voltage into your microcontroller.

Frequently Asked Questions

Why is my sensor DS18B20 reading exactly 85°C or -127°C?

A reading of exactly 85°C means you are reading the sensor's Power-On Reset (POR) default register. This happens when the sensor loses power during the temperature conversion phase, almost always caused by using Parasitic Power mode on a wire run that is too long or lacks a strong enough pull-up. A reading of -127°C (or -196.6°F) is the library's hardcoded error value indicating a complete communication failure—usually a disconnected wire, a missing pull-up resistor, or a shorted data line.

Can I wire multiple DS18B20 sensors to a single ESP32 GPIO pin?

Yes. The 1-Wire protocol supports addressing up to 127 devices on a single bus. Every DS18B20 has a unique, factory-lasered 64-bit ROM serial number. You wire all the DQ pins together to the single GPIO, all VDD pins to power, and all GND pins to ground. You still only need one single 4.7kΩ pull-up resistor for the entire bus. In your code, you use the `getAddress()` function to read the unique ROM codes, then query them individually using `getTempC()`.

What is the maximum cable length for a DS18B20 1-Wire bus?

Using standard 24 AWG wire and a passive 4.7kΩ pull-up, the reliable maximum length is about 10 to 15 meters. If you drop the pull-up resistor to 1.0kΩ (ensure your MCU GPIO can sink the resulting 5mA safely) and use high-quality Cat5e twisted-pair cable, you can push passive runs to 30 meters. For distances beyond 50 meters, you must abandon passive pull-ups and use an active 1-Wire master driver IC (like the DS2480B) or a dedicated RS-485 to 1-Wire bridge.

Does the waterproof sensor DS18B20 probe need thermal paste inside the steel tube?

No, you should not open or modify the factory-sealed stainless steel probe. The internal silicon die is already potted in a highly thermally conductive epoxy compound at the factory that bonds it to the inner wall of the steel tube. Attempting to inject your own thermal paste or silicone will compromise the waterproof seal. If you need faster thermal response times than the standard 15-30 second lag of the steel tube, use the bare TO-92 package and pot it yourself in marine-grade epoxy or thermally conductive RTV silicone.