Having trouble connecting the DHT22/AM2302 to the D1 Mini / ESP8266? Here’s a possible solution.

The Problem

Multitudes of tutorials show you how to connect the DHT22 / AM2302 Temperature and Humidity sensor to the Wemos/LOLIN D1 Mini (or other ESP8266 boards). However, most of them tell you to connect the data pin of this sensor to the D4 pin on the D1 Mini, which is GPIO2 on the ESP8266. After you flash, you will get errors.

If you are flashing for ESPHome / Home Assistant, you may get an error like this:

Requesting data from DHT failed!
Invalid readings! Please check your wiring (pull-up resistor, pin number).
Sending state nan °C with 1 decimals of accuracy
'Channel 10004-1 Humidity': Sending state nan % with 0 decimals of accuracy

For those who are frustrated but patient, you may notice that power cycling the board after flashing will make it work again. Then, we will start posting on the forums asking why the DHT22 only works sometimes and is inconsistent.

Among other issues (such as unstable voltage, or lack of a pull-up resistor), this one seems to be most pertinent to me and doesn’t seem to be mentioned much online.

The Cause

As per the schematic for the D1 Mini, the GPIO2 pin is pulled up with a 10k resistor. The GPIO2 pin does some interesting things during flash when using the D1 Mini or similar boards. More specifically, when flashing a new firmware, the loader enjoys blinking GPIO2 which is connected to an on-board LED on some boards. GPIO2 is pulled down (digital LOW) to turn on the LED. This will cause problems since the data pin on the DHT22 / AM2302 should remain high when powered on.

The Solution

The quick and dirty way to solve this problem if you experience this problem is to simply remove the power completely. Unplug the board after flashing and plug it back in. (Pro IT tip.) GPIO2 will not be pulled down and you will get readings.

The more sensible solution is to use a better pin for input. I use Random Nerd Tutorials for a comprehensive listing of the ESP8266 pins. If you’re lazy but still want to have an elegant solution, like me, then I would suggest the D3 pin on the D1 Mini, which is GPIO0. It also has a pull-up resistor so you don’t have to add one yourself.

There. Hopefully, I saved you a couple of hairs on your head.

One Reply to “Having trouble connecting the DHT22/AM2302 to the D1 Mini / ESP8266? Here’s a possible solution.”

  1. Just wanted to say thank you for this – a bunch of my DHT22 sensors which came on little “hats” for the d1 mini stopped working and it was driving me insane. I must have read a thousand blog posts before I finally found this post! Luckily I was able to cut the trace and run a bodge wire to the other side to get it working on D3 instead and now they are solid!

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.