The DS18B20 sensor outputs a digital 12-bit serial word over a single data wire (the 1-Wire protocol), requiring a 4.7kΩ pull-up resistor to VCC (3.0V–5.5V). To convert the raw hex output to Celsius, you multiply the signed 16-bit integer register value by 0.0625. Unlike thermistors, it requires no analog-to-digital conversion on your microcontroller, but it does demand strict microsecond timing and proper bus termination to avoid floating data lines and CRC errors.

The DS18B20 Sensing Principle and Digital Output

The DS18B20 relies on a bandgap temperature sensing architecture. Inside the silicon, the sensor measures the difference in base-emitter voltage between two transistors operating at different current densities. This voltage delta is directly proportional to absolute temperature. Rather than outputting this as a varying analog voltage, an onboard sigma-delta ADC immediately digitizes the reading into a 16-bit two's complement register.

Because the output is strictly digital (communicating via Maxim's 1-Wire protocol), the sensor does not suffer from the voltage drop or ADC reference drift issues that plague analog sensors like the LM35 or basic NTC thermistors. The microcontroller simply reads a serial data stream containing the temperature, a CRC check byte, and the device's unique 64-bit ROM address.

Wiring, Pinout, and Power Modes

Whether you are using the bare TO-92 transistor package or the popular stainless-steel waterproof probe, the internal pinout remains the same. Note that waterproof probes use color-coded wires instead of physical pin numbers.

Pin (TO-92) Wire Color (Probe) Function Connection Details
Pin 1 Black (GND) Ground Connect to MCU GND and power supply GND.
Pin 2 Yellow or White (DQ) Data In/Out Connect to MCU GPIO. Requires 4.7kΩ pull-up to VCC.
Pin 3 Red (VDD) Power Supply Connect to 3.3V or 5.0V (Supply range: 3.0V to 5.5V).
Callout Tip: Parasitic vs. External Power
The DS18B20 can derive power directly from the data line (parasitic power mode) by tying Pin 3 to GND. While this saves a wire, it is highly discouraged for ESP32 or long wire runs. The 1-Wire master must supply up to 1.5mA during temperature conversions, which often exceeds the continuous current sourcing capabilities of standard microcontroller GPIO pins, leading to brownouts and -127°C read errors. Always use external power (3-wire mode) for reliable bench and field deployments.

Output Signal Math: Raw Bytes to Celsius

The sensor outputs temperature as a 16-bit signed integer in two's complement format. The default resolution is 12-bit, meaning the least significant bit (LSB) represents 0.0625°C.

The Math:
1. Read the 16-bit raw register value.
2. If the value is positive (Bit 15 is 0), multiply the decimal value by 0.0625.
3. If the value is negative (Bit 15 is 1), the number is in two's complement. In C++, casting the raw 16-bit unsigned integer to a signed 16-bit integer (`int16_t`) handles the two's complement math automatically before you multiply.

Worked Example:
A raw hex output of 0x0550 translates to 1360 in decimal.
1360 × 0.0625 = 85.0°C.
Note: 85°C is the factory power-on reset value. If you see 85°C in your serial monitor, your code is reading the sensor before it has completed its first conversion cycle.

Calibration and Interference:
The DS18B20 is factory-calibrated to ±0.5°C accuracy between -10°C and +85°C. No scaling or offset math is required in your code unless you are doing high-precision lab work, in which case you must perform a multi-point offset trim in software against a calibrated reference.

Common interference sources include:
1. Bus Capacitance: Long wires act as capacitors, rounding off the sharp microsecond edges required by the 1-Wire protocol. This causes CRC (Cyclic Redundancy Check) failures.
2. Missing Pull-Up: Without the 4.7kΩ resistor, the open-drain data line floats, resulting in random noise readings.
3. EMI: Running unshielded sensor cables parallel to AC mains wiring induces noise. Always cross AC lines at 90-degree angles and use shielded cable for runs over 10 meters.

Step-by-Step ESP32/Arduino Implementation

To interface the sensor, we use Paul Stoffregen's OneWire library alongside the DallasTemperature wrapper. For the official protocol timing and electrical specs, refer to the Analog Devices DS18B20 Datasheet.

  1. Select the right GPIO: On the ESP32, avoid strapping pins (GPIO 0, 2, 12) which can cause boot failures if pulled high/low during startup. GPIO 4 or 5 are excellent choices.
  2. Wire the hardware: Connect Black to GND, Red to 3.3V, and Yellow to GPIO 4. Solder a 4.7kΩ resistor between the Yellow data wire and the 3.3V Red wire.
  3. Install Libraries: In the Arduino IDE Library Manager, install OneWire and DallasTemperature.
  4. Upload the Code: Use the robust script below, which includes error handling for disconnected sensors.
#include <OneWire.h>
#include <DallasTemperature.h>

// ESP32 GPIO 4 (Avoid strapping pins like 0, 2, 12)
#define ONE_WIRE_BUS 4

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

void setup() {
  Serial.begin(115200);
  sensors.begin();
  // Set resolution to 12-bit (0.0625°C increments)
  sensors.setResolution(12);
  Serial.println("DS18B20 Initialized.");
}

void loop() {
  sensors.requestTemperatures(); 
  float tempC = sensors.getTempCByIndex(0);
  
  // Error handling: -127°C indicates a disconnected sensor or missing pull-up
  if (tempC == -127.0) {
    Serial.println("Error: Sensor disconnected or bus fault.");
  } 
  // Error handling: 85°C indicates reading before conversion finished
  else if (tempC == 85.0) {
    Serial.println("Warning: Power-on reset value read. Add delay.");
  } 
  else {
    Serial.print("Temperature: ");
    Serial.print(tempC);
    Serial.println(" °C");
  }
  
  // Wait 2 seconds (12-bit resolution takes up to 750ms to convert)
  delay(2000);
}

DS18B20 Sensor FAQ

Why is my DS18B20 sensor reading -127°C?

A reading of -127°C (or sometimes -196°F) is the DallasTemperature library's hardcoded error flag. It means the microcontroller sent the read command, but no device responded on the bus. The three most common causes are: 1) You forgot the 4.7kΩ pull-up resistor on the data line, leaving it floating. 2) The sensor is wired in parasitic power mode but the GPIO cannot source enough current during the conversion phase. 3) A physical wire break exists between the probe and the breadboard. Check your continuity with a multimeter and verify the pull-up resistor is actually measuring ~4.7kΩ.

Can I wire multiple DS18B20 sensors to one GPIO pin?

Yes. The 1-Wire protocol is specifically designed as a multi-drop bus. You can wire dozens of DS18B20 sensors in parallel, sharing the same VCC, GND, and Data lines (with a single 4.7kΩ pull-up resistor). Because every DS18B20 has a factory-lasered, unique 64-bit ROM serial number, the microcontroller can address them individually. To do this, you must first run a "ROM scan" sketch to discover and print the unique hex addresses of each sensor, then hardcode those addresses into your production firmware using the getTempC() function instead of getTempCByIndex().

How far can I run the wire for a DS18B20 sensor?

In standard 3-wire external power mode using CAT5e or similar twisted-pair cable, you can reliably push a DS18B20 bus to about 30 to 50 meters. Beyond 20 meters, the capacitance of the cable starts to degrade the sharp voltage edges required by the 1-Wire timing protocol, leading to CRC errors. To extend the range up to 100 meters, drop the pull-up resistor value from 4.7kΩ to 2.2kΩ or even 1kΩ to provide a stronger, faster pull-up current. For runs exceeding 100 meters, abandon standard GPIO bit-banging and use a dedicated 1-Wire master IC like the MAX31850, which handles the strict timing and signal conditioning in hardware.