The DS18B20 is the sensor to use when temperature is all you need and it has to survive water, soil or a cold-storage room — the stainless-steel probe version is cheap and sealed.
On an ESP32 the wiring is three wires plus one resistor: a 4.7 kΩ pull-up from DQ to 3V3 is what makes the one-wire bus work. The sketch prints a reading every second at 12-bit resolution (0.0625 °C steps); each conversion takes about 750 ms.
Wiring
| ESP32 DevKit V1 | DS18B20 (1-Wire) | Wire | Notes |
|---|---|---|---|
| 3V3 | VDD | red | |
| D4 | DQ | yellow | |
| 3V3 | DQ | red | Via 4.7 kΩ — The one-wire bus needs this pull-up — waterproof probes too. Some breakout boards already have it. |
| GND | GND | black |
3.0–5.5 V part, 3.3 V board. Power it from your board's logic voltage (3.3 V on ESP32/Pico, 5 V on Uno) so DQ never goes above the GPIO limit. DQ needs a 4.7 kΩ pull-up to that same supply.
Board quirks
- Run the probe from 3V3: the DS18B20 works from 3.0 V, and the data line then never rises above what the ESP32 accepts.
- GPIO4 is free at boot and keeps working with Wi-Fi on. Several probes can share this one pin — each has its own 64-bit address.
- Waterproof probes: red = VDD, black = GND, yellow (sometimes white) = DQ. Check the colour code printed on the bag.
Code
// DS18B20 temperature sensor on an ESP32 DevKit V1.
// DQ → D4 (GPIO4) with a 4.7 kΩ pull-up to 3V3, VDD → 3V3, GND → GND.
#include <OneWire.h>
#include <DallasTemperature.h>
constexpr uint8_t ONE_WIRE_PIN = 4;
OneWire oneWire(ONE_WIRE_PIN);
DallasTemperature sensors(&oneWire);
void setup() {
Serial.begin(115200);
sensors.begin();
Serial.printf("DS18B20 on GPIO%u — %u sensor(s) found\n", ONE_WIRE_PIN, sensors.getDeviceCount());
sensors.setResolution(12); // 0.0625 °C steps, ~750 ms per reading
}
void loop() {
sensors.requestTemperatures();
float c = sensors.getTempCByIndex(0);
if (c == DEVICE_DISCONNECTED_C) {
Serial.println("Sensor not answering — check the pull-up and wiring");
} else {
Serial.printf("Temperature: %.2f °C\n", c);
}
delay(1000);
}// DS18B20 temperature sensor on an ESP32 DevKit V1.
// DQ → D4 (GPIO4) with a 4.7 kΩ pull-up to 3V3, VDD → 3V3, GND → GND.
#include <OneWire.h>
#include <DallasTemperature.h>
constexpr uint8_t ONE_WIRE_PIN = 4;
OneWire oneWire(ONE_WIRE_PIN);
DallasTemperature sensors(&oneWire);
void setup() {
Serial.begin(115200);
sensors.begin();
Serial.printf("DS18B20 on GPIO%u — %u sensor(s) found\n", ONE_WIRE_PIN, sensors.getDeviceCount());
sensors.setResolution(12); // 0.0625 °C steps, ~750 ms per reading
}
void loop() {
sensors.requestTemperatures();
float c = sensors.getTempCByIndex(0);
if (c == DEVICE_DISCONNECTED_C) {
Serial.println("Sensor not answering — check the pull-up and wiring");
} else {
Serial.printf("Temperature: %.2f °C\n", c);
}
delay(1000);
}Library: DallasTemperature (Miles Burton (also install OneWire by Jim Studt / Paul Stoffregen)) — compiled with OneWire 2.3.8.
Expected serial output
DS18B20 on GPIO4 — 1 sensor(s) found
Temperature: 27.94 °C
Temperature: 27.94 °C
Temperature: 28.00 °CDS18B20 on GPIO4 — 1 sensor(s) found Temperature: 27.94 °C Temperature: 27.94 °C Temperature: 28.00 °C
Common errors
Reads −127 °C
The sensor did not answer. Usually the 4.7 kΩ pull-up is missing, or DQ is on the wrong pin. Full fix.
Reads 85 °C at start-up
85 °C is the power-on default — a reading taken before a conversion finished. Call requestTemperatures() before reading, as the sketch does.