Blog Smart home 3 min read

An ESP32 temperature sensor in an afternoon

A DS18B20 probe, an ESP32 board and a short ESPHome file. By the evening, the temperature is on your Home Assistant dashboard.

IOAI blog cover: “An ESP32 temperature sensor in an afternoon” above a home’s daily energy curve.

A temperature sensor is the best first project in smart-home electronics. The parts are cheap, the wiring is simple and you learn the whole path from a sensor to a dashboard.

Here is how I would build one with ESPHome. It turns a short YAML file into firmware for the ESP32, so you don’t need to write any C++.

What you need

#
  • an ESP32 development board (any common one will do)
  • a DS18B20 temperature sensor (the waterproof probe is handy)
  • a 4.7 kΩ resistor, unless your sensor board already has one
  • a USB cable and ESPHome, either as the Home Assistant add-on or on your computer

Wiring

#

The DS18B20 has three wires:

  1. Red to 3.3 V
  2. Black to GND
  3. Data (usually yellow or white) to GPIO4

Then fit the 4.7 kΩ resistor between the data wire and 3.3 V. This pull-up keeps the data line high while nothing is talking. Without it you get no readings, or strange ones.

The configuration

#

In ESPHome, choose New device, give it a name, for example Room sensor, and pick ESP32. ESPHome writes a starter file for you. It already has the board, your Wi-Fi details, an encryption key for Home Assistant and a password for updates over Wi-Fi. Keep all of that and add these lines at the end:

yaml

one_wire:
  - platform: gpio
    pin: GPIO4

sensor:
  - platform: dallas_temp
    name: Temperature
    update_interval: 60s

The one_wire part tells the ESP32 which pin the data wire is on. The dallas_temp sensor reads the DS18B20 on that wire once a minute. With one sensor on the wire, ESPHome finds its address by itself. With several, give each one an address.

Flash it

#

The first upload goes over USB. Plug in the board, choose Install and pick the USB option. After that, every update can go over Wi-Fi.

Open the logs. Once a minute you should see a line with the temperature. Hold the probe in your hand and watch the number climb.

Add it to Home Assistant

#

Home Assistant usually finds the new device on its own. Open Settings → Devices & services, where it waits under Discovered. Add it and, if you are asked, paste the encryption key from the device file (it sits under api:). The reading then appears as sensor.room_sensor_temperature.

Now it’s an entity like any other. Put it on a dashboard, draw its history or use it in an automation.

A few tips

#
  • Keep the probe away from the board. The ESP32 gets warm and will push the reading up.
  • Once a minute is plenty for a room. Faster updates only fill the history.
  • A DS18B20 is accurate to about ±0.5 °C. If you want humidity and air pressure too, a BME280 is a good next step.

That is the whole path: a sensor, a small computer, a network and a dashboard. Once it works, every other sensor is a variation on the same idea.