Wiring guide

How to wire a DS18B20 to an ESP32

To wire a DS18B20 to an ESP32, connect VDD to 3V3, DQ to GPIO 4 (D4) and GND to GND. The sensor and its 4.7 kilohm pull-up both go to 3.3 V, so the one data line idles at the board’s own level. The sketch below uses the DallasTemperature (DallasTemperature.h) library. It counts the sensors on the one-wire bus and prints the temperature.

  • 3.3 V logic
  • Library: DallasTemperature.h
  • 3 wires, 1 helper parts
ESP32 · DS18B20live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running on the simulator with the sketch below. Press what can be pressed; click anything else to open it in the editor.
Pin to pin

DS18B20 to ESP32 wiring table

Every wire in the circuit above, with the part’s pin names and the board’s own labels. The wire colors are the ones the circuit uses; on a real bench any colors work.

DS18B20 pinESP32 pinWire colorWhy
VDD (right leg)3V3redThe DS18B20 runs from 3.0 to 5.5 V.
DQ (middle leg)GPIO 4 (D4)amberThe one data wire, shared by every sensor on the bus.
GND (left leg)GNDblueGround, with the flat face toward you.
4.7 k resistorDQ to 3V3n/aThe pull-up the bus needs: the sensor only ever pulls the line low.

Helper parts

The extra parts in the circuit above, and where their wires go:

FromToWire color
R1 (4.7 kΩ) pin 1DQamber
R1 (4.7 kΩ) pin 2VDDred
Voltage

Power and logic levels for DS18B20 and ESP32

Logic level

The ESP32’s GPIO pins are 3.3 V logic and are not 5 V tolerant. Nothing wired to a pin may go above 3.3 V.

Supply

The DS18B20 takes 3.3 V from 3V3. That pin is the ESP32’s own regulated 3.3 V output.

This pair

On the ESP32 the sensor and its pull-up go to 3V3, so the data line idles at 3.3 V. With a long cable, drop the pull-up to 2.2 k.

Pins to leave alone

GPIO 34, 35, 36 (VP) and 39 (VN) are input only: they cannot drive a pin or take an internal pull-up. GPIO 0, 2, 5, 12 and 15 are strapping pins, read at power-on, and GPIO 6 to 11 are the flash chip.

The sketch

The working code for the ESP32

Library: DallasTemperature (DallasTemperature.h). This is the sketch the circuit above runs.

DS18B20 on ESP32 GPIO 4 (D4)
#include <OneWire.h>
#include <DallasTemperature.h>

const int ONE_WIRE_PIN = 4;   // DQ, GPIO 4 (D4), with 4.7 k to 3V3

OneWire oneWire(ONE_WIRE_PIN);
DallasTemperature sensors(&oneWire);

void setup() {
  Serial.begin(115200);
  sensors.begin();
  Serial.print("Sensors found: ");
  Serial.println(sensors.getDeviceCount());
}

void loop() {
  sensors.requestTemperatures();          // start a conversion on every sensor
  float celsius = sensors.getTempCByIndex(0);
  if (celsius == DEVICE_DISCONNECTED_C) {
    Serial.println("Sensor not found");
  } else {
    Serial.print("Temperature: ");
    Serial.print(celsius, 2);
    Serial.println(" C");
  }
  delay(1000);
}
Output

What you should see

Open the serial monitor at 115200 baud. The sketch prints how many sensors it found and then the temperature in degrees C once a reading is ready; the sensor starts at 22 degrees.

A reading of -127 C, or no sensor found, means the data line has no pull-up or is on the wrong pin.

The serial monitor prints this, recorded from the circuit above left at its defaults:

Sensors found: 1
Temperature: 22.00 C
Pitfalls

Common mistakes wiring a DS18B20 to an ESP32

No pull-up resistor

Without the 4.7 k from DQ to the supply, the line never goes high, sensors.begin() finds nothing and every reading is -127. In the simulator, delete the resistor and you get exactly that.

The sensor in backwards

With the flat side facing you, the legs are GND, DQ, VDD from left to right. Reversed, a real DS18B20 gets hot fast. Waterproof probes use red for VDD, black for GND and yellow or white for DQ.

Putting the 1-Wire bus on an input-only pin

GPIO 34, 35, 36 and 39 on the ESP32 have no output driver at all. pinMode() to OUTPUT on one of them does nothing and the line never moves, which looks exactly like a dead part. GPIO 4 (D4) is a safe choice; so are GPIO 13, 14, 16 to 19, 23, 25 to 27 and 32 to 33.

Build your own DS18B20 project

Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.