0

$USD

$USD
TUTORIALS ESP32

ESP32 Arduino: DHT22 Minimum Sampling Period

DFRobot Apr 26 2018 2014

In this tutorial we will check how to get the minimum sampling period of the DHT22 and how to use it to make some temperature measurements without crossing the maximum sampling rate the sensor suports.

We are going to use this library to interact with the DHT22 from the ESP32, using the Arduino core. The details for installing this library, which is available for installation on the Arduino IDE libraries manager, can be seen here. The electronic schematic for the connection between the sensor and the ESP32 is also detailed in that post.

So, the minimum sampling period is the minimum time interval that we need to wait between two consecutive measurements from the sensor. In the case of the DHT22, this value is of 2 seconds.

Depending on your application, this value may be acceptable or not. Nonetheless, for the typical applications of measuring the environmental temperature and humidity which, in normal conditions don’t have a very fast variation, this sampling period should be acceptable.

We can obtain this minimum sampling period value in our code using the previously mentioned library, as we will be seeing in the coding section.

To facilitate the interaction with the sensor, I’m using a DFRobot DHT22 module which already has all the 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 initial part of the code will be similar to the previous posts. We start by including the DHT library and then we declare an object of class DHTesp.

#include "DHTesp.h" DHTesp dht;

Moving on to the Arduino setup function, we start by opening a serial connection to output the results of our program.

Followed by that, we initialize the sensor interface by calling the setup method on the DHTesp class object we just created. As input, this method receives the pin of the ESP32 that is connected to the DHT22.

Serial.begin(115200); dht.setup(27);

Now, still in the setup function, we will obtain and print the minimum sampling period by calling the getMinimumSamplingPeriod method of our object.

This method takes no arguments and returns the minimum sampling period in milliseconds, as an integer.

Note that this may seem unnecessary taking in consideration that we can easily get the value from a sensor datasheet and hard code it in our application. Nonetheless, following this approach, we can develop a generic program that can work with the multiple sensors supported by the library, which may have distinct minimum sampling periods.

Serial.print("Minimum Sampling Period: "); Serial.println(dht.getMinimumSamplingPeriod());

Moving on to the Arduino loop function, we will collect some temperature measurements periodically. As mentioned in the introductory section, we need to wait the minimum interval of 2 seconds between consecutive measurements. Thus, we can use the value from returned by the getMinimumSamplingPeriod method as delay in our Arduino loop.

Note however that this not ensures that we are obtaining samples at a rate of  0.5 per second (the inverse of the minimum sampling period is the maximum rating at which we can obtain samples).

This is because the delay function doesn’t guarantee the period of execution of the code. Besides the 2 seconds introduced by that function call, we need to take in consideration the execution of all the remaining code.

So, we are only using this delay to guarantee that we are not exceeding the maximum sampling rate. If you need to keep a fixed well defined sampling rate, there are other approaches that can be followed, such as using an hardware timer.

delay(dht.getMinimumSamplingPeriod());Next we can simply obtain a temperature measurement by calling the getTemperaturemethod on our dht object, like we did in previous post.

float temperature = dht.getTemperature();The final source code can be seen below. It already includes the printing of the obtained temperature measurements to the serial interface.#include "DHTesp.h" DHTesp dht; void setup() {  Serial.begin(115200);  dht.setup(27);  Serial.print("Minimum Sampling Period: ");  Serial.println(dht.getMinimumSamplingPeriod()); } void loop() {  delay(dht.getMinimumSamplingPeriod());  float temperature = dht.getTemperature();  Serial.println("------------------");  Serial.print("Temperature: ");  Serial.println(temperature); }

Testing the code

To test the code, simply follow the usual procedure of compiling it and uploading it with the Arduino IDE, after the wiring between the sensor and the ESP32 is done.

Once the procedure finishes, open the Arduino IDE serial monitor. You should get an output similar to figure 1, where the minimum sampling period is printed at the beginning of the program.

Followed by that, it should start to output temperature measurements periodically.


 

Figure 1 – Output of the program


 

Note that the period printed is coherent with what was mentioned in the introductory section. It corresponds to 2000 ms, which are 2 seconds, as stated in the sensor datasheet.

DFRobot supply lots of esp32 arduino tutorials and esp32 projects for makers to learn.