DS18B20: digital temperature probes on one wire
The DS18B20 is a digital thermometer. It sends the temperature as a number on the 1-Wire bus, so the long cable does not change the reading. It measures from −55 °C to +125 °C with ±0.5 °C accuracy from −10 °C to +85 °C. Each sensor has a unique 64-bit ROM address, so you can connect many probes to one GPIO. The waterproof version has the chip in a stainless-steel tube on a 1 m to 3 m cable. Use it in water, soil, aquariums, fermentation vessels and freezers.

Photo: Leonardo Postay, CC BY-SA 4.0
Set the real temperature of each probe on the right. Then request a conversion. All probes convert at the same time, so the wait time depends only on the resolution. Remove the pull-up to see the most frequent wiring fault.
Serial Monitor
How it works
The 1-Wire bus
One data line and ground carry commands and data in both directions. The line is open-drain: the ESP32 and the sensors can only pull it LOW. A 4.7 kΩ pull-up to 3.3 V makes the HIGH level. Without the pull-up, the line stays LOW and the ESP32 finds no devices.
64-bit ROM address
Each DS18B20 has a factory address of 8 bytes: the family code 0x28, a 48-bit serial number and a CRC byte.
The search ROM command finds all addresses on the bus. Then the ESP32 can talk to one sensor at a time.
Resolution and conversion time
The resolution sets the step size and the conversion time. 9 bit gives 0.5 °C steps in 93.75 ms. 12 bit gives 0.0625 °C steps in 750 ms. Each extra bit doubles the time. A small step does not make the sensor more accurate. The accuracy stays ±0.5 °C.
All sensors convert together
requestTemperatures() sends “Skip ROM” and “Convert T” to all sensors at the same time. After one conversion time, the ESP32 reads each
sensor's scratchpad (about 10 ms each). Ten sensors at 12 bit need about 0.85 s in total.
Parasite power
In parasite mode, VDD connects to GND and the sensor takes power from the data line. During a conversion, the sensor needs up to 1.5 mA, so the library drives DATA HIGH (a “strong pull-up”). If the supply is too weak, the result is 85 °C, the power-on value. Use 3 wires when you can.
Error values
−127 °C (DEVICE_DISCONNECTED_C) means no answer or a bad CRC. 85 °C means that the conversion did not run.
Your code must test for these two values and ignore them.
Wiring
Connections
| Probe wire | Connect to | Notes |
|---|---|---|
| Red (VDD) | 3V3 | 3.0–5.5 V. Use 3.3 V, so the data line stays at 3.3 V. |
| Yellow (DATA) | GPIO 4 | Add 4.7 kΩ from DATA to 3V3. Some cables use white or blue. |
| Black (GND) | GND | Common ground. |
Do not use GPIO 34–39 (input only) or the strapping pins 0, 2, 5, 12 and 15. Many low-cost probes contain clone chips. They operate the same way, but some clones do not support parasite power. Refer to GPIO.
Build steps
- Strip the probe wiresSolder each wire to a header pin, or use a screw-terminal adapter board.
- Add the pull-upConnect 4.7 kΩ from DATA to 3V3. Some adapter boards include this resistor.
- Install the librariesIn Library Manager, install “OneWire” by Paul Stoffregen (2.3.8 or newer) and “DallasTemperature” by Miles Burton.
- Read one probeUpload sketch 1. Hold the probe in your hand. The value must increase.
- Find the addressesAdd the other probes. Upload sketch 2. Mark each probe with a label and write down its address.
Code
OneWire 2.3.7 and older does not compile with Arduino-ESP32 core 3.x. Install OneWire 2.3.8 or newer from Library Manager. This release includes the ESP32 fixes. DallasTemperature uses OneWire, so it then works too.
1 · Read one probe
ds18b20_basic.ino
#include <OneWire.h>
#include <DallasTemperature.h>
const int ONE_WIRE_PIN = 4; // 4.7 kΩ pull-up to 3V3
OneWire oneWire(ONE_WIRE_PIN);
DallasTemperature sensors(&oneWire);
void setup() {
Serial.begin(115200);
sensors.begin();
sensors.setResolution(12); // 9..12 bit
Serial.printf("Devices found: %d\n", sensors.getDeviceCount());
}
void loop() {
sensors.requestTemperatures(); // blocks for the conversion time
float t = sensors.getTempCByIndex(0);
if (t == DEVICE_DISCONNECTED_C) {
Serial.println("Error: no answer (examine the wiring and the pull-up)");
} else {
Serial.printf("%.4f C\n", t);
}
delay(1000);
}
2 · Scan the bus and read each probe by address
ds18b20_multi.ino
#include <OneWire.h>
#include <DallasTemperature.h>
OneWire oneWire(4);
DallasTemperature sensors(&oneWire);
DeviceAddress addr[8];
int count = 0;
void printAddress(const DeviceAddress a) {
for (int i = 0; i < 8; i++) Serial.printf("%02X ", a[i]);
}
void setup() {
Serial.begin(115200);
sensors.begin();
count = min(sensors.getDeviceCount(), (uint8_t)8);
Serial.printf("Parasite power: %s\n", sensors.isParasitePowerMode() ? "yes" : "no");
for (int i = 0; i < count; i++) {
if (sensors.getAddress(addr[i], i)) {
sensors.setResolution(addr[i], 11);
Serial.printf("Probe %d: ", i);
printAddress(addr[i]);
Serial.println();
}
}
}
void loop() {
sensors.requestTemperatures(); // all probes convert together
for (int i = 0; i < count; i++) {
float t = sensors.getTempC(addr[i]);
if (t == DEVICE_DISCONNECTED_C || t == 85.0) continue; // error values
Serial.printf("Probe %d: %.3f C\n", i, t);
}
delay(2000);
}
In a final project, copy the addresses into your code as constants. Then each probe keeps its name when you add or remove other probes.
3 · Do not block the loop
At 12 bit, requestTemperatures() waits 750 ms. During that time, the ESP32 cannot do other work, for example update a display or a
web page. Tell the library not to wait. Then read the result after the conversion time.
ds18b20_nonblocking.ino
#include <OneWire.h>
#include <DallasTemperature.h>
OneWire oneWire(4);
DallasTemperature sensors(&oneWire);
const uint32_t CONVERSION_MS = 750; // 12 bit
uint32_t requestedAt = 0;
bool waiting = false;
void setup() {
Serial.begin(115200);
sensors.begin();
sensors.setResolution(12);
sensors.setWaitForConversion(false); // requestTemperatures() returns immediately
}
void loop() {
if (!waiting) {
sensors.requestTemperatures();
requestedAt = millis();
waiting = true;
} else if (millis() - requestedAt >= CONVERSION_MS) {
Serial.printf("%.2f C\n", sensors.getTempCByIndex(0));
waiting = false;
}
// ... other work here: buttons, display, Wi-Fi ...
}
Troubleshooting
−127 °C or “0 devices”
The pull-up is missing, DATA is on the wrong pin, or the wire colors are different. Measure DATA with a multimeter. It must show about 3.3 V when the bus is idle.
The value is always 85 °C
The sensor did not complete a conversion. Usually, this is a parasite-power problem. Connect VDD to 3.3 V (3-wire mode). Also, make sure that your code waits for the full conversion time.
Compile errors in OneWire
An old OneWire version does not compile with ESP32 core 3.x. Update OneWire to 2.3.8 or newer. Remove other copies of OneWire from your
libraries folder.
Resources
- DS18B20 datasheet (Analog Devices / Maxim)Timing, conversion times, parasite power and the ROM format.
- OneWire libraryThe low-level 1-Wire protocol.
- DallasTemperature libraryAll functions: resolution, alarms, addresses and asynchronous reads.
- Guidelines for reliable long-line 1-Wire networksCable, topology and pull-up values for long buses.
- DHT22Temperature and humidity in air.
- BMP280Temperature and air pressure on I²C.
- OLED displayShow the temperatures on a small screen.