My DHT22 sensor kept giving me 0% humidity—until I realized I was rushing the reads.
The first time I wired up a DHT22 sensor to my ESP32, I expected smooth sailing—a quick import dht
, a clean printout, and a job well done. Instead, I was greeted with a humidity reading of 0.0%—repeatedly. At first, I blamed the sensor. Then the code. Then the library. Only after digging through obscure forum posts and combing through the MicroPython source did I realize my mistake: I was asking for data too fast, and the DHT22 just wasn’t having it. It’s a picky little module—quirky, yes, but entirely workable once you learn to speak its language.
In this guide, I’ll show you how to use the DHT22 sensor with ESP32 the right way—with proper timing, stable wiring, and a healthy respect for sensor chill time. If you’re a fellow FOSS tinkerer tired of nonsense readings and ready to collect real-world data from your microcontroller, read on.
Using the dht
Module in MicroPython
The dht
module is not part of the core MicroPython distribution but is included in MicroPython firmware specifically built for ESP8266 and ESP32 boards. This means you can use it without additional installations if you are working with these platforms and standard firmware builds. For most ESP32 and ESP8266 users, the dht
module is pre-installed and ready to use.
If you are using a custom or minimal MicroPython build, you may need to verify whether the dht
module is included. If not, you can integrate the necessary drivers by downloading them from the MicroPython GitHub repository or using an alternative Python library.
For platforms other than ESP8266 and ESP32, the dht
module may not be available by default. In such cases, you will need to obtain a compatible DHT22 sensor driver and add it manually to your project. Always ensure the driver is compatible with your MicroPython version and board to avoid issues.
Materials Needed
- DHT22 Sensor Module
- ESP32 Development Board
- Breadboard and jumper wires
- Micro-USB cable
- Computer with Thonny IDE installed
- 10kΩ and 5.6kΩ resistors (for the voltage divider)
· · ─ ·𖥸· ─ · ·
Wiring the DHT22 Sensor Module with ESP32
The DHT22 sensor has three or four pins: VCC, GND, and DATA. Here are two wiring configurations based on the power supply voltage:
Direct Connection with 3.3V
If the DHT22 is powered by the ESP32’s 3.3V pin:
- VCC: Connect to the 3.3V pin on the ESP32.
- GND: Connect to one of the ESP32’s GND pins.
- DATA: Connect directly to GPIO4 on the ESP32.
- Pull-Up Resistor: Ensure a 10kΩ resistor is connected between the DATA pin and VCC.
· · ─ ·𖥸· ─ · ·
Connection with 5V Using a Voltage Divider
If the DHT22 is powered by 5V:
- VCC: Connect to the 5V pin on the ESP32.
- GND: Connect to one of the ESP32’s GND pins.
- DATA: Connect to GPIO4 on the ESP32 through a voltage divider.
- Pull-Up Resistor: Ensure a 10kΩ resistor is connected between the DATA pin and VCC.
Voltage Divider Example
The DHT22’s data pin operates at the same voltage as its power supply. To step down the 5V signal to 3.3V:
- Connect a 10kΩ resistor between the DATA pin and GND.
- Connect a 5.6kΩ resistor between the DATA pin and GPIO4 on the ESP32.
This configuration reduces the 5V signal to approximately 3.2V, safe for the ESP32.
Reading Data with MicroPython
After wiring, upload the DHT22 driver to the ESP32 and use this script to read data:
from machine import Pin
import dht
import time
# Initialize the DHT22 sensor
dht_sensor = dht.DHT22(Pin(4)) # Replace 4 with your GPIO pin number
while True:
try:
dht_sensor.measure()
temp = dht_sensor.temperature() # Temperature in Celsius
hum = dht_sensor.humidity() # Humidity in percentage
print(f"Temperature: {temp}°C")
print(f"Humidity: {hum}%")
except Exception as e:
print(f"Error: {e}")
time.sleep(2) # Delay between readings
Upload this code to your ESP32 using the Thonny IDE and monitor the readings on the serial console.
· · ─ ·𖥸· ─ · ·
Troubleshooting Tips
- Incorrect Readings: Check wiring, ensure pull-up resistor is present, and verify power supply stability.
- Timeout Errors: Inspect the voltage divider configuration and reduce noise from other components.
- No Data: Ensure the DHT22 driver is correctly installed and the pin assignment matches your wiring.
· · ─ ·𖥸· ─ · ·
Conclusion: Reliable Readings Require Respect
The DHT22 sensor isn’t complicated—but it does have boundaries. We’ve walked through how it works, how to wire it up, and why timing makes or breaks your results. Whether you’re logging climate data or automating plant care, understanding these nuances means you can build smarter, more responsive systems without chasing phantom bugs.
Want more no-fluff, field-tested ESP32 and MicroPython guides?
👉 Subscribe to DevDigest and get honest tutorials, FOSS tools, and practical electronics wisdom—straight to your inbox.
Like what you’re reading?
Help keep DevDigest
free and caffeine-powered
—buy me a coffee on Ko-fi.
Leave a Reply