ESP32 Arduino: Getting humidity measurements from a DHT22 sensor

In this tutorial, we will check how we can get humidity measurements from a DHT22 sensor, with the Arduino core running on the ESP32. To make the interaction with the sensor easier, I’m using a DFRobot DHT22 module which has all the additional electronics needed and exposes a wiring terminal that facilitates the connections.


Introduction

In this tutorial, we will check how we can get humidity measurements from a DHT22 sensor, with the Arduino core running on the ESP32.

The DHT22 is a temperature and humidity sensor and you can check on the previous tutorial how to get temperature measurements.

We will use this library to interact with the device. It is available for installation using the Arduino IDE libraries manager and the procedure to install it is detailed on the mentioned post.

For the schematic diagram needed to connect the ESP32 to the DHT22, please also consult the mentioned previous post.

To make the interaction with the sensor easier, I’m using a DFRobot DHT22 module which has all the additional electronics needed and exposes a wiring terminal that facilitates the connections.

The tests were performed using a DFRobot’s ESP32 module integrated in a ESP32 development board.


The code

The code for this tutorial will be really simple and it is pretty much similar to what we did to obtain temperature measurements.

The first thing we need to do is including the DHT library, so we have access to all the higher level functions that allow us to interact with the sensor without needing to worry about the single wire protocol it uses to exchange data with a microcontroller.

#include "DHTesp.h"

Then we need an object of class DHTesp, which exposes the methods needed to get both temperature and humidity measurements. In our specific case, we will use it to get humidity.

DHTesp dht;

Moving on to the Arduino setup, we will first open a serial connection to output the measurements and get them later using the Arduino IDE serial monitor.

Serial.begin(115200);

Besides that, we need to initialize the sensor interface and specify the ESP32 pin to which the sensor is connected. We do this by calling the setup method of the dht object we created before, passing as input the pin number. I’m using pin 27, but you can try with another.

Note that the setup function can take as optional argument an enumerated value specifying which sensor we are using (the library supports multiple ones). Nonetheless, if we don’t pass this argument, the function will try to automatically detect the sensor used.

 
dht.setup(27);

Moving on to the Arduino loop, we will periodically get the humidity measurements and print them to the serial interface.

To get a humidity measurement, we simply need to call the getHumidity method on our dht object. This function call takes no arguments and returns the humidity in percentage, as a float.

float humidity = dht.getHumidity();

We will then print the value to the serial interface and make a 10 seconds delay before getting the next measurement.

Serial.print("Humidity: ");
Serial.println(humidity);

delay(10000);

The final source code can be seen below.

#include "DHTesp.h"

DHTesp dht;

void setup()
{
  Serial.begin(115200);

  dht.setup(27);
}

void loop()
{

  float humidity = dht.getHumidity(); 

  Serial.print("Humidity: ");
  Serial.println(humidity);

  delay(10000);

}


Testing the code

To test the code, simply compile it and upload it to your device after having all the components wired accordingly to the schematic from the previous post.

Once the procedure finishes, open the Arduino IDE serial monitor. There, you should have an output similar to figure 1, which shows the measurements getting printed.

ESP32 Arduino DHT22 Get Humidity.PNG

Figure 1 – Output of the program.

