In this Project we will explain how to measure temperature and humidity using DHT22 digital sensor along with ESP8266.

The objective of this post is to explain how to connect the DHT22 temperature and humidity sensor with  ESP8266 NodeMcu and write code to get the measurements and subsequently print them every 2 seconds in the terminal via serial communication.

About DHT22

The DHT22 is a basic, low-cost digital temperature and humidity sensor. It uses a capacitive humidity sensor and a thermistor to measure the surrounding air, and spits out a digital signal on the data pin (no analog input pins needed). It's fairly simple to use, but requires careful timing to grab data. The only real downside of this sensor is you can only get new data from it once every 2 seconds, so when using our library, sensor readings can be up to 2 seconds old. The sensor will come as a 4-pin package out of which only three pin will be used.

Image: DHT22 temperature and humidity sensor

 

The DHT22 is a commonly used Temperature and humidity sensor. The sensor comes with a dedicated NTC to measure temperature and an internal circuit to output the values of temperature and humidity as serial data. The sensor is also factory calibrated and hence easy to interface with other microcontrollers.

The sensor can measure temperature from -40°C to 80°C and humidity from 0% to 100% with an accuracy of ±1°C and ±1%. So if you are looking to measure in this range then this sensor might be the right choice for you.

 

How does the DHT22 work?

The DHT22 consist of a humidity sensing component, a NTC temperature sensor (or thermistor) and an IC on the back side of the sensor.

For measuring humidity they use the humidity sensing component which has two electrodes with moisture holding substrate between them. So as the humidity changes, the conductivity of the substrate changes or the resistance between these electrodes changes. This change in resistance is measured and processed by the IC which makes it ready to be read by a microcontroller.

On the other hand, for measuring temperature these sensors use a NTC temperature sensor or a thermistor. A thermistor is actually a variable resistor that changes its resistance with change of the temperature. These sensors are made by sintering of semiconductive materials such as ceramics or polymers in order to provide larger changes in the resistance with just small changes in temperature. The term “NTC” means “Negative Temperature Coefficient”, which means that the resistance decreases with increase of the temperature.

Wiring

In this example we will have one ESP8266 connected to a DHT22 temperature sensor. The DHT22 has 4 pins: VCC, Data, NC (not connected, no function) and GND. The GND of both devices need to be wired together. VCC can be either the 3V3 pin (3.3V output) or Vin (5V output), both on the ESP8266 or an external voltage source. As the DHT22 can operate at voltages between 3.3-6V,  we connect this sensor to the ESP8266's Vin pin in this example. The data pin of the DHT22 is connected to GPIO pin 5 of the ESP8266 in this example.

 

Connect your DHT22 to your Node MCU 1.0 as follows:

  1. Connect 3V3 ( 3.3V pin) or Vin (5V pin) of the Node MCU  to the Vcc pin of the DHT22
  2. Digital pin D1 (GPIO 5) to the second pin of the DHT22
  3. Connect a 5K resistor data pin and VCC pin of the DHT22.
  4. Leave the third DHT22 pin unconnected
  5. Connect the fourth pin of the DHT22 to ground of ESP8266.

Image: Wiring of DHT22 with ESP8266. The Data line requires a 5k pull-up

 

 

 

Materials Needed

For the sake of simplicity, this project is demonstrated in its most basic form in such that features and materials are kept at a minimum.  Following materials are needed for this project:

  1. DHT22 Temperature Sensor
  2. NodeMCU ESP8266
  3. PC or laptop
  4. Breadboard
  5. Male-to-female jumper cables

 

 

 

ESP8266 Code

First, we start by including the libraries needed for all the functionality. We need the DHT sensor library for ESPx, in order to be able to get temperature and humidity data from the DHT22. This  library can be installed via Arduino IDE library manager.

Image: Install the required library from the Arduino IDE's library manager

The coding shown here is based on the examples provided in the library, which I encourage you to try.

#include "DHTesp.h"

#ifdef ESP32
#pragma message(THIS EXAMPLE IS FOR ESP8266 ONLY!)
#error Select ESP8266 board.
#endif

DHTesp dht;

void setup()
{
  Serial.begin(115200);
  Serial.println();
  Serial.println("Status\tHumidity (%)\tTemperature (C)");


  // Autodetect is not working reliable, don't use the following line
  // dht.setup(17);
  // use this instead: 
  dht.setup(5, DHTesp::DHT22); // Connect DHT sensor to GPIO 5
}

void loop()
{
  delay(2000);

  float humidity = dht.getHumidity();
  float temperature = dht.getTemperature();

  Serial.print(dht.getStatusString());
  Serial.print("\t");
  Serial.print(humidity, 1);
  Serial.print("\t\t");
  Serial.println(temperature, 1);

}

 

Test the Setup

After uploading the code to the board, open up your serial monitor, set the baud rate to 115200, and you should see something like the following:

 

 

Image: Serial output of temperature and humidity measurements

 

Wrap-Up

We connected the ESP8266 with the DHT22 and added code to read the measurements and output the temperature and humidity via serial communication. This is the most basic configuration and there are plenty other options to add functionalities. From here, you could set up a webserver on your esp8266 that you can visit to check the temperature and humidity, or you could display the results on an LCD or OLED screen. Another option would be to publish this data via MQTT protocol and read it froom multiple subscribers.

(Visited 245 times, 14 visits today)