Introduction

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.

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

  1. Looking for help in activating esp8266 12f platform with small OLED display on board. Can anybody please point me to suitable library? Display is ready made and already soldered to some pins of esp chip and standard libraries available in arduino environment doesn’t see it (probably), or I do something wrong. Simple blink sketch works perfectly, but display is dead when running examples from available libraries.

  2. Did you try an I2C scanner(just google search “I2C scanner arduino” : if your serial line works -there aare examples in the arduino IDE- and if your oled is an I2C one (ssd 1306) -you were not that explicite-, will give you the I2C aderss of your oled) . Then, u8g2 worked great for me.
    Other question: is it the right place to post to a -maybe i2C; maybe OLED- issue?

Leave a Reply

Your email address will not be published. Required fields are marked *

Captcha loading...