Using the DHT11 Temperature Sensor with a WeMos D1 Mini (ESP8266)
In this blog post I talk about the additional steps needed to use the DHT11 temperature sensor with a WeMos D1 Mini (ESP8266) with the Arduino IDE.
Differences from running a DHT11 on an Arduino
There are two major differences to bear in mind when using the DHT11 on a Wemos D1 Mini (ESP8266) or similar chipset.
The first is that the pin out is different in the Arduino IDE than when using the Arduino. I go into more details in my blog post Pin numbering for WeMos D1 Mini (ESP8266) but simply put, you need to use different pin numbers. If you haven’t read this and are having trouble connecting your DHT11 properly I recommend having a read.
The second issue is that the standard DHT11 library doesn’t work very well with the WeMos. In my case it was failing to compile due to issues in the architecture. After trying a couple libraries I found the easiest to use and install was the “DHT sensor library for ESPx” by beegee_tokyo.
After doing some investigation the reason the standard DHT11 library doesn’t work is that the Arduino is a lot slower than the ESP8266. This affects the standard DHT11 library as the DHT11 has strict timing requirements with its interface. The increased speed of the ESP8266 causes issues communicating with the DHT11 and stops it working.
Wiring it up to a WeMos D1 Mini
Here I am connecting the DHT11 sensor to a WeMos D1 mini. For ease of connecting up the sensor I connect the 3.3 volt and GND line to the breadboard. From there I connect the 3.3 volt line to the first pin on the DHT11.
The third pin is connected to the GND line from the WeMos.
The central pin is used for data and is connected to the D5 pin on the WeMos. In addition I connect a 10k resistor between this data pin and the 3.3 volts line to act as a pull-up resistor.
It is important not to connect the DHT11 sensor to the 5 volt line. This is because the power is used at the data pins and would therefore flow back into the WeMos.
Unlike the Arduino, the WeMos data pins are rated at 3.3 volts max. This means connecting the 5 volts to them will damage the microcontroller. Since the DHT11 supports between 3 and 5 volts it is perfectly fine to use the 3.3 volts line.
Programming the Wemos with the DHT11
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(D5, DHTesp::DHT11); } 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::DHT11
constant is used in the setup function to tell configure the dht
variable to talk to the sensor using the DHT11 protocol.
Summary
The DHT11 sensor is a useful and cheap temperature and humidity sensor that can be used with the WeMos D1 mini. Although the standard DHT11 libraries cannot be used there are similar libraries designed for ESP8266 chipsets such as the WeMos D1 mini.
For a full review of the DHT11 have a read of my previous post.
How can I use multiple DHT11s on the same WeMos? I have tried wiring up three to different sensor pins with resisters and different dhts and each to a different power pin, one common ground. The first sensor is read but the second one fails to read.
>and each to a different power pin
There are only two power pins on the Wemos, what do you mean by this?
As long as you are wiring them each to their own digital pin this should work fine. Are you using the right pin numbers? See: https://chewett.co.uk/blog/1066/pin-numbering-for-wemos-d1-mini-esp8266/
Curious that you mentioned that the D1 mini will not accept 5v for input on the signal pins. I’ve had a DHT22 connected to 5v power for a couple of months and have had no issues with the data input to the Wemos. Since reading your notes, I’ve changed it to 3.3v, but curious if this is from the spec sheet, and if anyone has actually had an issue with frying any inputs when using 5v to power the DHT22 or 11 (or other devices)?
Part of it will depend on the quality of the components and silicon on the device. The docs: https://www.wemos.cc/en/latest/d1/d1_mini.html state that it all operates on 3.3 volts however there will be tolerances. It may be that your WeMos will work fine with it but the increased voltage over time may slowly damage it.
Its one of those things where, really its somewhat determined on each component and wont easily be able to be answered one way or another…
Thank you for this. It was amazingly helpful. Kinda of late to the party, but I really enjoyed the article. It’s hard to find 8266 libraries without help !