WiFi Temperature Sensor with ESP32 and ESPHome

Build a wireless temperature and humidity sensor using an ESP32-DevKitC, a DHT22 or BME280 sensor, and ESPHome. This beginner guide walks through installation, wiring, YAML configuration, flashing, and Home Assistant integration in about 30 minutes with no soldering required.

Beginner · 30 minutes · 6 steps

What You Need

DHT22 vs BME280: Which Sensor to Choose

The DHT22 and BME280 are the two most popular temperature and humidity sensors for ESP32 projects, and both work out of the box with ESPHome. The right choice depends on your accuracy needs and budget.

The DHT22 costs around $3-5, uses a single GPIO data pin, and measures temperature to plus or minus 0.5 degrees Celsius with humidity accuracy of plus or minus 2-5% RH. It requires a 4.7k ohm pull-up resistor on the data line (some breakout boards include one). The DHT22 is the simplest option — one wire, one resistor, and you are reading data.

The BME280 costs $5-10, communicates over I2C (two wires: SDA and SCL), and delivers higher resolution at 0.01 degrees Celsius with humidity accuracy of plus or minus 3% RH. It also measures barometric pressure, which is useful for weather stations and altitude sensing. One important caveat: the BME280 self-heats in continuous mode, reading 1-3 degrees Celsius above ambient. ESPHome defaults to forced mode, which eliminates this issue by sleeping between measurements.

For a first project, the DHT22 is easier to wire. For long-term accuracy and additional pressure data, the BME280 is the better investment. Both sensors update every 60 seconds by default in ESPHome, which is more than sufficient for room climate monitoring.

Step-by-Step Instructions

  1. Step 1 Install ESPHome in Home Assistant

    ESPHome runs as an add-on inside Home Assistant. Open your Home Assistant instance, navigate to Settings, then Add-ons, and click Add-on Store. Search for ESPHome Device Builder and click Install. Once installed, click Start, then Open Web UI.

    If you do not have Home Assistant yet, install it on a Raspberry Pi, an old laptop, or a virtual machine. The Home Assistant website has installation guides for every platform. ESPHome requires Home Assistant 2024.1 or later.

    Alternatively, you can run ESPHome standalone via the command line with pip install esphome. The command-line approach works on any computer with Python 3.9 or later and does not require Home Assistant for the initial flash. You will still need Home Assistant to receive sensor data over the native API.

    Tip: The ESPHome web UI runs on port 6052. If you access Home Assistant remotely, make sure this port is accessible or use the built-in Ingress feature.
  2. Step 2 Create the ESPHome YAML Configuration

    In the ESPHome dashboard, click New Device and follow the wizard. Name your device something descriptive like living-room-climate. Select ESP32 as the platform and esp32dev as the board type (this matches the ESP32-DevKitC V4). The wizard generates a base YAML with WiFi credentials, API encryption key, and OTA update password.

    For a DHT22 sensor on GPIO 4, add the following to your YAML configuration:

    sensor: - platform: dht pin: GPIO4 model: DHT22 temperature: name: "Living Room Temperature" humidity: name: "Living Room Humidity" update_interval: 60s

    For a BME280 sensor over I2C (SDA on GPIO 21, SCL on GPIO 22), add:

    i2c: sda: GPIO21 scl: GPIO22 scan: true

    sensor: - platform: bme280_i2c temperature: name: "Living Room Temperature" oversampling: 16x humidity: name: "Living Room Humidity" pressure: name: "Living Room Pressure" update_interval: 60s address: 0x76

    The update_interval of 60 seconds is a good default. For the BME280, ESPHome uses forced mode automatically, which means the sensor sleeps between readings and avoids the self-heating issue that plagues continuous-mode operation.

    Tip: If your BME280 does not respond at address 0x76, try 0x77. Some breakout boards use a different default I2C address depending on the SDO pin state.
  3. Step 3 Wire the Sensor to the ESP32

    No soldering is required — use a breadboard and jumper wires for this project.

    DHT22 wiring (4 pins on the sensor, but only 3 are used): Connect pin 1 (VCC) to the ESP32 3.3V pin. Connect pin 2 (DATA) to GPIO 4 on the ESP32. Connect pin 4 (GND) to the ESP32 GND pin. Pin 3 is not connected. Place a 4.7k ohm resistor between pin 1 (VCC) and pin 2 (DATA) as a pull-up resistor. If your DHT22 is on a breakout board with three pins, the pull-up resistor is already included — just connect VCC, DATA, and GND.

    BME280 wiring (I2C, 4 connections): Connect VIN to ESP32 3.3V. Connect GND to ESP32 GND. Connect SDA to GPIO 21 on the ESP32. Connect SCL to GPIO 22 on the ESP32. Most BME280 breakout boards include pull-up resistors on SDA and SCL, so no external resistors are needed.

    Double-check that you are connecting to the 3.3V pin, not the 5V pin. The ESP32-DevKitC V4 has both, and the BME280 is a 3.3V device. The DHT22 tolerates 3.3-5V but 3.3V is preferred to match the ESP32 logic levels.

    Tip: GPIO 4 is a safe choice for the DHT22 because it has no secondary function during boot. Avoid GPIO 0, 2, and 12 — these affect the ESP32 boot sequence.
  4. Step 4 Flash the Firmware to the ESP32

    Connect the ESP32-DevKitC to your computer via a micro-USB cable. In the ESPHome dashboard, click the three-dot menu on your device and select Install. Choose Plug into this computer for the first flash — this uses the serial connection over USB.

    The ESPHome compiler downloads the required libraries, compiles the firmware, and uploads it to the ESP32. The first compile takes 2-3 minutes. Subsequent compiles are faster because ESPHome caches the build artifacts.

    After flashing, the ESP32 reboots and connects to your WiFi network. The ESPHome dashboard shows the device as Online with a green dot. Click Logs to see real-time output — you should see temperature and humidity readings appearing every 60 seconds.

    If the device shows as Offline, verify your WiFi credentials in the YAML. The ESP32-DevKitC V4 supports 2.4GHz WiFi only — it will not connect to 5GHz networks. Also check that your router is not blocking new devices.

    Tip: After the initial USB flash, all future updates use OTA (over-the-air). You can unplug the USB cable and power the ESP32 from any 5V USB adapter — updates push wirelessly from the ESPHome dashboard.
  5. Step 5 Add the Sensor to Home Assistant

    Home Assistant automatically discovers ESPHome devices on your network. Go to Settings, then Devices and Services. You should see a notification that a new ESPHome device was discovered. Click Configure, enter the API encryption key from your YAML (ESPHome generated this during the wizard), and click Submit.

    The device appears with its sensor entities — temperature, humidity, and pressure if you are using a BME280. Each entity is immediately available for dashboards, automations, and history graphs.

    If the device does not appear in the discovery section within 5 minutes, you can add it manually. Click Add Integration, select ESPHome, and enter the IP address of the ESP32. Find the IP address in your router's DHCP client list or in the ESPHome logs.

    Tip: Give the ESP32 a static IP address in your router's DHCP settings. This prevents the device from getting a new IP after a power cycle, which can cause Home Assistant to temporarily lose the connection.
  6. Step 6 Create a Dashboard and Automation

    In Home Assistant, go to your dashboard and click Edit. Add a new card and select the Entities card type. Add your temperature and humidity entities. The card displays the current readings with a history graph showing the last 24 hours.

    For a more visual display, use the Gauge card for temperature — set the minimum to 0 and the maximum to 40 degrees Celsius. Set color thresholds: green for 18-24 (comfortable range), yellow for 15-18 and 24-28, and red below 15 or above 28.

    Create a simple automation to alert you when temperature or humidity goes outside a comfortable range. Go to Settings, then Automations, and click Create Automation. Set the trigger to the temperature entity going above 28 degrees Celsius. Set the action to send a notification to your phone via the Home Assistant companion app. Add a second automation for humidity above 60% to catch mold-risk conditions.

    For more advanced monitoring, add a Statistics card that shows minimum, maximum, and average temperature over the past week. This data is useful for identifying heating issues, drafty rooms, or HVAC efficiency problems.

    Tip: Place the sensor away from direct sunlight, heating vents, and exterior walls for the most representative room temperature readings. A shelf at 1.5 meters height in the center of the room is ideal.

