DS18B20: pinout and wiring
The DS18B20 is a high-accuracy digital temperature sensor that uses a single data wire (1-Wire). See the pinout, why it needs the 4.7 kΩ resistor, the 9 to 12-bit resolution, the code, and try it in the simulator.
Specifications
- Range
- -55 °C to 125 °C
- Accuracy
- ± 0.5 °C from -10 °C to 85 °C
- Resolution
- 9 to 12 bits (0.5 °C to 0.0625 °C)
- Supply
- 3 V to 5.5 V
- Interface
- 1-Wire (one data wire)
- Conversion
- 94 ms (9 bits) to 750 ms (12 bits)
- Address
- Unique 64-bit factory code
- Versions
- TO-92 and waterproof probe
Pinout
With the flat face of the TO-92 toward you and the pins down, the order is GND, DQ and VDD, left to right. On the waterproof probe, black is GND, red is VDD and yellow (or white) is data.
DS18B20 (3 pins)
| 1 | GND | Ground |
| 2 | DQ | 1-Wire data (needs the 4.7 kΩ pull-up) |
| 3 | VDD | Supply, 3 V to 5.5 V |
DS18B20 simulator
Choose the temperature and resolution to see the value the sensor returns, the step and the conversion time.
The sensor truncates the temperature to the chosen resolution step and sends it in a 16-bit register with 1/16 °C per unit. Fewer bits convert faster, with less detail.
How to wire it
Connect VDD to 5 V (or 3.3 V), GND to ground and DQ to a digital pin, for example D2 on the Arduino Uno. Put a 4.7 kΩ resistor between DQ and VDD: without it the 1-Wire bus never goes HIGH and the reading fails.
Install the OneWire and DallasTemperature libraries. The code requests the conversion with requestTemperatures() and reads the result in °C with getTempCByIndex(0).
Each sensor has a unique 64-bit address, so several DS18B20 can share the same data wire and you read each by index or by address.
The default resolution is 12 bits (0.0625 °C), with a 750 ms conversion. For faster readings, lower it to 9 bits (0.5 °C, 94 ms) with setResolution().
On long cables (over 10 m) reduce the pull-up to 2.2 kΩ or use twisted pair. A reading of 85.0 °C is the factory value and means the conversion did not happen; -127 °C means the sensor is disconnected.
Example code (Arduino)
Sketch that reads a DS18B20 on pin 2 with the OneWire and DallasTemperature libraries.
#include <OneWire.h>
#include <DallasTemperature.h>
#define DATA_PIN 2
OneWire oneWire(DATA_PIN);
DallasTemperature sensors(&oneWire);
void setup() {
Serial.begin(9600);
sensors.begin();
}
void loop() {
sensors.requestTemperatures();
float t = sensors.getTempCByIndex(0);
if (t == DEVICE_DISCONNECTED_C) {
Serial.println("Sensor not found");
} else {
Serial.print(t, 2);
Serial.println(" C");
}
delay(1000);
}
Common mistakes
- Forgetting the 4.7 kΩ resistor between DQ and VDD: the reading returns -127 °C or 85 °C.
- Swapping GND and VDD: the sensor heats up fast and burns out. Check the flat face.
- Expecting an instant reading at 12 bits: conversion takes up to 750 ms, so do not call requestTemperatures() on every loop cycle.
- Using the waterproof probe with the wire colors mixed up: check that black is GND and red is VDD.
- Mixing parasite-powered sensors with a weak pull-up on long cables: prefer powering VDD.
Always check the manufacturer's datasheet for your exact model: pin order can vary between modules.
Frequently asked questions
How do I connect the DS18B20 to Arduino?
GND to ground, VDD to 5 V and DQ to a digital pin, with a 4.7 kΩ resistor between DQ and VDD. Use the OneWire and DallasTemperature libraries.
Why does the DS18B20 read -127 °C?
The sensor was not found: the 4.7 kΩ pull-up is missing, a wire is loose or the pinout is swapped.
Can I connect several DS18B20 to the same pin?
Yes. Each has a 64-bit address, and they all share the same data wire and a single pull-up resistor.
Does the DS18B20 work with ESP32?
Yes. Power it at 3.3 V, connect the 4.7 kΩ pull-up to 3.3 V and use any output GPIO, with the same libraries.