0

$USD

$USD
TUTORIALS ESP32

ESP32 Arduino tutorial: Getting humidity measurements from a DHT22 sensor

DFRobot Apr 26 2018 2077

In this esp32 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.

Figure 1 – Output of the program.
DFRobot supply lots of esp32 arduino tutorials and esp32 projects for makers to learn.