The DHT11 (Digital Temperature Humidity 11) is a sensor that generates a digital output signal encoding a temperature and humidity value measured in real time.
Temperature and humidity sensors can be used in a wide range of applications, from determining the temperature outside, to managing the watering of a small autonomous greenhouse… That’s why we’ve put together a small table summarizing the sensor’s characteristics:
DHT11 | Operating Value | Accuracy |
---|---|---|
Temperature (in °c) | [0°C – 50°C] | +/- 2 °C |
Humidity (in %) | [20% – 80%] | +/- 5% |
As you can see from the table, the DHT11 sensor cannot measure negative temperatures! So be careful when using it outdoors!
Here are the different pins of the DHT11 sensor:
To reproduce our circuits on Fritzing, you need the DHT11 temperature sensor component. You can watch our tutorial on how to install it in the software!
To compile the programs that run the temperature sensor, you’ll need the DHT11 library.
This is a .zip file, so to install it, go to sketch, then include library and add .zip library. You can find more details in our course on libraries.
A library is never mandatory in a program. It allows you to limit code length and create functions to make component handling easier. So, let’s take a look at the various functions offered by the DHT11 library, and in particular at the one that could be most useful to you:
DHT_nonblocking( pin, type) : This function initializes the library with the correct sensor and the pin to which it is assigned.
measure( float *temperature, float *humidity ) : Retrieves temperature and humidity values.
read_temperature( ) : Calculates temperature.
read_humidity( ) : Calculates humidity.
read_nonblocking( ) : Ensures that the sensor continues to read temperature and humidity, pausing briefly to cool down and save energy.
Now we’ll see how to display temperature and humidity on the serial monitor. Don’t forget to install the temperature sensor library to run the program.
#include <dht_nonblocking.h> // Call the sensor library
#define DHT_SENSOR_TYPE DHT_TYPE_11 // Define DHT11 as the library works for several temperature sensors
static const int sensor_temperature = 2; // We define where the temperature sensor is connected.
DHT_nonblocking dht_sensor( capteur_temperature, DHT_SENSOR_TYPE ); // Initialize library with correct pin and sensor type
void setup( ){
Serial.begin( 9600);} // Serial monitor is initialized.
static bool measure_environment( float *temperature, float *humidity ){ // We create a function to retrieve the value.
static unsigned long pause_entre_mesure = millis( ); // We define the time with which we'll take measurements
if( millis( ) - pause_entre_mesure > 3000ul ) { // We calculate the measurement every 4 seconds
if( dht_sensor.measure( temperature, humidity ) == true ){ // We take the new value if it has changed
pause_entre_mesure = millis( );
return( true );
}
}
return( false );}
void loop(){
float temperature; // We declare a variable to retrieve the temperature value
float humidite; // We declare a variable to retrieve the temperature value
if( measure_environment( &temperature, &humidite ) == true ) { // If values have changeddata: image/pjpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEB
This is what you see on the serial monitor: