ESP32 + DS18B20 with Home Assistant (ESPHome & MQTT) – Complete 2026 Guide

The DS18B20 is one of the most robust and accurate digital temperature sensors for:

  • Boilers & hot water tanks
  • Underfloor heating
  • Outdoor monitoring
  • Aquariums
  • HVAC pipes
  • Radiators
  • Freezers

It uses a 1-Wire protocol, allowing multiple sensors on the same wire, making it perfect for home automation.

This guide includes two methods:

  1. ESPHome (recommended — auto-discovery)
  2. MQTT (manual)

1. Parts Required

  • ESP32 DevKit
  • DS18B20 waterproof probe
  • 4.7k resistor (required)
  • Jumper wires
  • USB cable

2. DS18B20 Wiring Diagram

Required connections:

ESP32 → DS18B20
3.3V → VDD
GND  → GND
GPIO4 → DATA
4.7k resistor between DATA and 3.3V

METHOD 1 — ESPHome Integration

This is the easiest method for most users.


3. ESPHome YAML for DS18B20 (Working Code)

esphome:
  name: esp32-ds18b20
  platform: ESP32
  board: esp32dev

wifi:
  ssid: "YOUR_WIFI"
  password: "YOUR_PASSWORD"

logger:
api:
ota:

# 1-Wire bus
dallas:
  - pin: 4

sensor:
  - platform: dallas
    address: 0x3c0000031aa7c828
    name: "Boiler Temperature"
    update_interval: 5s

Notes:

  • If you don’t know the sensor’s address, omit address:
  • ESPHome will auto-detect the sensor and show its ID in logs
  • Then you can add the address for stability

METHOD 2 — MQTT Integration (Matches Your Actual Setup)

If you use MQTT sensors inside Home Assistant’s mqtt: section, here is the correct YAML for your configuration style.


4. Home Assistant configuration.yaml (Correct MQTT Code)

Add under:

mqtt:
  sensor:

Correct DS18B20 MQTT YAML Block

mqtt:
  sensor:
    - name: "Boiler Temperature"
      state_topic: "home/boiler/ds18b20"
      value_template: "{{ value_json.temperature }}"
      unit_of_measurement: "°C"

5. ESP32 Arduino Code for DS18B20 (MQTT Publisher)

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

#define ONE_WIRE_BUS 4
#define WIFI_SSID "YOUR_WIFI"
#define WIFI_PASS "YOUR_PASSWORD"
#define MQTT_SERVER "192.168.0.10"

WiFiClient espClient;
PubSubClient client(espClient);

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

void setup() {
  Serial.begin(115200);
  
  sensors.begin();

  WiFi.begin(WIFI_SSID, WIFI_PASS);
  while (WiFi.status() != WL_CONNECTED) delay(500);

  client.setServer(MQTT_SERVER, 1883);
}

void loop() {
  if (!client.connected()) {
    while (!client.connected()) client.connect("ESP32_DS18B20");
  }

  sensors.requestTemperatures();
  float tempC = sensors.getTempCByIndex(0);

  String payload = "{\"temperature\":" + String(tempC) + "}";

  client.publish("home/boiler/ds18b20", payload.c_str());

  client.loop();
  delay(5000);
}

6. Adding Multiple DS18B20 Sensors

If you add more sensors, all can share the same DATA wire.

MQTT example:

mqtt:
  sensor:
    - name: "Tank Top Temperature"
      state_topic: "home/boiler/ds18b20/top"
      value_template: "{{ value_json.temperature }}"
      unit_of_measurement: "°C"

    - name: "Tank Bottom Temperature"
      state_topic: "home/boiler/ds18b20/bottom"
      value_template: "{{ value_json.temperature }}"
      unit_of_measurement: "°C"

7. Home Assistant Dashboard Example

type: vertical-stack
cards:
  - type: sensor
    entity: sensor.boiler_temperature
  - type: history-graph
    hours_to_show: 24
    entities:
      - sensor.boiler_temperature

8. Example Automation

Automatically switch boiler off when water reaches 55°C:

automation:
  - alias: "Boiler Auto Shutoff"
    trigger:
      - platform: numeric_state
        entity_id: sensor.boiler_temperature
        above: 55
    action:
      - service: switch.turn_off
        target:
          entity_id: switch.boiler_relay

9. Troubleshooting

Sensor reads -127°C

  • Missing 4.7k pull-up resistor
  • Broken cable
  • Wrong data pin

Sensor shows 85°C

  • DS18B20 not initialized
  • First reading after boot (normal)

Sensor drops randomly

  • Long cable → use shielded cable
  • Add 100nF capacitor between VDD/GND

10. Why DS18B20 Is Perfect for Boilers & Heating

FeatureBenefit
Waterproof probeCan be attached to pipes/tanks
Long cables OKUp to 20–30 meters with shielded cable
Very stableDoes not drift over time
Multiple sensors per wireBoiler top/bottom / pipes

esp32 ds18b20
home assistant ds18b20
mqtt ds18b20
esp32 boiler sensor
dallas temperature esp32
esp32 water temperature sensor
ds18b20 wiring esp32
ds18b20 esphome
Share your love

Leave a Reply

Your email address will not be published. Required fields are marked *