Connecting a DS18B20 to Arduino is the gold standard for DIY temperature logging, but the 1-Wire protocol is unforgiving if you miss a single pull-up resistor or misread the TO-92 pinout. The direct answer for standard 5V logic: connect the sensor VDD to 5V, GND to GND, and the Data (DQ) pin to Arduino Digital Pin 2, with a 4.7kΩ pull-up resistor bridging DQ and 5V.

This guide skips the generic overviews and goes straight into bench-tested realities. We will cover the exact timing constraints of the Analog Devices (formerly Maxim) DS18B20+, provide a robust C++ sketch that catches phantom reads, and break down the specific error strings that plague clone sensors.

DS18B20 Sensor Specifications & Timing Data

Before writing code, you must understand the conversion timing. The DS18B20 is not an analog sensor; it contains an internal ADC that requires a specific blocking time to complete a temperature conversion. If your code polls the sensor before this window closes, you will read the power-on reset default (85°C).

Table 1: DS18B20 Resolution vs. Conversion Time (Datasheet Values)
Resolution (Bits) Temperature Step Max Conversion Time Typical Use Case
9-bit 0.5°C 93.75 ms Fast ambient room monitoring
10-bit 0.25°C 187.5 ms General weather stations
11-bit 0.125°C 375.0 ms Incubators, brewing
12-bit (Default) 0.0625°C 750.0 ms Lab-grade logging, thermal profiling

Notice the 750ms requirement for 12-bit resolution. Many beginner tutorials use a hardcoded delay(750) in the main loop. In production firmware, blocking the main thread for nearly a second is unacceptable. The code provided later uses non-blocking timing or the library's built-in wait handling to keep your Arduino responsive.

Hardware BOM & Pin Mapping

The code and wiring below target the Arduino Nano V3 (ATmega328P, 5V/16MHz) or the Arduino Uno R3. If you are using a 3.3V board like the ESP32 or Arduino Due, you must use a 3.3V pull-up and ensure your sensor is a genuine Analog Devices part, as many cheap clones fail to oscillate at lower voltages.

Parts List

  • Microcontroller: Arduino Nano V3 (ATmega328P) or Uno R3
  • Sensor: Genuine Analog Devices DS18B20+ (TO-92 package or waterproof stainless steel probe). Avoid unbranded clones; they frequently fail CRC checks.
  • Resistor: 4.7kΩ 1/4W metal film (Color code: Yellow-Violet-Red-Gold). Do not use 10kΩ; it causes signal rise-time failures on cables over 2 meters.
  • Wiring: 22 AWG solid core for breadboard, or 24 AWG stranded for waterproof probe pigtails.

Pin Mapping Table

Table 2: DS18B20 Pinout by Package Type
Function TO-92 Package (Flat side facing you) Waterproof Probe (Wire Colors) Arduino Nano V3 Pin
GND Pin 1 (Left) Black GND
Data (DQ) Pin 2 (Center) Yellow (or White) D2 (with 4.7kΩ pull-up to 5V)
VDD Pin 3 (Right) Red 5V

Wiring Procedure & Production-Ready Code

Follow these steps to wire the sensor. Always double-check the TO-92 pinout; reading it from the curved bottom instead of the flat top will reverse VDD and GND, instantly destroying the internal silicon if left powered.

  1. Insert the Arduino Nano into your breadboard.
  2. Connect the sensor GND (Black/Pin 1) to the Nano GND rail.
  3. Connect the sensor VDD (Red/Pin 3) to the Nano 5V rail.
  4. Connect the sensor DQ (Yellow/Pin 2) to Nano Digital Pin 2.
  5. Insert the 4.7kΩ resistor bridging the DQ wire (Pin 2) and the 5V rail. This is the 1-Wire pull-up. Without it, the open-drain data line will float, yielding garbage data.
Library Requirements: Install OneWire (by Jim Studt/Paul Stoffregen, v2.3.7+) and DallasTemperature (by Miles Burton, v3.11.0+) via the Arduino Library Manager before compiling.

Compilable C++ Sketch with Error Handling

This code targets the Arduino Nano/Uno. It includes explicit error handling for the two most common DS18B20 failure modes: the 85°C power-on reset glitch and the -127°C disconnected bus error.

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

// Pin definitions for Arduino Nano V3 / Uno R3
#define ONE_WIRE_BUS 2
#define SENSOR_RESOLUTION 12 // 9 to 12 bits

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature library
DallasTemperature sensors(&oneWire);

// Variable to hold the device address (populated in setup)
DeviceAddress sensorAddress;
bool sensorFound = false;

void setup() {
  Serial.begin(115200);
  while (!Serial); // Wait for serial port (Nano/Leo only)
  
  Serial.println(F("DS18B20 Initialization..."));
  sensors.begin();
  
  int deviceCount = sensors.getDeviceCount();
  if (deviceCount > 0) {
    sensorFound = true;
    sensors.getAddress(sensorAddress, 0);
    sensors.setResolution(sensorAddress, SENSOR_RESOLUTION);
    Serial.print(F("Sensor found. Resolution set to: "));
    Serial.println(SENSOR_RESOLUTION);
  } else {
    Serial.println(F("ERROR: No devices found on bus. Check 4.7k pull-up and wiring."));
  }
}

