by Floris Wouterlood – July 1, 2017
Summary
The DHT11 is a very inexpensive sensor that, in combination with an Arduino microcontroller, measures the ambient temperature and relative humidity.
The purpose of this paper is to provide a beginner with the basics of connecting a DHT11 to an Arduino and the instructions necessary to get the temperature and relative humidity data out of the sensor.
Introduction
The amount of water in the air that surrounds us is very important for humans and for the plants and animals that breathe air from the earth’s atmosphere. The amount of water that the atmosphere can carry depends on the air temperature. Hot air can hold much more water than cold air. When hot, moist air becomes colder and colder a certain temperature threshold will be reached: the dew point, below which water vapor condenses in the air and forms water droplets: clouds, fog, rain, snow, hail. As a consequence the humidity of the atmosphere is always expressed as relative humidity, that is, in relation to the air temperature. In physical terms: the relative humidity of air is the ratio of the partial pressure of water vapor to the equilibrium vapor pressure at a given temperature (wikipedia).
A high relative humidity of the air around us makes us feel uncomfortable because the ability of our skin to dissipate redundant body heat by sweating is diminished. Dry air isn’t comfortable either: when the relative humidity is below 30% the our noses start to feel dry and unhappy.
Relative humidity can be very accurately measured with a big, hand held device called a psychrometer. In many homes old fashioned analog hair hygrometers indicate the humidity. Today, electronics does the work for us, fast and economically. A typical, very inexpensive device is the DHT11 (see fig. 1) which contains a thermistor and a humidity sensor that measures resistance over a substrate whose resistance capacity depends on the amount of water absorbed from the air. Thus, the DHT11 measures two essential parameters in the surrounding air: temperature and humidity. If the data pin of a DHT11 is connected to an Arduino these two parameters can be requested from the device.
DHT11: pins and connectivity with an Arduino
Figure 1 shows a DHT 11. The square openings in the front serve to allow air to reach the sensors unobstructed. When you look at the front of the device you notice four pins sticking out from the bottom. From left to right these pins are called Vcc, Data, NC and GND. Vcc delivers 5V DC power to the DHT11, Data is the data carrier pin, NC is redundant (not connected) while GND needs to be connected with ground. Breakout boards with a DHT soldered on it usually have three pins marked Vcc, Data and GND.
The accuracy of the DHT is relatively low. My experience is an inaccuracy of 1 degree Celsius and 10% difference in relative humidity. The inaccuracy is a function of price, so if you want more reliable readings, the solution is to buy a more expensive sensor.
Connectivity with an Arduino is quite straightforward and is pictured in figure 1. We use in this example an Arduino Nano, but an Arduino Uno has the same pin layout as the Nano and works equally well.
Connectivity table
pin on the DHT11 – connects to …. | pin on the Arduino marked |
Vcc | 5V |
Data | Any digital pin. In this example: pin 10 |
NC | |
GND | GND |
.
Pull up resistor
A 10 kΩ resistor must be mounted between the Data pin and Vcc pin to keep the signal level high during operation of the DHT11. Some breakout boards already contain a 10 kΩ resistor which makes wiring to the Arduino extremely easy. A 10 kΩ pull up resistor is incorporated in the wiring scheme of Figure 1.
Figure 1: Connection wiring scheme of a DHT11 and an Arduino, in this example a Nano. The Data pin of the DHT11 is here connected with pin 10 of the Arduino but any of the other pins D1-D12 or A0-A5 of the microcontroller board may be used. As pin 13 of the Arduino contains a built-in pull up resistor, problems may arise using this pin.
Electronics and supplies
1x Arduino Nano microcontroller board, 1x 10 kΩ resistor, breadboard, jumper wires.
Figure 2: Working version of a Nano mounted together with a DHT11 on a breadboard. Note the 10 kΩ resistor placed between 5V and the Data pin of the DHT11.
Sketch
Below is the complete ‘bare’ sketch. An up- and running Arduino IDE on your computer is necessary. Copy-paste the list of instructions below into an empty new file in the Arduino IDE,
// sketch
// single DHT11 temp-humidity sensor on Arduino with serial monitor reporting only
// original written by Tim Stephens 2013
// http:www.tjstephens.com/blog/2013/11/23/temperature-logger/
// public domain
// modified Floris Wouterlood July 1, 2017
// based on DHT11 examples by Ladyada.
// data pin of DHT11 sensors wired to pin 10 on Arduino
// 10k pull up resistor (between Data and 5V)
#include “DHT.h”
float h, t;
DHT DHT_sens(10, DHT11); // datapin sensor connected to pin 10 Arduino
void setup() {
DHT_sens.begin();
Serial.begin (9600);
Serial.println (“===============================================”);
Serial.println (“Bare DHT11 temp-humidity sensor – June 30, 2017”);
Serial.println (“===============================================”);
Serial.println (” “);
}
void loop(){
// ==== read from buffer and display =========================
h = DHT_sens.readHumidity();
t = DHT_sens.readTemperature();
delay (1000); // pause a second
Serial.print (“Humidity: “);
Serial.print (h,0); // zero decimal
Serial.print (” %\t”);
Serial.print (“Temperature: “);
Serial.print (t,1); // one decimal
Serial.println (” *C”);
delay (1000); // pause a second
}
Let’s go through the sketch
#include “DHT.h”
• calls the library DHT.h. This library is necessary for compiling the instructions to machine format
float h, t;
• one has to declare variables. Variables that have decimal notation must be declared as ‘float’
DHT DHT_sens(10, DHT11); //datapin sensor connected to pin 10 Arduino
• tells the microcontroller to expect a DHT11 at pin 10
DHT_sens.begin();
• instructs the microprocessor to initialize the DHT sensor
Serial.begin (9600);
Serial.println (“===============================================”);
Serial.println (“Bare DHT11 temp-humidity sensor – June 30, 2017”);
Serial.println (“===============================================”);
Serial.println (” “);
• instructs the microcontroller to start the serial port and to return a few introductory lines via the serial port (USB connection) to the Serial Monitor of the Arduino IDE
in the ‘loop’ section:
// ==== read from buffer and display =========================
h = DHT_sens.readHumidity();
t = DHT_sens.readTemperature();
delay (1000); // pause a second
Serial.print (“Humidity: “);
Serial.print (h,0); // zero decimal
Serial.print (” %\t”);
Serial.print (“Temperature: “);
Serial.print (t,1); // one decimal
Serial.println (” *C”);
delay (1000); // pause a second
• h and t are humidity and temperature, respectively. h is always in percentage (because it is a relative measure) while T can be Celsius or Fahrenheit.
• Serial.print (h,0) instructs the microcontroller to show the relative humidity in Serial Monitor without decimals.
• Serial.print (t,1) instructs the microcontroller to show the temperature in serial Monitor with one decimal only. The DHT11 provides humidity and temperature readings with two decimals, but the accuracy is so low that displaying temperature without decimals is most realistic. Displaying relative humidity with decimals is ridiculous. With a DHT11 the decimals are just for decoration.