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 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.
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.
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: ");
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.
Serial.print("Minimum Sampling Period: ");
float temperature = dht.getTemperature();
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