ESP32 DS18B20 Simulator: Wiring, Code and a Live Circuit
This is a DS18B20 digital temperature sensor wired to an ESP32 DevKit with its 4.7 k pull-up, read live with the OneWire and DallasTemperature libraries: one reading a second, to a hundredth of a degree.
Three legs and one resistor: VDD to 3V3, GND, and DQ to GPIO 4 (D4) with the pull-up from DQ to 3V3. In the editor, drag the sensor’s temperature slider and the next reading follows.
- 3.3 V logic
- Library: DallasTemperature.h
- DQ on GPIO 4 (D4)
- 4.7 k pull-up
Wiring the DS18B20 to the ESP32
Every connection in the circuit above, with the board’s own pin names.
| DS18B20 pin | ESP32 pin | Why |
|---|---|---|
| VDD (right leg) | 3V3 | The DS18B20 runs from 3.0 to 5.5 V. |
| DQ (middle leg) | GPIO 4 (D4) | The one data wire, shared by every sensor on the bus. |
| GND (left leg) | GND | Ground, with the flat face toward you. |
| 4.7 k resistor | DQ to 3V3 | The pull-up the bus needs: the sensor only ever pulls the line low. |
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 on the ESP32
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.
On a real bench
The thirty-pin DevKit straddles a breadboard with one row of holes free on each side, which is where the DS18B20’s jumpers go. Many ESP32 boards are wider and cover both rows; then use two breadboards side by side. Hold BOOT while uploading if the IDE cannot connect.
The code
The sketch the circuit above runs. Open it in the editor, change a line and press Run: it compiles in your browser.
#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);
}
OneWire oneWire(ONE_WIRE_PIN) runs the 1-Wire protocol on the pin, and DallasTemperature sensors(&oneWire) speaks to DS18B20s over it. sensors.begin() searches the bus, and getDeviceCount() says how many sensors answered.
requestTemperatures() starts a conversion on every sensor at once and waits for it: 750 ms at the default 12 bits. getTempCByIndex(0) then reads the first sensor found. DEVICE_DISCONNECTED_C, -127, means no sensor answered.
Each DS18B20 has a unique 64-bit address, so several can share this one pin. Serial.begin(115200) opens the serial monitor. Serial is UART0 on the USB bridge. ESP32 sketches usually run it at 115200 baud, which this one does.
Common mistakes with the DS18B20 on the 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.
Questions and answers
Can I simulate a DS18B20 with an ESP32 DevKit?
Yes. The circuit at the top of this page is an ESP32 DevKit with a DS18B20, running in your browser on a simulated ESP32 (Xtensa LX6). The simulated sensor speaks real 1-Wire, timing and addresses included, so the libraries run unchanged. Press Run it in the editor to change the wiring or the code; it is free and needs no account.
Why do I get -127?
That is DEVICE_DISCONNECTED_C: no sensor answered. The pull-up is missing, DQ is on a different pin from the sketch, or the sensor is wired backwards.
Can I connect several DS18B20s to one pin?
Yes. Wire them all in parallel to GPIO 4 (D4), with one pull-up for the bus, and read each with getTempCByIndex(0), (1) and so on, or by its address.
What is parasite power?
A mode where VDD is tied to GND and the sensor draws power from the data line through the pull-up. It saves a wire but needs a strong pull-up during conversions; three wires, as here, is more reliable.
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.