The DS18B20 and Arduino Uno combination remains the gold standard for DIY digital temperature logging. Unlike analog thermistors that suffer from ADC drift and voltage reference noise, the DS18B20 outputs a calibrated digital signal via Maxim's 1-Wire protocol. The direct answer for a successful build: you must wire the sensor in external power mode (not parasite power) and place a 4.7kΩ pull-up resistor between the 5V VCC line and the Data pin (typically D2). Skip the pull-up, and your serial monitor will flood with -127.00°C errors.
Project Overview & Difficulty Rating
This guide covers the physical wiring, provides production-ready code with built-in error handling for disconnected sensors, and details the exact troubleshooting steps for the most common 1-Wire failure modes. We assume you are using the standard 5V Arduino Uno R3; if you are using a 3.3V board like the ESP32, see the extending section for logic-level notes.
Hardware Spec Sheet & Parts List
The DS18B20 comes in two common physical form factors: the TO-92 transistor package (for breadboards) and the waterproof stainless-steel probe. Both share the same internal silicon and pinout.
| Component | Exact Variant / Spec | Notes & Sourcing |
|---|---|---|
| Microcontroller | Arduino Uno R3 (ATmega328P) | 5V logic, 16MHz clock. Genuine or high-quality clone. |
| Temperature Sensor | DS18B20 (Waterproof Probe) | Maxim/Analog Devices silicon. Beware of cheap clones that fail above 80°C. |
| Pull-up Resistor | 4.7kΩ (1/4W, 5% tolerance) | Mandatory for 1-Wire bus stability. Yellow-Violet-Red-Gold bands. |
| Wiring | 22 AWG solid core / Jumper wires | Keep 1-Wire bus runs under 10 meters to avoid capacitance issues. |
| Prototyping | Half-size breadboard | Standard 400-tie-point board. |
Pin Mapping & Physical Wiring Steps
The waterproof DS18B20 probe typically uses a three-wire color code: Red (VDD / 5V), Black (GND), and Yellow or White (Data). Always verify with a multimeter if you have a bare TO-92 chip, as pinouts can vary by manufacturer.
| DS18B20 Wire (Probe) | DS18B20 Pin (TO-92) | Arduino Uno R3 Pin | Function |
|---|---|---|---|
| Red | Pin 3 (VDD) | 5V | External Power Supply |
| Black | Pin 1 (GND) | GND | Ground Reference |
| Yellow/White | Pin 2 (DQ) | Digital Pin 2 | 1-Wire Data Bus |
| N/A (Resistor) | Between VDD & DQ | 5V to Pin 2 | 4.7kΩ Pull-up |
- De-energize the board: Ensure the Arduino is unplugged from USB before wiring.
- Connect Power and Ground: Plug the red wire into the Arduino 5V rail and the black wire into the GND rail.
- Wire the Data Line: Connect the yellow (or white) data wire to Digital Pin 2 on the Arduino.
- Install the Pull-up Resistor: Insert one leg of the 4.7kΩ resistor into the 5V rail and the other leg into the same row as the data wire (Pin 2). This pulls the bus high when the sensor's open-drain transistor is off.
- Verify Connections: Use a multimeter in continuity mode to ensure the 4.7kΩ resistor bridges 5V and D2, and that there are no shorts between VDD and GND.
Complete Arduino Code with Error Handling
This code targets the Arduino Uno R3. It requires two standard libraries: Paul Stoffregen's OneWire and Miles Burton's DallasTemperature. Install both via the Arduino Library Manager before compiling.
Unlike basic tutorials, this sketch includes robust error handling. It checks for the dreaded -127.00°C disconnect error and the 85.00°C power-on-reset default value, preventing you from logging false data to an SD card or MQTT broker.
#include <OneWire.h>
#include <DallasTemperature.h>
// Pin definition for the 1-Wire bus
#define ONE_WIRE_BUS 2
#define TEMP_READ_INTERVAL 2000 // Read every 2 seconds
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
unsigned long lastReadTime = 0;
int deviceCount = 0;
void setup() {
Serial.begin(115200);
Serial.println(F("DS18B20 & Arduino Uno - Robust Logger"));
sensors.begin();
deviceCount = sensors.getDeviceCount();
if (deviceCount == 0) {
Serial.println(F("ERROR: No DS18B20 sensors found on bus!"));
Serial.println(F("Check 4.7k pull-up resistor and wiring."));
} else {
Serial.print(F("Found "));
Serial.print(deviceCount);
Serial.println(F(" sensor(s)."));
// Set resolution to 12-bit (0.0625°C increments)
sensors.setResolution(12);
}
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - lastReadTime >= TEMP_READ_INTERVAL) {
lastReadTime = currentMillis;
if (deviceCount > 0) {
sensors.requestTemperatures(); // Blocking call for simplicity
float tempC = sensors.getTempCByIndex(0);
// Error Handling: Check for disconnected sensor (-127°C)
if (tempC <= -126.0) {
Serial.println(F("ERROR: Device disconnected or wiring fault (-127.00 C)."));
}
// Error Handling: Check for Power-On Reset value (85°C)
else if (tempC == 85.0) {
Serial.println(F("WARNING: Read 85°C. Sensor just powered on or conversion failed."));
}
// Valid Reading
else {
Serial.print(F("Temperature: "));
Serial.print(tempC, 2);
Serial.println(F(" °C"));
}
}
}
}
Debugging the '-127.00°C' and 'No More Addresses' Errors
When working with the DS18B20 and Arduino, you will inevitably encounter bus errors. Here is the exact decision path for the two most common failure strings.
Error 1: Serial monitor prints -127.00 C
The DallasTemperature library returns -127.0 when the 1-Wire bus fails to return a valid CRC or the sensor does not respond. This is a hardware or timing fault, not a software bug.
- Cause 1 (Most Likely): Missing or incorrect pull-up resistor. The 1-Wire bus is open-drain; without the 4.7kΩ resistor pulling it to 5V, the data line floats and reads garbage.
- Cause 2: Parasite power failure. If you wired VDD to GND to use parasite power, the Arduino's GPIO pin cannot source enough current to power the sensor's internal ADC during the temperature conversion phase.
- Cause 3: Broken internal wire. In cheap waterproof probes, the connection between the stainless steel tube and the internal TO-92 chip often breaks where the epoxy meets the wire.
Error 2: Serial monitor prints No more addresses.
This exact string appears when running a 1-Wire bus scan sketch (like the OneWire Address Scanner example) and the master cannot find any slave ROM codes.
- Cause 1: Data and VDD pins are swapped. You are feeding 5V into the Data pin and trying to read from the VDD pin.
- Cause 2: The sensor is dead or shorted. A wiring mistake that sent >5.5V into the VDD pin will permanently fry the internal silicon.
1. Pull-up Continuity: Measure resistance between 5V and D2 with the power off. It must read ~4.7kΩ.
2. Pinout Orientation: If using a bare TO-92 chip, hold it with the flat face toward you. Left is GND, Middle is Data, Right is VDD.
3. Cable Continuity: Use a multimeter to beep out the red, black, and yellow wires from the probe tip to the breadboard. Waterproof probes have a high out-of-box failure rate.
Extending and Simplifying the Build
How to Extend: The 1-Wire protocol allows you to daisy-chain up to 20-30 DS18B20 sensors on a single Arduino digital pin. Each sensor has a unique 64-bit laser-trimmed ROM code. To read multiple sensors, use the sensors.getTempCByIndex(1) method in the code above, or retrieve their specific ROM addresses using the OneWire Address Scanner sketch and query them individually. If you are extending the bus beyond 5 meters, bus capacitance increases. Drop the pull-up resistor value to 2.2kΩ or even 1kΩ to sharpen the rising edge of the digital signal.
How to Simplify: If you hate breadboarding tiny resistors, buy a DS18B20 breakout board (like the Adafruit or SparkFun variants) which includes the 4.7kΩ pull-up and a decoupling capacitor pre-soldered. Alternatively, if the 1-Wire protocol's strict timing requirements are causing conflicts with other interrupt-heavy libraries in your project, simplify your architecture by switching to an I2C temperature sensor like the TMP117 or BME280, which use a hardware clock line and do not rely on microsecond-precise software bit-banging.
Frequently Asked Questions
Can I wire multiple DS18B20 sensors to one Arduino pin?
Yes. The 1-Wire protocol is designed for multi-drop buses. You wire all red wires to 5V, all black wires to GND, and all yellow data wires to the same Digital Pin 2. You only need a single 4.7kΩ pull-up resistor for the entire bus, regardless of how many sensors you add. The Arduino identifies each sensor by its unique 64-bit factory-programmed serial number.
Why does my DS18B20 read 85°C on startup?
According to the Analog Devices DS18B20 datasheet, the sensor's internal temperature register powers on with a default value of 85°C. If you call getTempC() before the sensor has completed its first analog-to-digital conversion (which takes up to 750ms at 12-bit resolution), it will return this default. The code provided above includes a specific check to flag and ignore this 85°C power-on-reset value.
Do I need a logic level shifter for DS18B20 and ESP32?
Technically, the DS18B20 requires a minimum of 3.0V on the VDD pin to operate in external power mode, and the ESP32 outputs 3.3V logic. While many hobbyists report success running the DS18B20 directly at 3.3V with a 3.3kΩ or 4.7kΩ pull-up to the ESP32's 3.3V rail, the PJRC OneWire library documentation notes that 1-Wire timing margins tighten significantly at lower voltages. For mission-critical or long-distance runs, power the DS18B20 VDD with 5V, but use a bidirectional logic level shifter (like the BSS138 MOSFET circuit) on the data line to protect the ESP32's 3.3V GPIO from 5V signals.
How far can I run the DS18B20 cable from the Arduino?
Using standard 22 AWG wire and a 4.7kΩ pull-up resistor, reliable communication typically maxes out around 10 to 15 meters. Beyond this, the parasitic capacitance of the long cable rounds off the sharp digital edges of the 1-Wire signal, causing CRC errors. To push the distance to 50+ meters, use Cat5e Ethernet cable (twisted pair for Data and GND), lower the pull-up resistor to 1kΩ, and ensure you are using external power mode, as parasite power will fail completely on long runs due to voltage drop.






