add wishlist add wishlist show wishlist add compare add compare show compare preloader
  • phone WhatsApp +91-7023863380
  • Currencies
    • INR
    L/A

DS18B20 Waterproof Temperature Sensor: Complete Wiring Guide for ESP32 & Arduino

The DS18B20 is one of the most popular digital temperature sensors in the electronics and IoT world — and for good reason. It delivers accurate readings (±0.5°C from −10°C to +85°C), communicates over a single data wire using the Dallas OneWire protocol, supports long cable runs of up to 100 metres, and is available in a stainless-steel waterproof probe form factor that can be immersed in liquids or buried in soil.

In this step-by-step guide you will learn how to wire the DS18B20 waterproof probe to both an Arduino Uno and an ESP32, load the correct libraries, run example code, and troubleshoot the most common issues.

What You Will Need

  • DS18B20 waterproof temperature probe (available at Wirelays)
  • Arduino Uno or ESP32 development board
  • 4.7 kΩ resistor (1/4 W) — this is the pull-up resistor for the data line
  • Breadboard and jumper wires
  • USB cable for programming
  • Arduino IDE installed on your computer

DS18B20 Pinout

The waterproof DS18B20 probe has three wires. The colour coding is standard across most manufacturers:

Wire Colour Function Description
Red VCC (Power) Connect to 3.3 V or 5 V supply
Yellow (or White) DATA OneWire data line
Black GND Connect to ground

Important: Always add a 4.7 kΩ pull-up resistor between the DATA line and VCC. This resistor is essential for reliable OneWire communication. Without it, readings will be erratic or the sensor will not be detected at all.

Wiring with Arduino Uno

The Arduino Uno runs at 5 V logic, which is within the DS18B20's operating range (3.0 V – 5.5 V). Here is the wiring:

DS18B20 Wire Arduino Pin
Red (VCC) 5 V
Black (GND) GND
Yellow (DATA) Digital Pin 2

Place the 4.7 kΩ resistor between Digital Pin 2 and the 5 V rail.

Arduino Code Example

Install the OneWire and DallasTemperature libraries from the Arduino Library Manager, then upload this sketch:

#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 2          // Data wire on pin 2

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

void setup() {
  Serial.begin(9600);
  sensors.begin();
  Serial.println("DS18B20 sensor ready");
}

void loop() {
  sensors.requestTemperatures();
  float tempC = sensors.getTempCByIndex(0);

  if (tempC == DEVICE_DISCONNECTED_C) {
    Serial.println("Error: sensor disconnected");
    return;
  }

  Serial.print("Temperature: ");
  Serial.print(tempC);
  Serial.println(" C");
  delay(1000);
}

Wiring with ESP32

The ESP32 uses 3.3 V logic levels. The DS18B20 works reliably at 3.3 V but you must connect VCC to the 3.3 V pin (not 5 V from USB, unless your board has a level shifter). Most GPIO pins work, but GPIO 4 is a common and safe choice.

DS18B20 Wire ESP32 Pin
Red (VCC) 3.3 V
Black (GND) GND
Yellow (DATA) GPIO 4

Place the 4.7 kΩ pull-up resistor between GPIO 4 and the 3.3 V rail.

ESP32 Code Example

The same libraries work on ESP32. In the Arduino IDE, select your ESP32 board, then upload:

#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 4          // Data wire on GPIO 4

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

void setup() {
  Serial.begin(115200);
  sensors.begin();
  Serial.println("DS18B20 on ESP32 ready");
}

void loop() {
  sensors.requestTemperatures();
  float tempC = sensors.getTempCByIndex(0);

  if (tempC == DEVICE_DISCONNECTED_C) {
    Serial.println("Error: sensor disconnected");
    return;
  }

  Serial.print("Temperature: ");
  Serial.print(tempC);
  Serial.println(" C");
  delay(1000);
}

Connecting Multiple Sensors on One Bus

One of the DS18B20's biggest advantages is that you can connect multiple sensors on the same data wire. Each sensor has a unique 64-bit address, and the DallasTemperature library handles address resolution automatically.

To wire multiple probes, simply connect all Red wires together to VCC, all Black wires together to GND, and all Yellow wires together to the same data pin. One 4.7 kΩ pull-up resistor is still sufficient for up to about 10 sensors on a short bus. For longer buses or more sensors, you may need to drop the resistor to 3.3 kΩ or 2.2 kΩ.

Common Issues and Troubleshooting

Sensor returns −127°C

This is the DEVICE_DISCONNECTED value. It almost always means the pull-up resistor is missing or the data wire has a poor connection. Double-check your wiring and ensure the 4.7 kΩ resistor is in place.

CRC Errors or Intermittent Readings

Long cable runs pick up electrical noise. Solutions include: using shielded cable, lowering the pull-up resistor value (try 3.3 kΩ), keeping sensor cables away from mains wiring, and adding a 100 nF decoupling capacitor between VCC and GND at the sensor end.

Parasite Power Mode

The DS18B20 can operate in "parasite power" mode where it draws power from the data line. This is useful for two-wire installations but can cause issues with long cables or multiple sensors. For reliable operation, always use the three-wire (normal power) configuration with VCC connected.

ESP32: Wrong Temperature or No Response

Some ESP32 GPIO pins have internal pull-ups or are used for strapping during boot. Avoid GPIO 0, 2, 5, 12, and 15 for OneWire. Stick to GPIO 4, 13, 14, 16, 17, 18, 19, 21, 22, 23, 25, 26, 27, 32, or 33.

Tips for Outdoor and Industrial Deployment

  • Waterproofing: The stainless-steel probe itself is waterproof, but the cable junction is not. Use heat-shrink tubing or marine-grade silicone sealant where the cable exits the probe housing.
  • Strain Relief: Always secure the cable with cable glands or zip ties so that pulling on the cable does not stress the solder joints.
  • Enclosures: House your microcontroller in an IP65 or IP67 rated enclosure for outdoor use. Route cables through PG cable glands.
  • Cable Length: For runs over 10 metres, use twisted-pair or shielded cable. Over 50 metres, consider a dedicated OneWire bus driver IC.
  • Power Supply: In industrial settings, use a regulated power supply instead of USB power to avoid voltage drops over long cables.

Where to Buy

Wirelays stocks DS18B20 waterproof probes in various cable lengths, along with ESP32 and Arduino development boards, resistor kits, and all the accessories you need to get started. Browse our collections and build your temperature monitoring system today.

Light
Dark
0

20% off on buy 2 shoes

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt.

Sail and harvest moon
₹ 26,000 ₹ 27,500

You added 1

Sail and harvest moon

₹ 14,932 ₹ 16,590

Cart (0)

Add $40 to get a 10% off on total price