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.

10 Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.