Frequently Asked Questions

Can I use an ESP32-C3 instead of the ESP32-DevKitC for this project?

Yes. The ESP32-C3-DevKitM-1 is smaller, cheaper, and uses a RISC-V core that draws 5uA in deep sleep. The YAML configuration is nearly identical — just change the board type to esp32-c3-devkitm-1. The C3 has fewer GPIO pins (22 vs 34) but more than enough for a single sensor. It supports 2.4GHz WiFi and Bluetooth 5.0.

How accurate is the DHT22 compared to the BME280?

The DHT22 measures temperature to plus or minus 0.5 degrees Celsius and humidity to plus or minus 2-5% RH. The BME280 offers higher resolution at 0.01 degrees Celsius and plus or minus 3% RH humidity accuracy. In real-world testing, the BME280 produces more consistent, stable readings. The BME280 also measures barometric pressure, which the DHT22 does not.

Does ESPHome work without Home Assistant?

ESPHome can run standalone and expose sensor data via its native API or MQTT. Without Home Assistant, you can use MQTT to send readings to any MQTT broker (Mosquitto, HiveMQ) and consume them with Grafana, Node-RED, or custom scripts. The ESPHome CLI (pip install esphome) compiles and flashes firmware without Home Assistant.

How do I add multiple sensors to one ESP32?

Add additional sensor blocks to your YAML. The ESP32-DevKitC has enough GPIO pins and processing power to run 10 or more sensors simultaneously. For I2C sensors like the BME280, you can connect multiple sensors on the same SDA/SCL bus — each needs a unique I2C address. For DHT22 sensors, each one needs its own GPIO pin.

Can I run this sensor on battery power?

Yes, but battery life depends on your wake interval. With WiFi active and 60-second updates, the ESP32-DevKitC draws roughly 80-160mA, draining a 2500mAh battery in under a day. For battery operation, add the deep_sleep component to wake every 5-30 minutes, which extends battery life to weeks or months. The ESP32-C6 is a better choice for battery sensors — its 7uA deep sleep draws far less than the classic ESP32's 10uA.

What is the maximum WiFi range for the ESP32 sensor?

The ESP32-DevKitC with its onboard PCB antenna achieves 30-50 meters indoors through typical walls and 100+ meters outdoors with line of sight. For sensors in garages, sheds, or far corners of the house, use an ESP32 board with an external antenna connector (U.FL or SMA) and attach a higher-gain antenna for 2-3x range improvement.

How do I update the firmware after the initial flash?

ESPHome supports OTA (over-the-air) updates after the first USB flash. Edit your YAML in the ESPHome dashboard, click Install, and select Wirelessly. The new firmware uploads over WiFi in 30-60 seconds. The ESP32 reboots with the updated configuration automatically. No USB cable needed for any future changes.