The Verdict: Which DS18B20 Arduino Setup to Choose
For 95% of DIY and prototyping builds, you should use a waterproof stainless steel DS18B20 probe in external power mode with a dedicated 4.7kΩ pull-up resistor on the data line. While the bare TO-92 transistor package is cheaper and the parasitic power mode saves a wire, the waterproof probe survives physical abuse and condensation, and external power mode eliminates the timing-sensitive current-starvation issues that plague parasitic setups on long cable runs.
| Your Scenario | Sensor Format | Power Mode | Concrete Pick |
|---|---|---|---|
| Indoor ambient air monitoring | TO-92 Package | External | Bare DS18B20 TO-92 |
| Liquids, outdoors, or high humidity | Waterproof Probe | External | Stainless Probe (Default Pick) |
| Extremely pin-constrained board | Any | Parasitic | Probe + VDD tied to GND |
| Running >10 meters of cable | Waterproof Probe | External (Active Pull-up) | Probe + MOSFET active pull-up |
Hardware Spec Sheet & Parts List
The DS18B20 communicates over the 1-Wire protocol, meaning it requires precise microsecond-level timing from the microcontroller. Because of this, cable capacitance and pull-up resistor values matter immensely. Do not skip the 4.7kΩ resistor, even if your breakout board claims to have one onboard (many cheap clones omit it or use a 10kΩ that fails on longer wires).
| Component | Exact Variant / Spec | Est. Cost |
|---|---|---|
| Microcontroller | Arduino Uno R3 (ATmega328P, 16MHz) | $24.00 |
| Sensor | DS18B20 Waterproof Probe (Analog Devices MAXIM or verified clone) | $4.50 |
| Pull-up Resistor | 4.7kΩ 1/4W Carbon Film (Yellow-Violet-Red-Gold) | $0.10 |
| Wiring | 22 AWG solid core jumper wires, half-size breadboard | $6.00 |
Wiring the DS18B20 to Arduino Uno R3
The standard waterproof probe exposes three wires: Red (VDD), Black (GND), and Yellow or White (Data/DQ). We are wiring this in External Power Mode, which provides the most stable readings and allows multiple sensors to share a single bus without browning out the Arduino's 5V rail.
| DS18B20 Wire | Arduino Uno R3 Pin | Notes |
|---|---|---|
| Red (VDD) | 5V | Do not use 3.3V; the sensor requires 3.0V-5.5V, but 5V ensures strong logic highs. |
| Black (GND) | GND | Connect to any ground pin. |
| Yellow/White (DQ) | Digital Pin 2 | This is the 1-Wire data bus. |
| 4.7kΩ Resistor | Between 5V and Pin 2 | Mandatory pull-up. Bridges VDD and DQ. |
- Insert the resistor: Place one leg of the 4.7kΩ resistor into the 5V rail and the other leg into the Digital Pin 2 row on your breadboard.
- Connect Power: Plug the Red sensor wire into the 5V rail and the Black wire into the GND rail.
- Connect Data: Plug the Yellow/White sensor wire into the same Digital Pin 2 row as the resistor leg.
- Verify: Use a multimeter in continuity mode to ensure the resistor is actually bridging 5V and Pin 2. Measure the resistance across the resistor legs; it should read between 4.5kΩ and 4.9kΩ.
Compilable Code with Error Handling
This code targets the Arduino Uno R3. It requires the OneWire and DallasTemperature libraries, both installable via the Arduino Library Manager. Unlike basic tutorials, this sketch includes explicit error trapping for the two most common DS18B20 failure states: the 85°C power-on reset fault and the -127°C bus disconnect fault.
#include <OneWire.h>
#include <DallasTemperature.h>
// Pin definition for the 1-Wire bus
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature
DallasTemperature sensors(&oneWire);
void setup() {
Serial.begin(115200);
while (!Serial); // Wait for serial port on native USB boards
sensors.begin();
// Set resolution to 12-bit (highest precision, takes 750ms to convert)
sensors.setResolution(12);
Serial.println("DS18B20 Initialized. Searching for devices...");
int deviceCount = sensors.getDeviceCount();
Serial.print("Found ");
Serial.print(deviceCount);
Serial.println(" devices.");
}
void loop() {
sensors.requestTemperatures(); // Send the command to get temperatures
// Read temperature in Celsius from the first device on the bus
float tempC = sensors.getTempCByIndex(0);
// ERROR HANDLING: Trap known fault states
if (tempC == 85.0) {
Serial.println("ERROR: Read 85.00C. This is the power-on reset scratchpad value.");
Serial.println("Fix: Check wiring, ensure 4.7k pull-up is present, or add a delay before reading.");
}
else if (tempC == -127.0) {
Serial.println("ERROR: Read -127.00C. Device disconnected or bus shorted.");
Serial.println("Fix: Check continuity on DQ wire. Ensure sensor is not wired backwards.");
}
else if (tempC == DEVICE_DISCONNECTED_C) {
Serial.println("ERROR: Device disconnected macro triggered.");
}
else {
// Valid reading
Serial.print("Temperature: ");
Serial.print(tempC);
Serial.println(" °C");
}
// Wait 2 seconds before next reading.
// Note: 12-bit resolution requires up to 750ms conversion time natively.
delay(2000);
}
Debugging: Exact Error Strings and Ranked Causes
When a DS18B20 build fails, it rarely fails silently. The sensor's internal scratchpad registers default to specific hex values on power-up or bus failure, which the DallasTemperature library translates into distinct floating-point numbers. If your serial monitor is throwing errors, follow this decision path.
The First Three Things to Check
Before swapping parts or rewriting code, verify these three physical layer issues:
- The 4.7kΩ Pull-up Resistor: Is it actually there? Many cheap 'plug-and-play' DS18B20 modules claim to have an onboard resistor but ship without it. Measure it with a multimeter.
- Data and VDD Swap: On waterproof probes, color codes vary. Some manufacturers use Yellow for Data, others use White. Some use Blue for GND. Verify your specific probe's pinout with a continuity test against the TO-92 internal die if you have a bare sensor to compare.
- USB Cable Voltage Drop: The 1-Wire protocol relies on microsecond timing. If your Arduino is powered by a cheap, thin USB cable experiencing voltage drop (brownout), the ATmega328P clock timing skews, causing 1-Wire CRC checksum failures.
Ranked Causes by Serial Output
| Exact Serial Output | Root Cause | The Fix |
|---|---|---|
85.00 °C | You are reading the factory default scratchpad value before a temperature conversion has completed. | Add delay(1000) after sensors.begin() in setup, or ensure you aren't reading faster than the 750ms 12-bit conversion time. |
-127.00 °C | The microcontroller cannot see the sensor on the bus. The DQ line is floating, shorted to ground, or the sensor is dead. | Check DQ continuity. Ensure VDD is actually 5V. Try a different 4.7k resistor. |
No devices found | The OneWire library search failed to detect a valid 64-bit ROM address on initialization. | Verify the data wire is on Pin 2. Check for cold solder joints on the probe wires. |
CRC Error | Data was corrupted in transit. Common on cable runs over 5 meters due to capacitance and EMI. | Lower the resolution to 9-bit, use a shielded cable, or implement an active MOSFET pull-up instead of a passive resistor. |
Servo.h, SoftwareSerial, or high-frequency Timer interrupts), they will block the 1-Wire timing, resulting in intermittent CRC Errors or -127°C reads. Move the sensor read to a dedicated polling window or use a hardware I2C/SPI temperature sensor like the BME280 if interrupt conflicts persist.
Extending the Build: Multi-Drop 1-Wire Bus
The greatest strength of the DS18B20 is the 1-Wire multi-drop bus. You can wire up to 20+ sensors to a single Arduino digital pin, provided you manage the power delivery correctly. Every DS18B20 has a unique, factory-lasered 64-bit ROM serial number.
How to Extend (Multi-Sensor Wiring)
To add more sensors, do not run individual wires back to the Arduino (star topology). Instead, daisy-chain them in a linear bus topology:
- Connect all Red wires together to the 5V rail.
- Connect all Black wires together to the GND rail.
- Connect all Yellow/White data wires together to Digital Pin 2.
- Use one single 4.7kΩ pull-up resistor at the Arduino end of the bus. Do not add a resistor for every sensor.
How to Simplify (Addressing the Sensors)
When you have 5 sensors on one pin, how do you know which reading belongs to which physical probe? You must extract their unique ROM addresses.
Run the OneWireSearch example sketch included with the OneWire library. It will print hex addresses like 28-FF641D16043A. Copy these addresses into an array in your code and use sensors.getTempC(deviceAddress) instead of getTempCByIndex(0). This guarantees that Sensor A always maps to your hot water tank, and Sensor B maps to your ambient room air, regardless of the order they power up in.
For authoritative protocol timing details and electrical characteristics, refer to the Analog Devices DS18B20 Datasheet. For deep-dives into the Arduino 1-Wire library implementation and interrupt handling, consult Paul Stoffregen's OneWire Library Documentation.






