Smart home

ESP32 temperature humidity sensor (ESPHome)

Build a DHT22 on ESP32-C6 with ESPHome: wiring, YAML, Home Assistant and troubleshooting. Beginner, 1–2 hours. Parts list, GPIO wiring, ESPHome YAML and Ho

Updated · Emmanuel SAUVAGE

  • LevelBeginner
  • Time1–2 h
  • Budgetabout CA$10–25 depending on the seller

Rough estimate, check with your seller: prices vary by shop and period.

Project goal

You will build a small sensor that measures the temperature and humidity of a room and sends both to Home Assistant every 30 seconds, the free software that gathers all your connected devices on one screen. The brain of the build is an ESP32, a small electronic board with built-in Wi-Fi, programmed with ESPHome, a tool that prepares the board’s program from a simple settings file. You need no Zigbee gateway and no subscription: at the end, two new readings, Temperature and Humidity, show up on your dashboard, keep their history and can trigger automations.

Why build it

This sensor lets you keep an eye on a nursery, a cellar, a greenhouse or a room prone to mould. You can also switch on a dehumidifier when the air gets too damp, or compare the real temperature with what your thermostat believes it is. It is also the best first project with an ESP32: there is only one component to connect, with three wires, and every step can be checked within seconds.

Useful terms for this project: ESP32 (small Wi-Fi board) · ESPHome (prepares the board’s program) · GPIO (the board’s pins) · OTA (update without a USB cable)

Prerequisites

  • A working Home Assistant with the ESPHome add-on (see the install tutorial).
  • A 2.4 GHz Wi-Fi network reaching the room.
  • A computer with Chrome or Edge for the first USB flash.

Diagrams

Wiring diagram : Temperature and humidity sensor
Wiring diagram : Temperature and humidity sensor3V3 → DHT22 (module) VCC (+); GPIO10 → DHT22 (module) DATA (OUT); GND → DHT22 (module) GND (−)ESP32-C6 DevKitC-1DHT22 (module)3V3VCC (+)GPIO10DATA (OUT)GNDGND (−)PowerData or signalGround (GND)
Principle diagram : Temperature and humidity sensor
Principle diagram : Temperature and humidity sensorDHT22 → ESP32-C6 + ESPHome → Home AssistantDHT22Measures temperature and humidityOne-wire digital signalGPIO10ESP32-C6 + ESPHomeReads the sensor every 30 sPublishes values on the local network2.4 GHz Wi-Fi + encrypted APIHome AssistantTemperature and Humidity entitiesHistory, dashboard, automations

Parts list

This ESP32 board is the base for every project in the series. ESPHome also works with other ESP32 boards: the board-choice guide helps you compare them. Which ESP32 board to choose?

ESP32-C6-DevKitC-1

Suggested board

ESP32-C6-DevKitC-1

check with the seller
  • Wi-Fi 6, Bluetooth 5, Zigbee and Thread (at hardware level)
  • Supported by ESPHome (ESP-IDF framework)
  • USB-C for flashing and power

Watch-outs

  • 2.4 GHz Wi-Fi only
  • Use a USB cable that carries data

Main components

ItemQuantityRole and precautions
ESP32-C6-DevKitC-11Development board (see the box above). The XIAO ESP32C6 works too if you adapt the pins.
DHT22 (AM2302) module, 3 pins1Manufacturer-stated accuracy: about ±0.5 °C and ±2–5 % humidity. The “module” version includes the pull-up resistor. Main component: search for « DHT22 AM2302 temperature humidity sensor module » at your seller (affiliate link, check price and availability).
BME280 I²C module (alternative)0–1More accurate and adds pressure; the I²C wiring is covered in the full guide.

Small parts

