Temperature Sensor DS18B20 with ESP32: Complete Guide

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:

  1. GND: Ground connection.
  2. VCC: Power supply (3.3V or 5V).
  3. 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.

DS18B20-Pinout
DS18B20 Pinout

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:

  1. ESP32 sends a request via the data pin.
  2. DS18B20 measures and converts the temperature.
  3. 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 PinESP32 Pin
GNDGND
VCC3.3V
DATAGPIO 4 (or any digital GPIO)

Include a 4.7kΩ resistor between the DATA and VCC pins.

DS18B20-with-ESP32-Wiring-Diagram
DS18B20 with ESP32 Wiring Diagram

🔧 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:

  1. OneWire Library: Facilitates communication with One-Wire devices.
  2. DallasTemperature Library: Specifically designed for DS18B20 sensors.

Installing the Libraries:

  1. Open the Arduino IDE and go to Sketch > Include Library > Manage Libraries.
  2. 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:

DS18B20 Temperature Sensor with ESP32

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:

  1. Connect all DATA lines together to a single ESP32 GPIO pin.
  2. 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

FeatureDS18B20DHT11DHT22LM35
OutputDigitalDigitalDigitalAnalog
Accuracy±0.5°C±2°C±0.5°C±0.5°C
Temperature Range-55°C to 125°C0°C to 50°C-40°C to 80°C-55°C to 150°C
Power3.0V-5.5V3.3V-5V3.3V-5V4V-30V
Humidity SensingNoYesYesNo

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

  1. Parasitic Mode: Power the sensor through the DATA line alone. Suitable for scenarios where wiring additional power is difficult.
  2. 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!


External References


Related Guides

Leave a Comment