Microcontroller Tutorials

Arduino, Raspberry Pi, PIC, Beaglebone Resources

ESP8266 NodeMCU DHT22 Interfacing Tutorial

NodeMCU DHT22

The DHT22 is a temperature and humidity sensor in one package. It is widely used mainly due to its price but also because of it being robust and user-friendly. In this NodeMCU DHT22 interfacing tutorial, I will guide you in creating an IoT-based weather monitoring project.

Introduction

The DHT22 is an upgrade over the DHT11. The upgrade, in this sense, is on the temperature and humidity range and sampling rate. The table below summarizes the difference between the DHT11 and DHT22.

Parameter DHT11 DHT22
Temperature Range 0 to 50°C -40 to 125°C
Humidity Range 20 to 80% 0 to 100%
Sampling Rate Once a second Twice a second
Dimension 15.5mm x 12mm x 5.5mm 15.1mm x 25mm x 7.7mm

Like its predecessor, the DHT22 contains only three usable pins: VCC, DOUT and GND.

NodeMCU DHT22

Depending on your chosen module, the DHT22 requires a pull-up resistor of 10k on its DOUT pin. The DHT22 module by DFRobot as pictured does not require any pull-up resistor. See more information about the DHT22.

NodeMCU DHT22 Interfacing


The DOUT pin connects to any digital pin of the NodeMCU. As mentioned, a 10k pull-up resistor on the DOUt pin is necessary except if you own DFRobot’s module. The VCC pin and GND pin connect to the 3.3V pin and GND pin of the NodeMCU respectively.

We will use the following diagram for the rest of the tutorial:

NodeMCU DHT22 Fritzing diagram

DHT22 Pin NodeMCU Pin
VCC 3.3V
DOUT D2
GND GND

We will be using the Arduino IDE to program our NodeMCU. As for the sketch, using the Simple DHT library makes our work shorter. The library comes with a basic example which we can use to test if the DHT22 to NodeMCU interface is correct:


Upload the sketch, open serial monitor and the humidity and temperature readings should appear.

IoT Weather Monitor

Now that we have successfully read both temperature and humidity from the DHT22, it is time to make a simple IoT weather monitor.

We will modify the sketch in the NodeMCU AJAX tutorial to display both temperature and humidity in a web page. Using AJAX here would be great because we want the temperature and humidity in real time.


Upload the sketch above to your NodeMCU and open serial monitor to acquire the IP address of your device. Don’t forget to supply your own WiFi SSID and password on lines 10 and 11.

Using your preferred browser, visit the IP address as seen on the serial monitor. The temperature and humidity is now visible and updates in real time.

 

One thought on “ESP8266 NodeMCU DHT22 Interfacing Tutorial

  1. Thank you for the ESP 8266 sample code.
    I tried to copy and move immediately but I got an error code.
    Line 74 says invalid operands of types 'const char *' and 'const char [3]' to binary 'operator +'.
    I am a beginner and I do not know what I am saying.
    I would like to know how to fix it.
    This may be confusing because it was translated automatically. I'm sorry.

Leave a Reply

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

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