ItemQuantityRole and precautions
USB-C data cable1For flashing and power. Charge-only cables do not carry data.
USB 5 V power supply (CE/UKCA-marked charger)1At least 1 A for the board alone; more if peripherals draw power from it.
Dupont jumper wires (male-female and female-female)1One mixed pack covers every prototype.
Breadboard1To test without soldering; replace with a soldered build if you need it permanent.
Vented plastic or 3D-printed enclosure1Protects the board; leave a cable gland and vents if the sensor measures air.
4.7 to 10 kΩ resistor0–1Only if your DHT22 is a bare 4-pin part (between DATA and 3.3 V).

Amazon links are affiliate links: the site may earn a commission at no extra cost to you.

Step-by-step build

Step 1. Prepare ESPHome and create the device

  1. In Home Assistant, open the ESPHome Device Builder add-on (see the tutorial “Install ESPHome in Home Assistant (Device Builder)”). ESPHome is the software that turns a simple settings file into the program for your ESP32 board, without you having to write any code.
  2. Open “Secrets” (top right): this file keeps your passwords apart, so you can share your configurations without revealing them. Check that wifi_ssid (the name of your Wi-Fi network), wifi_password, api_encryption_key (the key that encrypts the link with Home Assistant), ota_password (for updates without a cable) and ap_password (for the board’s fallback Wi-Fi network) exist, and add any missing line. A valid API key is a long 32-byte string encoded in base64, which you can generate with “openssl rand -base64 32”.
  3. Click “New device”, name it “capteur-temperature”, pick ESP32-C6, skip the flash for now, then replace the file content with the YAML from this page (“Edit”). YAML is a configuration file written in plain text: it describes, line by line, what the board must do.

What you should see: The file opens in the editor with syntax highlighting and no red underline.

Check: The “Validate” action (⋮ menu) answers “Configuration is valid”.

Step 2. Wire the DHT22 on the breadboard

  1. Unplug the USB cable so you work with the power off: a wire placed wrongly on a live board can damage it. Then find the three pins of the DHT22 module, the sensor itself: VCC (or +) receives the power, DATA (or OUT) carries the readings and GND (or −) is the ground, the return path of the current. These names are usually printed on the circuit.
  2. With three Dupont wires (flexible wires whose ends plug in without soldering), connect VCC to the 3V3 pin of the board, DATA to the GPIO10 pin and GND to a GND pin. GPIO stands for “General Purpose Input Output”: these are the metal pins lined up along the edge of the board, and the number after GPIO tells them apart.
  3. Place the sensor a few centimetres from the board, away from the USB port which warms up.

What you should see: The module has no mandatory LED; check visually, wire by wire, before powering up.

Check: The red wire (3.3 V) is not on the 5V pin, and DATA is on GPIO10 as in the diagram.

Step 3. Flash the board and add it to Home Assistant

  1. Connect the board to your computer with the USB-C data cable: “flashing” means writing the program into the board’s memory, and this first write must be done over a cable. The board can already be wired or sitting on the breadboard, but any external peripheral with its own power supply must stay switched off.
  2. In ESPHome click “Install”, then “Plug into this computer” (Chrome or Edge over HTTPS) or “Manual download” and upload the file with ESPHome Web (web.esphome.io). If the port is missing, follow the flashing tutorial.
  3. After the reboot open Settings → Devices & services: the ESPHome device is discovered; click “Configure” and paste the API encryption key.

What you should see: The “Logs” show the Wi-Fi connection and then “Successfully connected to Home Assistant”.

Check: The device appears under Devices & services → ESPHome with its entities; later updates need no cable and go over Wi-Fi (this is OTA, “Over The Air”).

Step 4. Check the readings and correct the offset

  1. Open the “Logs” tab in ESPHome to watch live what the board reports: a “Got Temperature=… Humidity=…” line should appear every 30 seconds, which proves the sensor answers.
  2. Put a reference thermometer next to the sensor for 30 minutes and note the difference.
  3. Enter the difference in the YAML “offset” filter (for example -0.8), then click “Install” > “Wirelessly”.