7 thoughts on “ESP32 Arduino: Getting humidity measurements from a DHT22 sensor”

  1. Pingback: ESP32 Arduino: Temperature, humidity and CO2 concentration web server | techtutorialsx

  2. Pingback: ESP32 Arduino: Temperature, humidity and CO2 concentration web server | techtutorialsx

  3. Pingback: ESP866 Arduino: Getting temperature and humidity with a DHT22 sensor | techtutorialsx

  4. Pingback: ESP866 Arduino: Getting temperature and humidity with a DHT22 sensor | techtutorialsx

  5. Pingback: ESP32 Espruino DHT22: Getting temperature and humidity | techtutorialsx

  6. Pingback: ESP32 Espruino DHT22: Getting temperature and humidity | techtutorialsx

  7. So I copied this sketch (and the temperature sketch) and tried to compile it and received these error messages (in both cases):

    Arduino: 1.8.5 (Mac OS X), Board: “ESP32 Dev Module, Disabled, Default, QIO, 80MHz, 4MB (32Mb), 115200, None”

    /Users/michaelreid/Documents/Arduino/DHTesp_tester/DHTesp_tester.ino: In function ‘void setup()’:
    DHTesp_tester:9: error: call of overloaded ‘setup(int)’ is ambiguous
    dht.setup(25);
    ^
    In file included from /Users/michaelreid/Documents/Arduino/DHTesp_tester/DHTesp_tester.ino:1:0:
    /Users/michaelreid/Documents/Arduino/libraries/DHT_sensor_library_for_ESPx/DHTesp.h:125:8: note: candidate: void DHTesp::setup(uint8_t)
    void setup(uint8_t dhtPin) __attribute__((deprecated));
    ^
    /Users/michaelreid/Documents/Arduino/libraries/DHT_sensor_library_for_ESPx/DHTesp.h:126:8: note: candidate: void DHTesp::setup(uint8_t, DHTesp::DHT_MODEL_t)
    void setup(uint8_t pin, DHT_MODEL_t model=AUTO_DETECT);
    ^
    exit status 1
    call of overloaded ‘setup(int)’ is ambiguous

    Any suggestions for resolving this error?

    1. Hi!

      I’m not sure what is causing the problem. In my environment it compiles fine.

      My suggestion is to open an issue on the GitHub page of the libraries to check if someone can help.

      Let us know if you manage to solve the problem 🙂

      Best regards,
      Nuno Santos

      1. I had the same error. I fixed it by avoiding to use the AUTO_DETECT parameter, but specifying the used sensor in the dht.setup() method.

        Try to pass the used sensor like this: dht.setup(25, DHTesp::DHT22). Or use one of these (depending on your used sensor):
        DHT11,
        DHT22,
        AM2302, // Packaged DHT22
        RHT03 // Equivalent to DHT22

        Best regards
        Richard

  8. So I copied this sketch (and the temperature sketch) and tried to compile it and received these error messages (in both cases):
    Arduino: 1.8.5 (Mac OS X), Board: “ESP32 Dev Module, Disabled, Default, QIO, 80MHz, 4MB (32Mb), 115200, None”
    /Users/michaelreid/Documents/Arduino/DHTesp_tester/DHTesp_tester.ino: In function ‘void setup()’:
    DHTesp_tester:9: error: call of overloaded ‘setup(int)’ is ambiguous
    dht.setup(25);
    ^
    In file included from /Users/michaelreid/Documents/Arduino/DHTesp_tester/DHTesp_tester.ino:1:0:
    /Users/michaelreid/Documents/Arduino/libraries/DHT_sensor_library_for_ESPx/DHTesp.h:125:8: note: candidate: void DHTesp::setup(uint8_t)
    void setup(uint8_t dhtPin) __attribute__((deprecated));
    ^
    /Users/michaelreid/Documents/Arduino/libraries/DHT_sensor_library_for_ESPx/DHTesp.h:126:8: note: candidate: void DHTesp::setup(uint8_t, DHTesp::DHT_MODEL_t)
    void setup(uint8_t pin, DHT_MODEL_t model=AUTO_DETECT);
    ^
    exit status 1
    call of overloaded ‘setup(int)’ is ambiguous
    Any suggestions for resolving this error?

    1. Hi!
      I’m not sure what is causing the problem. In my environment it compiles fine.
      My suggestion is to open an issue on the GitHub page of the libraries to check if someone can help.
      Let us know if you manage to solve the problem 🙂
      Best regards,
      Nuno Santos

      1. I had the same error. I fixed it by avoiding to use the AUTO_DETECT parameter, but specifying the used sensor in the dht.setup() method.
        Try to pass the used sensor like this: dht.setup(25, DHTesp::DHT22). Or use one of these (depending on your used sensor):
        DHT11,
        DHT22,
        AM2302, // Packaged DHT22
        RHT03 // Equivalent to DHT22
        Best regards
        Richard

  9. Dear Mr. Richard
    thanks for information, just i have one question for you if i have DHT21 what i can do?
    because when i compile i have error “DHT21′ is not a member of ‘DHTesp”
    and for DHT22 it compile whith out any problem .
    thanks for your help.
    best regards.

  10. Dear Mr. Richard
    thanks for information, just i have one question for you if i have DHT21 what i can do?
    because when i compile i have error “DHT21′ is not a member of ‘DHTesp”
    and for DHT22 it compile whith out any problem .
    thanks for your help.
    best regards.

Leave a Reply

Discover more from techtutorialsx

Subscribe now to keep reading and get access to the full archive.

Continue reading