void loop() {
  if (!sensorFound) {
    delay(2000);
    return; // Halt loop if no sensor detected at boot
  }

  // Request temperature conversion
  sensors.requestTemperatures();
  
  // Read the temperature (blocking wait handled by library based on resolution)
  float tempC = sensors.getTempC(sensorAddress);
  
  // ERROR HANDLING: Catch phantom reads and bus faults
  if (tempC == -127.0) {
    Serial.println(F("Read error: -127.00°C | Cause: Disconnected sensor or shorted data line."));
  } 
  else if (tempC == 85.0) {
    Serial.println(F("Read error: 85.00°C | Cause: Read executed before 750ms conversion completed."));
  } 
  else {
    Serial.print(F("Temperature: "));
    Serial.print(tempC, 2);
    Serial.println(F(" °C"));
  }

  // Wait 1 second before next poll (adjust based on your application needs)
  delay(1000);
}

Debugging: Resolving "Device Not Found" & Phantom Reads

When a DS18B20 circuit fails, it rarely fails silently. The DallasTemperature library returns specific sentinel values that tell you exactly what went wrong on the physical layer.

The First Three Things to Check When It Fails

  1. Verify the Pull-Up Resistor: Measure the resistance between Digital Pin 2 and the 5V rail with your multimeter. It must read ~4.7kΩ. If it reads infinite (OL), the bus will float high and the Arduino will see no devices.
  2. Check the TO-92 Orientation: Look at the flat side of the sensor. Pin 1 (Left) is GND, Pin 2 (Center) is Data, Pin 3 (Right) is VDD. Reversing VDD and GND will cause the chip to overheat instantly.
  3. Inspect Waterproof Probe Splices: If using a waterproof probe, the internal wire colors are not standardized across all manufacturers. Always use a multimeter in continuity mode to map the Red/Yellow/Black wires to the plug pins before soldering.

Exact Error Strings & Ranked Causes

Table 3: DS18B20 Error Diagnosis Matrix
Exact Serial Output / Value Primary Cause (Most Likely) Secondary Cause Fix
No devices found on bus. Missing 4.7kΩ pull-up resistor. Data wire broken or wrong GPIO pin defined. Install pull-up; verify pin mapping in #define.
Read error: 85.00°C Conversion time exceeded; reading scratchpad before ADC finishes. Parasitic power mode failing to supply enough current. Ensure 12-bit mode waits 750ms. Switch to external VDD power.
Read error: -127.00°C Sensor physically disconnected or data line shorted to GND. Clone chip failing internal CRC check. Check continuity. Replace clone with genuine Analog Devices part.

For a deeper look into the 1-Wire protocol physics and why the open-drain architecture requires that specific pull-up value, refer to the PJRC OneWire Library Documentation, which remains the definitive technical reference for Arduino 1-Wire implementations.

Scaling the Bus: Multi-Drop & Parasitic Power Modes

Once you have a single sensor working, you can extend or simplify the build depending on your physical constraints.

Extending: The Multi-Drop Bus

The 1-Wire protocol allows you to wire up to 20 DS18B20 sensors in parallel on a single Arduino digital pin. Every sensor has a factory-lasered 64-bit ROM embedded in silicon, meaning the Arduino can address them individually.

  • Wiring: Tie all VDD lines together to 5V, all GND lines to GND, and all DQ lines to Pin 2.
  • Hardware tweak: If your bus length exceeds 5 meters, drop the pull-up resistor to 2.2kΩ or 1kΩ to sharpen the signal rise time and overcome cable capacitance.
  • Code tweak: Use the OneWire library's search() function to iterate through and print the 64-bit hex addresses of all connected sensors, then hardcode those addresses into an array in your sketch for reliable indexing.

Simplifying: Parasitic Power Mode

If running three wires to a remote sensor is impossible, the DS18B20 supports "parasitic power." This allows the sensor to draw power directly from the data line, reducing the wiring to just two conductors (Data and GND).

  • Wiring: Connect the sensor VDD pin to GND. Connect DQ to Pin 2 (with the 4.7kΩ pull-up).
  • The Catch: During the temperature conversion phase, the sensor requires up to 1.5mA. A standard 4.7kΩ pull-up cannot supply this. You must use a "strong pull-up"—a MOSFET controlled by a second Arduino pin that drives the data line HIGH during conversion. The DallasTemperature library supports this via the sensors.setPowerMode(true) command, but for most hobbyists, running a third wire for VDD is vastly more reliable than building a MOSFET driver circuit.

For official electrical characteristics and timing diagrams regarding parasitic power limitations, consult the Analog Devices DS18B20 Product Page and download the latest datasheet.

Bench Tip: If you are logging data to an SD card or sending it over MQTT, never trust the raw float output without bounds checking. A sudden spike to 85°C due to a brownout on the 5V rail will corrupt your dataset. Always implement the if (tempC == 85.0 || tempC == -127.0) guardrails shown in the code above before writing to storage.