What you should see: The temperature settles without fast drift; humidity rises if you gently breathe near the sensor.

Check: The gap with the reference thermometer is under 1 °C after correction.

ESPHome code (YAML)

YAML is a configuration file written in plain text: it tells ESPHome what the board must do. Paste this one into ESPHome after creating the device. It targets an ESP32-C6 board; values to personalise are marked with “CHANGE” comments.

esphome:
  name: capteur-temperature
  friendly_name: Temperature sensor

esp32:
  board: esp32-c6-devkitc-1   # XIAO ESP32C6: use board: seeed_xiao_esp32c6
  framework:
    type: esp-idf             # required for ESP32-C6

logger:                       # serial logs, also visible over Wi-Fi

api:
  encryption:
    key: !secret api_encryption_key

ota:
  - platform: esphome
    password: !secret ota_password

wifi:
  ssid: !secret wifi_ssid       # 2.4 GHz network only
  password: !secret wifi_password
  ap:                           # fallback hotspot if the Wi-Fi is unreachable
    ssid: "capteur-temperature fallback"
    password: !secret ap_password

captive_portal:

sensor:
  - platform: dht
    pin: GPIO10                 # CHANGE if you use another free GPIO
    model: DHT22                # AM2302 modules use the same protocol
    update_interval: 30s        # DHT22 needs at least 2 s between reads
    temperature:
      name: "Temperature"
      filters:
        - offset: 0.0           # CHANGE after comparing with a reference thermometer
    humidity:
      name: "Humidity"

  - platform: wifi_signal       # Wi-Fi quality, handy to diagnose a bad location
    name: "WiFi signal"
    update_interval: 120s
    entity_category: diagnostic

binary_sensor:
  - platform: status            # online / offline in Home Assistant
    name: "Status"

YAML comments are in English, as in the ESPHome documentation. This code has not been run on your hardware: use “Validate” before flashing and adjust pins and names.

Home Assistant integration

  • The “Temperature” and “Humidity” entities appear under Settings → Devices & services → ESPHome → your device. Click “Add to dashboard” to create the card.
  • The history graph is available after the first hour; for multi-month curves keep the default recorder (10 days) or set up long-term statistics.
  • Typical automation: if humidity stays above 65 % for 10 minutes, switch on a dehumidifier plug; if the temperature drops below 16 °C, send a notification.

Common problems and troubleshooting

SymptomFix
The value is “Unavailable” or NaNCheck DATA on GPIO10, the 3.3 V supply and the pull-up resistor (4.7–10 kΩ on a bare DHT22). Try another free GPIO and raise update_interval.
The board does not join Wi-FiThe ESP32 only uses 2.4 GHz. Keep a 2.4 GHz SSID on your router, avoid special characters in the password or try the fallback hotspot.
Temperature reads higher than the roomMove the sensor away from the board and USB, or use a longer wire. The offset filter fixes a constant error, not a variable self-heating.
Compilation error for the boardUpdate ESPHome: ESP32-C6 support is recent and needs the ESP-IDF framework (already set in the YAML).

FAQ

DHT22 or BME280: which one?

The DHT22 is simplest (3 wires) for a first build. The BME280 is more accurate and also measures pressure, but uses the I²C bus and needs address 0x76 or 0x77. The full guide covers both.

Can I power the sensor with a battery?

An ESP32 with permanent Wi-Fi empties a battery in days. For a battery build you need ESPHome deep sleep and a low-sleep-current board, or choose a Zigbee sensor.

How many sensors on one board?

As many as you have free GPIOs: each DHT22 takes one. I²C sensors (BME280, BH1750…) share two wires.

Related tutorials

These step-by-step guides explain the gestures common to every project in the series.

Other ESP32 projects

Sources and documentation

Cette page vous a-t-elle été utile ?

Have a question about this guide or need advice? Ask us here

Torn between two models or stuck on a build? Drop us a line and we will get back to you by email.