The Problem
There are multitudes of tutorials out there that 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 through a quick web search tell you to connect the data pin of the 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 that are frustrated but patient, you may notice that after 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 it 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. Which
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 simply to unplug the board after flashing and plugging 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 love going to 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 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.