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.

Advertisements
This entry was posted in ESP32 and tagged , , , , , , , . Bookmark the permalink.

5 Responses to ESP32 Arduino: Getting humidity measurements from a DHT22 sensor

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

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

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

  4. Michael Reid says:

    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?

    Liked by 1 person

    • antepher says:

      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

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s