The DS18B20 is one of the most popular digital temperature sensors in the maker community, and for good reason. It communicates over a single data wire using the Dallas OneWire protocol, which means you can connect multiple sensors on the same bus using just one GPIO pin. Each DS18B20 has a unique 64-bit address burned in at the factory, so the ESP32 can distinguish between them automatically. It is waterproof (in the probe version), accurate, and inexpensive -- a great first sensor to learn with.
This guide uses the ESP32-WROOM-32 DevKit. Pin labels may differ on your board -- check your board's pinout diagram.
🔗Key Specs
| Parameter | Value |
|---|---|
| Temperature range | $-55$ to $+125\,\text{°C}$ |
| Accuracy | $\pm 0.5\,\text{°C}$ (from $-10$ to $+85\,\text{°C}$) |
| Resolution | 9 to 12 bits (configurable, default 12-bit) |
| Supply voltage | $3.0$ to $5.5\,\text{V}$ |
| Protocol | Dallas OneWire (single data wire) |
| Response time | ~750 ms at 12-bit resolution |
At 12-bit resolution, the sensor reports temperature in increments of $0.0625\,\text{°C}$, which is more than precise enough for most projects.
Alternatives: The DS18B20 measures temperature only. If you also need humidity, consider the DHT22 (budget), BME280 (adds pressure too), or SHT31 (best humidity precision). See our sensor comparison guide for help choosing.
🔗What You'll Need
| Component | Qty | Notes | Buy |
|---|---|---|---|
| ESP32 dev board | 1 | AliExpress | Amazon.de .co.uk .com | |
| DS18B20 temperature sensor | 1 | AliExpress | Amazon.de .co.uk .com | |
| 4.7k ohm resistor | 1 | Pull-up for the OneWire data line | AliExpress | Amazon.de .co.uk .com |
| Breadboard | 1 | AliExpress | Amazon.de .co.uk .com | |
| Jumper wires | 3 | AliExpress | Amazon.de .co.uk .com |
Links marked Amazon/AliExpress are affiliate links. We may earn a small commission at no extra cost to you.
🔗Wiring
Connect the DS18B20 to your ESP32 as follows:
| DS18B20 Pin | ESP32 Pin | Notes |
|---|---|---|
| VCC (red) | 3.3V | Can also use 5V |
| GND (black) | GND | |
| Data (yellow) | GPIO 4 | Any digital GPIO will work |
You also need a 4.7 kΩ pull-up resistor connected between the Data line and VCC. The OneWire protocol uses an open-drain bus, meaning the data line is pulled low by the sensor to communicate. Without the pull-up resistor, the bus cannot return to a high state and communication will fail.
Tip: If you are using a DS18B20 breakout board (as opposed to a bare sensor), check whether a pull-up resistor is already included on the board. Many breakout modules have one built in.
🔗Parasite Power Mode
The DS18B20 supports a special mode called parasite power, where the sensor draws power directly from the data line instead of requiring a separate VCC connection. In this mode, you only connect GND and Data (leaving VCC disconnected or tied to GND). The sensor charges an internal capacitor from the data line when it is high.
Parasite power is useful when running long cable lengths with only two wires, but it can be unreliable at higher temperatures or with multiple sensors. For beginners, the standard three-wire connection described above is recommended.
🔗Required Libraries
You need two libraries, both available through the Arduino Library Manager:
- OneWire by Paul Stoffregen -- handles the low-level OneWire protocol
- DallasTemperature by Miles Burton -- provides a convenient API for DS18B20 sensors
To install them in the Arduino IDE:
- Go to Sketch > Include Library > Manage Libraries...
- Search for OneWire and install it
- Search for DallasTemperature and install it
🔗Code Example
This sketch reads the temperature from a single DS18B20 sensor and prints it to the Serial Monitor every two seconds.
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 4 // GPIO pin connected to the Data line
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void setup() {
Serial.begin(115200);
sensors.begin();
Serial.print("Found ");
Serial.print(sensors.getDeviceCount());
Serial.println(" sensor(s) on the bus.");
}
void loop() {
sensors.requestTemperatures(); // Send command to all sensors on the bus
float tempC = sensors.getTempCByIndex(0); // Read first sensor
if (tempC == DEVICE_DISCONNECTED_C) {
Serial.println("Error: Could not read temperature data.");
return;
}
Serial.print("Temperature: ");
Serial.print(tempC, 2);
Serial.println(" °C");
delay(2000);
}🔗Reading Multiple Sensors
One of the best features of the DS18B20 is the ability to connect multiple sensors on the same data wire. Each sensor has a unique 64-bit address, so the ESP32 can read them individually.
The following sketch discovers all sensors on the bus and reads each one by its address:
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
// Arrays to store sensor addresses
DeviceAddress sensor1, sensor2;
void printAddress(DeviceAddress addr) {
for (uint8_t i = 0; i < 8; i++) {
if (addr[i] < 16) Serial.print("0");
Serial.print(addr[i], HEX);
}
}
void setup() {
Serial.begin(115200);
sensors.begin();
int deviceCount = sensors.getDeviceCount();
Serial.print("Found ");
Serial.print(deviceCount);
Serial.println(" sensor(s).");
// Retrieve addresses by index
if (deviceCount >= 1) {
sensors.getAddress(sensor1, 0);
Serial.print("Sensor 1 address: ");
printAddress(sensor1);
Serial.println();
}
if (deviceCount >= 2) {
sensors.getAddress(sensor2, 1);
Serial.print("Sensor 2 address: ");
printAddress(sensor2);
Serial.println();
}
}
void loop() {
sensors.requestTemperatures();
float temp1 = sensors.getTempC(sensor1);
float temp2 = sensors.getTempC(sensor2);
Serial.print("Sensor 1: ");
Serial.print(temp1, 2);
Serial.print(" °C | Sensor 2: ");
Serial.print(temp2, 2);
Serial.println(" °C");
delay(2000);
}Tip: When using long cable runs (over a few meters), consider lowering the bus speed or adding a stronger pull-up resistor (closer to $2.2\,\text{k}\Omega$) for reliable communication.
🔗How It Works
The DS18B20 uses the Dallas OneWire protocol, a communication method that requires only a single data wire plus ground. The ESP32 sends a command to request a temperature conversion. The sensor then performs the measurement internally and stores the result in its scratchpad memory. The ESP32 reads this value back over the same data line.
At 12-bit resolution, the conversion takes up to $750\,\text{ms}$. The DallasTemperature library handles the timing automatically when you call requestTemperatures().
🔗Troubleshooting
| Problem | Possible Cause | Solution |
|---|---|---|
| Reading shows $-127\,\text{°C}$ | Sensor not detected | Check wiring, verify pull-up resistor is present |
| Reading shows $85\,\text{°C}$ | Sensor returning power-on reset value | Ensure requestTemperatures() is called before reading |
| No sensors found | Wiring issue or wrong GPIO pin | Double-check connections and confirm the correct GPIO in code |
| Inconsistent readings | Loose connection or electrical noise | Use shorter wires, ensure solid connections |
| Multiple sensors but only one reads | Duplicate addresses (rare) or bus contention | Try adding sensors one at a time, check pull-up value |
🔗Next Steps
- Try reading multiple DS18B20 sensors on the same bus and compare indoor vs. outdoor temperatures
- Log temperature data over WiFi to a server or cloud platform
- Combine the DS18B20 with a display (such as an OLED) to build a standalone thermometer
- Set up temperature alerts using threshold values in your code