Skip to content
Go back

ESP32 temperature sensor with ESPHome, step by step

Published:  at  12:00 PM

What we are going to build

In this project an ESP32 will read a temperature sensor and send the value to Home Assistant using ESPHome. The goal is not to manufacture a finished product but to practise the full path: power, wiring, firmware, WiFi, entity and automation.

You can use a DHT22 to read temperature and humidity. Its limits and differences with the DHT11 are explained in the sensor comparison.

Materials

Do not connect mains loads in this build. The first goal is to read a low-voltage sensor and validate the software.

Wiring

We will use GPIO4 as an example for the data signal:

SensorESP32
VCC3.3 V if the module allows it
GNDGND
DATAGPIO4

If you use the bare sensor without a module, check whether it needs a pull-up resistor between VCC and DATA. Many modules already include one. The pinout changes between boards, so confirm the silkscreen and do not connect a 5 V signal to an ESP32 GPIO.

Creating the device in ESPHome

In the ESPHome dashboard, create a new configuration and select the exact board. Then adapt this block:

esphome:
  name: sensor-temperatura
  friendly_name: Sensor de temperatura

esp32:
  board: esp32dev

logger:

api:
  encryption:
    key: !secret api_encryption_key

ota:
  - platform: esphome
    password: !secret ota_password

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

sensor:
  - platform: dht
    pin: GPIO4
    model: DHT22
    temperature:
      name: "Temperatura"
    humidity:
      name: "Humedad"
    update_interval: 30s

Keys and network credentials live in secrets.yaml, not in the repository. The board value and the sensor model must match your hardware.

First installation

  1. Validate the configuration in ESPHome.
  2. Compile the firmware and fix the first error that appears in the log.
  3. Connect the ESP32 over USB and flash the first version.
  4. Check that it joins WiFi and that Home Assistant discovers the integration.
  5. Watch temperature and humidity for several hours before fixing the sensor in place.

The first USB flash is easier to recover from than an OTA update. Do not disconnect the board during flashing and keep a data cable around for future incidents.

Checking that it measures correctly

You do not need a laboratory calibration to validate the first build, but you do need to check consistency:

For comparison, place the module next to another reference for a stable period. Do not wet it or expose it to condensation to speed up a test.

Turning the reading into an automation

Once the entity is stable, create a reversible automation, for example an alert if the temperature goes over a threshold. Start with a notification before controlling a heater or a relay. That way you can check the logic without putting a load at risk.

Frequent problems

External sources

Next step

The homeiotguide editorial team

Editorial lead: Néstor Espuch


Suggest changes
Share this article on:

Previous article
Automatic irrigation with ESPHome and a soil sensor
Next article
Raspberry Pi as a Home Assistant server: installation