Using the DS18B20 with ESP32 is an excellent way to measure temperature accurately in your IoT projects. Thanks to its One-Wire protocol and waterproof option, this digital sensor is widely used in both indoor and outdoor applications, including room monitoring, industrial automation, and liquid temperature sensing. In this guide, we’ll walk you through everything you need to know to use the DS18B20 temperature sensor with ESP32, including wiring, coding, and how it compares with other popular sensors like DHT11, DHT22, and LM35.
What is the DS18B20 Temperature Sensor?
The DS18B20 is a digital temperature sensor from Maxim Integrated that communicates using the One-Wire protocol. It is known for its high accuracy, ease of use, and capability to connect multiple sensors to a single GPIO pin, each with a unique 64-bit serial code. This makes it ideal for ESP32-based applications that require multiple temperature readings.

Key Features:
- Temperature Range: -55°C to +125°C.
- Accuracy: ±0.5°C in the range of -10°C to +85°C.
- Resolution: Programmable from 9 to 12 bits.
- Power Supply: 3.0V to 5.5V.
- Unique 64-bit Serial Code: Enables multiple sensors to share the same data line.
- Waterproof Variant: Ideal for liquid temperature measurements.
DS18B20 Pinout
The DS18B20 sensor has three pins:
- GND: Ground connection.
- VCC: Power supply (3.3V or 5V).
- DATA: Digital signal pin used for communication.
Note: Use a 4.7k pull-up resistor between the DATA pin and VCC to ensure reliable communication.

How DS18B20 Works with ESP32
The sensor communicates with ESP32 using the One-Wire protocol. When the ESP32 sends a request, the sensor responds with the temperature reading. Each sensor has a unique address, allowing multiple sensors to share the same data pin.
Working Steps:
- ESP32 sends a request via the data pin.
- DS18B20 measures and converts the temperature.
- The sensor sends back the temperature reading to ESP32.
Wiring the DS18B20 with ESP32
Here’s how to connect the DS18B20 to the ESP32:
DS18B20 Pin | ESP32 Pin |
---|---|
GND | GND |
VCC | 3.3V |
DATA | GPIO 4 (or any digital GPIO) |
Include a 4.7kΩ resistor between the DATA and VCC pins.

🔧 NOTE: Not all ESP32 pins are created equal! Make sure to check out our ESP32 Pinout Guide to select the best GPIO pins for sensor connections. Avoid using pins that are input-only or used during boot, such as GPIOs 0, 2, and 15 in some configurations.
Required Libraries for DS18B20
To interface the DS18B20 with ESP32, you need the following libraries:
- OneWire Library: Facilitates communication with One-Wire devices.
- DallasTemperature Library: Specifically designed for DS18B20 sensors.
Installing the Libraries:
- Open the Arduino IDE and go to Sketch > Include Library > Manage Libraries.
- Search for “OneWire” and “DallasTemperature.”


3. Install both libraries.
Code Example for DS18B20 with ESP32
// DS18B20 with ESP32 - ArduinoYard
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 4 // GPIO 4 on ESP32
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void setup() {
Serial.begin(115200); // Use 115200 for ESP32
sensors.begin();
}
void loop() {
sensors.requestTemperatures();
float temperature = sensors.getTempCByIndex(0);
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
delay(1000);
}
Upload the code, open Serial Monitor at 115200 baud, and you should start seeing temperature readings.
Demonstration
Here’s an example setup and the corresponding serial monitor output:
Hardware Setup:

Serial Monitor Output:

Using Multiple DS18B20 Sensors
You can connect multiple DS18B20 sensors to a single GPIO pin due to their unique 64-bit address.
Steps:
- Connect all DATA lines together to a single ESP32 GPIO pin.
- Use the DallasTemperature library to get each sensor’s address and temperature.
This allows you to measure temperatures from multiple locations using one pin.
Troubleshooting Common Issues
1. Sensor Not Detected:
- Ensure the pull-up resistor (4.7k) is properly connected between the DATA and VCC pins.
- Verify the wiring and check for loose connections.
- Confirm the correct pin number in the code.
2. Incorrect Temperature Readings:
- Check for stable power supply to the sensor.
- Avoid long cable lengths that may cause signal degradation.
- Ensure there are no sources of electrical interference near the sensor.
3. Data Loss in Long Cables:
- Use a lower resistance pull-up resistor (e.g., 2.2k) for improved signal strength.
- Use shielded cables or add decoupling capacitors for signal integrity.
4. Multiple Sensors Not Working:
- Verify each sensor’s unique address using the DallasTemperature library.
- Confirm all sensors share the same DATA pin but are correctly powered.
Comparing DS18B20 Temperature Sensor with Other Sensors
Feature | DS18B20 | DHT11 | DHT22 | LM35 |
---|---|---|---|---|
Output | Digital | Digital | Digital | Analog |
Accuracy | ±0.5°C | ±2°C | ±0.5°C | ±0.5°C |
Temperature Range | -55°C to 125°C | 0°C to 50°C | -40°C to 80°C | -55°C to 150°C |
Power | 3.0V-5.5V | 3.3V-5V | 3.3V-5V | 4V-30V |
Humidity Sensing | No | Yes | Yes | No |
Applications of DS18B20 with ESP32
- Smart Home Temperature Monitoring
- Liquid Temperature Detection (Aquariums, Water Tanks)
- Weather Stations
- Remote Temperature Logging using Wi-Fi
- Industrial IoT Systems
Advanced Topics
- Parasitic Mode: Power the sensor through the DATA line alone. Suitable for scenarios where wiring additional power is difficult.
- Long Cable Connections: Use capacitors and resistors for signal integrity.
Conclusion
The DS18B20 with ESP32 is a powerful and reliable temperature sensing solution for a wide range of projects. Its high precision, simple wiring, and One-Wire protocol support make it ideal for both beginner and advanced makers. Whether you’re building a weather station or an IoT water monitoring system, the DS18B20 is a great sensor to start with.
Integrate it into your ESP32 projects, experiment with multiple sensors, and enjoy precise temperature monitoring with minimal wiring!