The DS18B20 temp sensor outputs a strictly digital 1-Wire signal, requiring a 4.7kΩ pull-up resistor on the data line and operating from a 3.0V to 5.5V supply. Unlike analog thermistors, it requires no external ADC or voltage divider. The default 12-bit resolution yields a 16-bit two's complement integer that you multiply by 0.0625 to get exact degrees Celsius. For 90% of embedded projects, the waterproof stainless-steel probe variant (DS18B20-WP) is the default pick.
How the 1-Wire Digital Output Actually Works
The DS18B20 uses a bandgap temperature sensing circuit integrated directly into a silicon die. As temperature changes, the voltage difference between two transistors operating at different current densities shifts predictably. An onboard ADC digitizes this analog delta into a 16-bit two's complement register, completely isolating the microcontroller from analog noise.
Because the output is a timed sequence of high/low logic pulses representing a digital address and a data payload, it is impossible to conflate this with an analog voltage output. The factory trims the internal registers to ±0.5°C accuracy from -10°C to +85°C. No hardware calibration or analog scaling is needed on your PCB; you only perform software scaling via bit-shifting or decimal multiplication.
Wiring and Pinout: External vs. Parasite Power
The DS18B20 supports two power modes: external power (3 pins used) and parasite power (2 pins used, drawing current from the data line). While parasite power saves a wire, it is a frequent source of brownouts during the ADC conversion phase. For reliable bench and jobsite use, always wire it in external power mode.
| Pin (TO-92) | Wire Color (Waterproof) | Function | Connection Rule |
|---|---|---|---|
| 1 (GND) | Black | Ground | Connect to MCU GND. Do not daisy-chain grounds through high-current loads. |
| 2 (DQ) | Yellow / White | Data (1-Wire) | Connect to MCU GPIO. Must have a 4.7kΩ pull-up resistor to VDD. |
| 3 (VDD) | Red | Power (3.0V - 5.5V) | Connect to 3.3V or 5V. Add a 100nF ceramic decoupling capacitor to GND. |
Raw-to-Unit Math: Converting the 16-Bit Register
The sensor returns a 16-bit two's complement integer. The resolution is configurable from 9-bit to 12-bit. The factory default is 12-bit, which provides 0.0625°C per Least Significant Bit (LSB). The math to convert the raw register value to physical units is a single multiplication step.
The Formula:
Temperature (°C) = Raw_Register_Value × 0.0625
Positive Temperature Example:
If the sensor returns the hex value 0x0550, the decimal equivalent is 1360.
1360 × 0.0625 = 85.0°C
Negative Temperature Example:
If the sensor returns 0xFF5E, this is a negative number in two's complement. Inverting the bits and adding 1 yields a decimal value of -162.
-162 × 0.0625 = -10.125°C
When writing C++ for an ESP32 or Arduino, ensure your raw variable is cast as a signed 16-bit integer (int16_t). If you accidentally use an unsigned integer (uint16_t), negative temperatures will wrap around and display as massive positive values (e.g., 65374°C).
Interference, Calibration, and Failure Modes
The 1-Wire protocol is highly susceptible to bus capacitance and electromagnetic interference (EMI). The most common interference sources are long unshielded wire runs, routing the data line parallel to AC mains, and missing decoupling capacitors. High capacitance flattens the rise times of the digital pulses, causing the microcontroller to misread the timing windows and throw Cyclic Redundancy Check (CRC) errors.
To fix signal integrity issues on runs longer than 5 meters, use twisted-pair cable (like Cat5e), keep the 4.7kΩ pull-up resistor physically close to the microcontroller, and drop the bus speed in your software library if supported.
When the DS18B20 powers up, its internal scratchpad defaults to exactly 85.0°C. If your code reads the sensor immediately after boot without first issuing the
Convert T command and waiting the required 750ms for 12-bit conversion, your database will log a false 85°C spike. Always discard the first reading or implement a boot-delay in your firmware.
Regarding calibration: the sensor is laser-trimmed at the factory. You cannot adjust the hardware calibration registers. If your specific application requires tighter tolerances than the ±0.5°C spec, you must apply a static software offset in your code after comparing the sensor against a NIST-traceable reference thermometer in a stable thermal bath.
ESP32 Implementation Code
Below is the robust implementation using the standard Paul Stoffregen OneWire library and the DallasTemperature wrapper. This handles the timing and raw-to-unit math automatically.
#include <OneWire.h>
#include <DallasTemperature.h>
// GPIO where the DS18B20 data pin is connected
const int ONE_WIRE_BUS = 4;
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&sensors);
void setup() {
Serial.begin(115200);
sensors.begin();
// Force 12-bit resolution for maximum precision
sensors.setResolution(12);
}
void loop() {
sensors.requestTemperatures(); // Send Convert T command
// Read the first (or only) sensor on the bus
float tempC = sensors.getTempCByIndex(0);
// Filter out the 85C power-on default and disconnected sensor (-127C)
if (tempC != 85.0 && tempC != -127.0) {
Serial.printf("Temperature: %.2f C\n", tempC);
} else {
Serial.println("Sensor fault or still converting.");
}
delay(2000);
}
Decision Tree: Which DS18B20 Variant to Buy
The Analog Devices DS18B20 datasheet specifies several physical packages. Choosing the wrong package for your environment is the leading cause of sensor failure in DIY builds. Use this decision matrix to select the exact part number.
| Application Scenario | Required Package | Concrete Part Pick |
|---|---|---|
| Indoor ambient air, breadboard prototyping, enclosed PCBs | TO-92 Through-Hole | Maxim DS18B20+ (Approx. $2.50) |
| High-density automated PCB manufacturing | 8-SOIC Surface Mount | DS18B20-S+ (Approx. $3.00) |
| Liquids, soil, outdoor weather, refrigeration, hydroponics | Waterproof Stainless Probe | DS18B20-WP with 1m PVC jacket (Approx. $4.50 - $6.00) |
The Default Recommendation: Unless you are designing a custom surface-mount PCB, buy the waterproof stainless steel probe variant with a pre-attached 1-meter PVC cable. Bare TO-92 packages are highly susceptible to moisture ingress, which causes internal corrosion and permanent short circuits on the data line. The waterproof probe costs roughly $5, seals the silicon die in epoxy and stainless steel, and provides enough cable length to keep the heat-generating microcontroller physically separated from the sensing tip, preventing self-heating measurement errors.






