Skip to main content
Chemistry LibreTexts

2: Read Temperature and Humidity with DHT22

  • Page ID
    290331
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    You will need

    • ESp8266-NodeMCU Development Board
    • DHT22 Temperature&Humidity sensor
    • Breadboard
    • Jumper Wires
    • USB to MicroUSB cable

    Connect DHT22 sensor

    Connect the DHT22 Temperature and Humidity sensor to Node MCU as shown below. Data (Signal) line is connected to pin D2 of NodeMCU.

    clipboard_e7f2e61ca149f62f0fa815c65a0d4fda7.png
    Figure \(\PageIndex{5}\): DHT22 connected to NodeMCU (Elena Lisitsyna)

    Using USB to MicroUSB cable connect NodeMCU to your computer. You should hear a sound indicating that your computer recognized the device, otherwise you need to go back and make sure you've completed all set up steps.

    Sketch for DHT22

    1. Download DHT Sensor library by Adafruit

    2. Copy the following code to a new Sketch. You can replace the prefilled functions since this code already contains them.

    Note: To comment something out use a double slash (//). If you need to comment out more than one line you can highlight them, Right-click --> Comment/Uncomment. There is also an option with /* and */ (see example in the code below).

    /*
    *Read Temperature and Humidity with DHT22
    *Sketch for ESP8266-NodeMCU
    */
    
    #include "DHT.h"  //Library for the sensor
    #define DHTTYPE DHT22 // There are several DHT sensors that this code can be used for. In our case we use DHT22
    #define DHTPIN D2 // DHT22 sensor's data line is connected to pin D2
    
    float h; //It is always a good idea to declare variables in the very beginning.
    float t;
    
    DHT dht(DHTPIN, DHTTYPE);
    
    void setup() {
      Serial.begin(115200);  //Start serial monitor. We only need to do this once, hence this line of code is in setup()
      dht.begin(); // Start the sensor
    }
    
    void loop() {
      h = dht.readHumidity(); //Store humidity value in variable h
      t = dht.readTemperature(); ////Store temperature value in variable t
      
      Serial.print("Humidity: ");
      Serial.print(h);
      Serial.println(" %");
      Serial.print("Temperature: ");
      Serial.print(t);
      Serial.println(" *C");
    
    
      delay(10*1000);       // print new values every 10 seconds (10,000 milliseconds)
    }
    

    3. Click "Upload" to compile the Sketch and upload to NodeMCU

    4. Open Serial Monitor by clicking  on the magnifying glass in the top right corner of Arduino IDE window.

    5. Make sure baud is set to 115200 as we declared in the code.

    Figure \(\PageIndex{6}\): Serial output for DHT22

     

    Contributors and Attributions

    Robert E. Belford (University of Arkansas Little Rock; Department of Chemistry). The breadth, depth and veracity of this work is the responsibility of Robert E. Belford, rebelford@ualr.edu. You should contact him if you have any concerns. This material has both original contributions, and content built upon prior contributions of the LibreTexts Community and other resources, including but not limited to:

    • Elena Lisitsyna 

     


    2: Read Temperature and Humidity with DHT22 is shared under a not declared license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?