If you are building a brewery controller, a server rack monitor, or an outdoor weather station, the temp sensor DS18B20 remains the gold standard for embedded temperature logging. Originally designed by Dallas Semiconductor, later acquired by Maxim Integrated, and now under Analog Devices, this sensor eliminates the analog noise headaches of thermistors by putting the ADC directly inside the probe. But while it is famously robust, its 1-Wire protocol and specific timing requirements trip up many hobbyists and trade students when cables get long or multiple sensors share a bus.
How the DS18B20 Actually Measures Temperature
The DS18B20 relies on a bandgap temperature sensing architecture integrated directly into the silicon die. It measures temperature by comparing a proportional-to-absolute-temperature (PTAT) voltage against a highly stable, temperature-independent bandgap reference voltage. The internal analog-to-digital converter samples this voltage differential and applies factory-programmed calibration coefficients to compensate for inherent silicon non-linearities, outputting a precise digital representation of the thermal state.
Unlike NTC thermistors or RTDs that output a variable resistance (requiring a voltage divider and a microcontroller's ADC to read an analog voltage), the DS18B20 outputs a strictly digital signal. There is no analog voltage or current to measure on the data line. The sensor handles all signal conditioning internally and clocks out a 16-bit signed binary word directly to your microcontroller's GPIO pin via the 1-Wire protocol. Because the math is handled on-chip, no user calibration or complex Steinhart-Hart equation scaling is needed on the microcontroller side.
Wiring, Pinout, and the 4.7kΩ Pull-Up Rule
The DS18B20 is most commonly found in two physical formats: the bare TO-92 transistor package and the waterproof stainless-steel probe. The electrical pinout remains identical, though the wire colors on waterproof probes can occasionally vary by manufacturer (always verify with a multimeter if you lack a datasheet).
The 1-Wire data line (DQ) is an open-drain bus. It can pull the line LOW to ground, but it cannot drive it HIGH. You must install a pull-up resistor between the DQ line and your logic voltage (VDD). The standard value is 4.7kΩ, but see the interference section below for long-cable exceptions.
| Pin Number | Function | TO-92 Pinout | Waterproof Probe Wire | Notes & Supply Range |
|---|---|---|---|---|
| 1 | GND | Left (Flat side facing you) | Black | Connect to MCU Ground |
| 2 | DQ (Data) | Middle | Yellow (or White) | Requires 4.7kΩ pull-up to VDD |
| 3 | VDD | Right | Red | Supply Range: 3.0V to 5.5V DC |
Numbered Wiring Steps (External Power Mode)
- Connect the sensor GND (Black) to your Arduino/ESP32 GND pin.
- Connect the sensor VDD (Red) to your microcontroller's 3.3V or 5V pin (match your logic level).
- Connect the sensor DQ (Yellow) to your chosen digital GPIO pin (e.g., Pin 4 on Arduino, GPIO 4 on ESP32).
- Solder or breadboard a 4.7kΩ resistor between the VDD line and the DQ line, as close to the microcontroller as possible.
- Verify connections with a multimeter in continuity mode before applying power to prevent shorting VDD to GND.
The Raw-to-Celsius Math: Decoding the 16-Bit Register
When you use a library like DallasTemperature, the math is abstracted away. But if you are writing bare-metal 1-Wire drivers or debugging a custom PCB, you need to understand how the sensor formats its data. The DS18B20 stores the temperature in a 16-bit signed two's complement register.
The most significant bit (MSB, Bit 15) indicates the sign: 0 for positive, 1 for negative. The remaining 15 bits represent the magnitude. Because the sensor's default resolution is 12-bit, the lowest 4 bits (Bits 0 through 3) represent fractional degrees in powers of two ($2^{-1}, 2^{-2}, 2^{-3}, 2^{-4}$). This means the raw integer value is exactly 16 times the actual temperature in Celsius.
Suppose your microcontroller reads the raw 16-bit hex value
0x0550 from the sensor's scratchpad.1. Convert hex to decimal:
0x0550 = 1360.2. Check the MSB: The first digit is '0' (binary 0000), so the temperature is positive.
3. Apply the scaling factor: Divide by 16.0.
4.
1360 / 16.0 = 85.0°C.Note: 85°C is the factory power-on default register value, which leads us to the FAQ section below.
For negative temperatures (e.g., 0xFF50), the MSB is 1. You must invert the bits, add 1 to get the positive magnitude, divide by 16, and then apply a negative sign. (e.g., 0xFF50 -> invert to 0x00AF -> add 1 = 0x00B0 (176) -> 176 / 16.0 = 11.0 -> Final result: -11.0°C).
Real-World Interference and Debugging Ghost Readings
Because the DS18B20 uses a digital protocol, it is highly immune to the analog noise that plagues thermistors. However, the 1-Wire protocol is extremely sensitive to timing violations caused by bus capacitance and electromagnetic interference (EMI).
Bus Capacitance on Long Runs: The 1-Wire protocol relies on precise microsecond timing windows (e.g., a read time slot requires the master to pull the line low for 1µs to 15µs, then release it and sample within 15µs). When you run a waterproof probe on a 10-meter cable, the parasitic capacitance of the wire acts like a low-pass filter. Combined with the 4.7kΩ pull-up resistor, this creates an RC delay that slopes the rising edge of the signal. If the voltage doesn't cross the microcontroller's logic HIGH threshold before the 15µs sampling window closes, the bit is read as a 0, corrupting the CRC check.
The Fix: For cable runs between 5 and 15 meters, drop the pull-up resistor to 2.2kΩ or even 1.5kΩ to charge the cable capacitance faster. For runs over 20 meters, use a dedicated 1-Wire bus master IC (like the DS2480B) or an active MOSFET pull-up circuit.
Parasitic Power Sag: If you wire the sensor in parasitic power mode (VDD tied to GND, drawing power from the DQ line's pull-up), the sensor requires a massive surge of current (up to 1.5mA) during the active temperature conversion phase (up to 750ms at 12-bit resolution). A standard 4.7kΩ pull-up cannot supply this current without the bus voltage collapsing below the sensor's brownout threshold. If you must use parasitic power, the microcontroller must drive the DQ line HIGH via a MOSFET to provide a "strong pull-up" during conversion. Otherwise, use the standard 3-wire external power mode.
DS18B20 Sensor FAQ: Solving the Most Common 1-Wire Headaches
Why is my temp sensor DS18B20 reading exactly 85°C on startup?
This is the most common question on embedded forums. When the DS18B20 powers up, its internal scratchpad register is pre-loaded with a default value of 0x0550, which translates to exactly 85.0°C. The sensor does not automatically begin measuring temperature upon receiving power. Your microcontroller must explicitly send the Convert T command and then wait for the conversion to finish (up to 750ms for 12-bit resolution) before reading the scratchpad. If your code reads the sensor before that first conversion completes, you will get the 85°C ghost reading. Add a 1-second delay in your setup() routine or trigger a conversion and wait before taking your first logged measurement.
Can I wire multiple DS18B20 sensors to a single microcontroller pin?
Yes. The 1-Wire protocol is specifically designed for multi-drop bus topologies. Every single DS18B20 chip has a unique, factory-lasered 64-bit ROM serial number embedded in it. You can wire dozens of sensors in parallel (all DQ pins tied together, all VDD pins tied together, all GND pins tied together) sharing a single GPIO pin and a single 4.7kΩ pull-up resistor. To read them individually, your code must first issue a Search ROM command to discover the unique 64-bit addresses on the bus, and then use the Match ROM command to address a specific sensor before triggering a conversion. The Arduino OneWire library handles the ROM search algorithm for you.
Why am I getting -127°C from the DallasTemperature library?
If you are using the popular DallasTemperature wrapper library, a return value of -127°C (or sometimes -126°C) is not a real physical measurement. It is a hardcoded error flag generated by the library. This occurs when the microcontroller fails to detect a presence pulse from the sensor, or when the 8-bit CRC (Cyclic Redundancy Check) calculated by the microcontroller does not match the CRC byte transmitted by the sensor. This almost always points to a physical layer issue: a broken wire inside the waterproof probe, a missing pull-up resistor, or severe EMI from a nearby switching power supply or VFD corrupting the data packets. Check your wiring and verify the pull-up resistor with a multimeter.






