Using the DHT22 Temperature Sensor with a WeMos D1 Mini (ESP8266)
In this blog post I talk about the additional steps needed to use the DHT22 temperature sensor with a WeMos D1 Mini (ESP8266) with the Arduino IDE.
Differences from running a DHT22 on an Arduino
There are two major differences to bear in mind when using the DHT22 on a Wemos D1 Mini (ESP8266) or similar chipset.
The primary difference when using the Arduino IDE with the WeMos chips is that the standard pin numbers do not refer to the pin numbers on the WeMos. For example, instead of using 1
to refer to digital pin 1
, you need to use the constant D1
.
I go into more details for this in my blog post Pin numbering for WeMos D1 Mini (ESP8266). If you haven’t read this and are having trouble connecting your DHT22 properly I recommend having a read.
The second and more problematic issue is that the standard DHT22 library does not work with the WeMos. After some work looking into this I found that this was because the ESP8266 runs much faster than the Arduino. Since the DHT library has quite strict timing requirements this means that it fails to work correctly.
However there is a “DHT sensor library for ESPx” by beegee_tokyo which works well.
Wiring it up to a WeMos D1 Mini
Here I am connecting the DHT22 sensor to a WeMos D1 mini. To more easily connect the sensor I have connected the 3.3 volt and GND lines to the breadboards + and – lines respectively.
In the diagram I am showing the four pin version of the DHT22. To more easily show the sensor diagram I have reversed the DHT22 sensor. The pins left to right are VCC (show on the right on the diagram), Data, not connected (not present in the 3 pin version), and GND (shown on the left on the diagram).
It is important to note that the WeMos’s digital pins can only accept up to 3.3 volts maximum on their inputs. This means that the DHT22 sensors VCC pin must be connected to the 3.3 volts and not the 5 volt line.
The data pin is connected to D2 on the WeMos. This is also connected to a 10k resistor between the data pin and the 3.3 volt line on the breadboard. This pull-up resistor ensures that the line is pulled up when floating.
Programming the Wemos with the DHT22
Here instead of using the standard DHT library I am using DHTesp. This library built specifically to use DHT devices on ESP8266 microcontrollers.
I was able to use this library to log the temperature and humidity and print it out on the serial connection.
#include "DHTesp.h" DHTesp dht; void setup() { Serial.begin(115200); dht.setup(D2, DHTesp::DHT22); } void loop() { float h = dht.getHumidity(); float t = dht.getTemperature(); Serial.print("{\"humidity\": "); Serial.print(h); Serial.print(", \"temp\": "); Serial.print(t); Serial.print("}\n"); delay(2000); }
The DHTesp::DHT22
constant is used in the setup function to tell configure the dht
variable to talk to the sensor using the DHT22 protocol.
Summary
The DHT22 sensor is a really nice sensor to measure temperature and humidity that can be used with the WeMos D1 mini. Although there is some complication as the standard DHT22 library cannot be used there is a library designed for the ESP8266 chipset such as the WeMos D1 mini.
For a full review of the DHT22 have a read of my previous post.
When I connected my dht22 to my wemos d1 mini, I used USB power and connected dht22 to 5v on wemos, and I didn’t use the resistor (ground went to ground and data went to d4)
It seems to work, I’ve got three wemos/dht22 combos, one on each level of the house and they’re broadcasting to blynk app on my phone. Everything looks correct.
Are the dht22’s going to have a shorter life connecting this way? Will my house explode?
Hi Trevor,
Im not entirely surprised it worked as the ESP8266 (the WeMos) is a pretty versatile micro-controller. To address your points:
Wiring the DHT22 to 5 Volts – This is something that will mostly work however the WeMos isn’t specced to be able to take such voltages. Possibly you may see a reduced lifespan of the WeMos device. But if it didnt break immediately when connected, fingers crossed!
Not using a resistor on the data line – This will certainly work as the resistor is only used to pull the data line high. It is used to reduce the amount of noise. This means without it you might get some bad data now and then, but it isn’t likely to be massively visible.
I hope that answers your questions
Chris
Thanks alot for your reply. I do notice I get a “Failed to read from DHT sensor!” now and then but I was sampling every second and it wasn’t a big issue. Now I’ve switched it to sample every minute and it seems like every reading is a Fail. I’m going to try and connect it as you have done, to the 3.3V and with a pullup resistor.
I could not get my WeMos D1 mini to read from the DHT22 with any example code I found, but when I tried connecting the sensor to 5V instead of 3.3V it’s now working!
Hi,
This is great, thank you. I’ve wasted over a week trying to get a WEMOS working and have finally managed to get one to connect to my WiFi, and serial print values from a DHT22. However, as a total beginner I’m not sure how to put it all together. I am using OpenHAB as a home automation solution, so guessing MQTT would be required. Do you have any suggestions for me?
Many thanks in advance,
Bret
I’ve got a DHT22 working in a D1 Mini based thermometer (managed by Home Assistant) and no problems (even had it hooked up to 5v for over a month and it’s been purring like a kitten). However, when trying to implement it into a garage-door opener, I’m running into some issues. Found this page in my search for answers, but I have more questions than answers, I’m afraid.
The code snippets you’re included are a little different than what I’ve been using to program the D1 for both my Thermostat and garage-door opener. For instance, my code uses the following format to import the libraries:
#include //ESP8266 Core WiFi Library
#include
#include
#include
But your code uses the following format:
#include “DHTesp.h”
What are you using to generate your code? I’m a relative nuub to arduino and the ESP8266, so it’s probably obvious to most of your readers. I’m using Arduino 1.8.13 to create and compile my sketches, and so far I’ve been learning from examples that others have posted online. Any clarification would be helpful. Thanks!
Oi!! The system removed most of my example code! Basically, the file names in the #include statements were surrounded by angle-brackets , rather than double-quotes. Hope my example doesn’t get removed here as well!
Sorry about the removal, it was wordpress!
#include ANGLEBRACKET Filename ANGLEBRACKET
Is typically used for references to any library code, some compilers will look in the libary directorys first (and some will only look there).
#include “filename”
Is typically used to refer to a location which is in the local directory first, before going to the library directories.
For the most part, they are prettymuch the same, but some compilers might treat them slightly differently. I haven’t been too careful about which I use.
I’m still using the Arduino IDE because it makes it simple to upload to the devices and the serial console is pretty useful!
Thanks so much for the response. That does clear it up, though raises some additional questions (or at least eyebrows). lol
Thanks for you blog post. It wasn’t working for my WeMos D1 Mini using the standard library. Now it does using DHTesp!