Preview build — page text is a draft awaiting human review. Code badges come from real compiles. The flasher is real but not yet hardware-tested; the Playground is still a preview.

DS18B20 with ESP32 — waterproof temperature probe wiring and code

Wire a DS18B20 (bare or waterproof probe) to an ESP32 DevKit V1 on GPIO4 with its 4.7 kΩ pull-up and read 12-bit temperatures in Arduino C++.

Arduino C++Verified — core esp32 3.3.12, OneWire 2.3.8, 3 Nov 2026Compile-only

Community draft — report a mistake

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 V1Only the pins used are shown · 3.3 V logicDS18B20 (1-Wire)4.7k → 3V3GNDD43V3GNDDQVDD
Every connection
ESP32 DevKit V1DS18B20 (1-Wire)WireNotes
3V3VDDred
D4DQyellow
3V3DQredVia 4.7 kΩ — The one-wire bus needs this pull-up — waterproof probes too. Some breakout boards already have it.
GNDGNDblack

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-esp32.inoArduino C++Open in PlaygroundOpen in PlaygroundFlash
// 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

Serial Monitor · 115200Output
DS18B20 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.

↑↓ to move · Enter to open