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
- An ESP32 development board.
- A DHT22 sensor or a compatible module.
- A breadboard and jumper wires.
- A USB data cable.
- A stable USB power supply.
- Home Assistant with ESPHome installed.
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:
| Sensor | ESP32 |
|---|---|
| VCC | 3.3 V if the module allows it |
| GND | GND |
| DATA | GPIO4 |
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
- Validate the configuration in ESPHome.
- Compile the firmware and fix the first error that appears in the log.
- Connect the ESP32 over USB and flash the first version.
- Check that it joins WiFi and that Home Assistant discovers the integration.
- 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:
- Temperature does not change abruptly without a reason.
- Humidity does not stay at zero or
NaNall the time. - The reading changes when you ventilate the room.
- The sensor is not stuck to the regulator or the ESP32, which generate heat.
- The read frequency respects the DHT’s limit.
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
- Does not appear over USB: charge-only cable, driver or BOOT button.
- Does not connect to WiFi: credentials, coverage or incompatible network.
NaNvalues: model, pin, power supply, pull-up or wiring.- Reboots: unstable power supply or consumption from a peripheral connected to the ESP32.
- OTA not available: the device must be on the network and the key must match.
External sources
- ESPHome: DHT sensor — platform, model, pin, update, calibration, pull-up.
- Espressif: ESP32 documentation — SDK, pins, memory, WiFi, Bluetooth, security, ADC.
- Espressif: ESP32 datasheet — electrical characteristics, pins, memory, consumption, antenna, temperature.
- DHT22: datasheet — range, accuracy, resolution, response, power, wiring, pull-up.
- DHT11: datasheet — range, accuracy, resolution, response, power, wiring, pull-up.
- ESPHome: DHT sensor — platform, model, pin, update, calibration, pull-up, filtering.
- ESPHome: temperature sensor — platforms, units, filters, calibration, offset, update.
- Home Assistant: ESPHome integration — discovery, entities, services, API encryption, OTA updates.