Starting Electronics needs your help! Please make a donation to help cover our hosting and other costs. Click the donate button to send a donation of any amount.

Arduino DHT11 Sensor Tutorial

Created on: 28 January 2022

Arduino DHT11 sensor tutorial that shows how to measure temperature and humidity with an Arduino Uno and Arduino MEGA 2560. The DHT11 four pin sensor measures air temperature and relative humidity. This tutorial shows how to connect a DHT11 temperature and humidity sensor to an Arduino Uno and MEGA 2560. Build a breadboard circuit using the DHT11 sensor and connect it to an Arduino. After that, install a DHT11 library in the Arduino IDE. Finally use the library in a sketch to read and display temperature and humidity. Display temperature and humidity in the serial monitor window of the Arduino IDE.

Part 12 of the Arduino Tutorial for Beginners

DHT11 Temperature and Humidity Sensor

The following image shows the DHT11 sensor pin numbering or pinout. As can be seen in the image, the sensor has a blue plastic case with square holes. Because of the holes, air passes onto the sensor surface. This allows the sensor to measure the current air temperature and relative humidity.

DHT11 Temperature and Humidity Sensor Pinout
DHT11 Temperature and Humidity Sensor Pinout

Power the DHT11 sensor with 5V from the Arduino, as explained in the next section. This sensor clocks temperature and humidity data out of the DATA pin. That is, it sends serial digital data out on the pin, rather than outputting an analog value. This is different from the MCP9700 temperature sensor that outputs an analog voltage that represents temperature. In order to use the DHT11 sensor in an Arduino sketch, a DHT11 library is loaded in the Arduino IDE. The section after the circuit diagram that follows explains how to install a DHT11 sensor library.

Arduino DHT11 Sensor Tutorial Circuit Diagram

The following circuit diagram shows how to connect a DHT11 sensor to an Arduino Uno. Use the same connections on an Arduino MEGA 2560. That is, connect the DHT11 VDD or VCC pin to Arduino 5V. After that, connect the DHT11 GND pin to Arduino GND. Finally connect the DATA or IO pin of the DHT11 to an Arduino digital pin. Use Arduino digital pin 2 in this tutorial, as show in the circuit diagram.

In the circuit diagram, capacitor C1 is an optional capacitor. It is intended to be placed close to the sensor power pins to help stabilize the supply voltage. Resistor R1 is a 4k7 pull-up resistor. In fact a value of between 4k7 and 10k works fine for R1. Breadboard layout circuits follow in two subsections for the Uno and MEGA 2560.

Arduino Uno DHT11 Temperature Humidity Sensor Circuit Diagram
Arduino Uno DHT11 Temperature Humidity Sensor Circuit Diagram

Some tutorials connect the DATA pin of the DHT11 sensor to an Arduino analog pin such as A0. This only works because analog pins can actually be used as digital pins. That is because they are multi-function pins. Refer to the Ultimate Arduino Uno Hardware Manual for the Arduino Uno to find out how analog pins are configured as digital input/output pins, as well as analog input pins. Find the same information in the Ultimate Arduino MEGA 2560 Hardware Manual for the Arduino MEGA 2560.

DHT11 Sensor Pin Names

Notice that the DHT11 symbol pin labels in the circuit diagram differ from the labels on the sensor at the right of the circuit. On the sensor, pin 1 is VDD, pin 2 is DATA, and pin 4 is GND. These names are from the translated DHT11 datasheet. The n.c. pin is a not connected pin that does nothing. In contrast, the symbol for the DHT11 sensor has the following different labels. Firstly pin 1 which is VDD is labeled VCC instead. Secondly pin 2 which is DATA is labeled IO on the symbol. Just be aware of these different labels, and know that they are alternate names for the same pins.

4k7 Resistor

So far in this Arduino tutorial for beginners, part 6 shows how to read the value of a 470 ohm resistor. Part 8 shows how to read the value of a 10k resistor. Below is an explanation of how to read the value of a 4k7 resistor.

A 4k7 resistor is a 4700 ohm resistor. Similarly it can be called a 4.7k resistor. When written as 4k7, the k separates the thousands units from the hundred units of the value. In other words, it separates 4, which is 4000 ohms from 7, which is 700 ohms.

A 5% tolerance 4k7 resistor has the color bands yellow, violet and red. After that is the gold 5% tolerance band. Yellow has the value 4, violet 7 and red 2. Place the values from the first two color bands to together. That is 4 and 7 which gives 47. Finally place the number of zeros represented by the third color band after the first two digits. That is 2 in this example, which is two zeros. Finally we have 4700, which is written 4k7 for short. Refer back to resistors for beginners in electronics if needed. That article has the resistor color chart at the bottom of the page.

Arduino Uno DHT11 Sensor Breadboard Circuit

Build the circuit from above on a breadboard and connect it to an Arduino Uno as shown below. Optional capacitor C1 shown in the circuit diagram is not placed in the breadboard circuit. If you have an Arduino MEGA 2560, then build the breadboard circuit shown in the next subsection.

How to Connect a DHT11 Sensor to an Arduino Uno
How to Connect a DHT11 Sensor to an Arduino Uno

Arduino MEGA 2560 DHT11 Sensor Breadboard Circuit

Build the following DHT11 breadboard circuit if you have an Arduino MEGA 2560. Optional capacitor C1 shown in the circuit diagram is not used in the breadboard circuit. Feel free to take 5V and GND from the connector at the end of the Arduino MEGA 2560 if desired. The following breadboard layout makes the connections clear. It avoids crossing wires. Build the circuit how you like, so long as it is electrically the same as the circuit diagram.

How to Connect a DHT11 Sensor to an Arduino MEGA 2560
How to Connect a DHT11 Sensor to an Arduino MEGA 2560

Arduino DHT11 Sensor Tutorial Sketch Code

Currently there is no default DHT11 library in the Arduino IDE. Install a library in the Arduino IDE in the next section. After that, load a sketch to the Arduino that gets the temperature and humidity from the DHT11.

Install a DHT11 Library in the Arduino IDE

Connect the computer that is running the Arduino IDE to the internet. That is because the Arduino IDE retrieves libraries from the internet.

Open the Arduino IDE. After that, select Tools → Manage Libraries… from the top IDE menu bar. As a result, the Library Manager dialog box opens. Type DHT11 in the top right search box of the Library Manager dialog box. This filters available libraries that apply to the DHT11 sensor as shown in the following image. Hover the mouse cursor over the DHT sensor library, as the below image shows. As a result an Install button appears. Click the Install button to install the DHT sensor library.

Arduino IDE Library Manager Dialog Box with DHT11 Filtered in the Search Field
Arduino IDE Library Manager Dialog Box with DHT11 Filtered in the Search Field

A dependencies dialog box opens after clicking the Install button in the Library Manager. This is because the DHT sensor library relies on another library in order to work. Click the Install all button in the dependencies dialog box to install the extra library. A red dot highlights the button in the following image.

Dependencies Dialog Box that Appears when Installing the DHT Sensor Library
Dependencies Dialog Box that Appears when Installing the DHT Sensor Library

Click the Close button at the bottom right of the Library Manager dialog box after the library installation has finished. Run sketch code in the next section that gets the temperature and humidity from the DHT11 sensor.

DHT11 Sensor Arduino Sketch Code

An example sketch called DHTtester was installed with the DHT sensor library. Open this example sketch as follows. Select File → Examples → DHT sensor library → DHTtester from the top Arduino IDE menu bar. The DHT sensor library item is far down the Examples menu. With the mouse cursor on the menu that pops out from Examples, scroll down the menu. Eventually a heading appears called Examples from Custom Libraries. Find the DHT sensor library menu item under this heading. Finally, click the DHTtester item on this menu.

Modify the Arduino DHT11 Sketch Code

In the DHTtester sketch, find the following lines of code near the top of the sketch.

Currently selected sensor in code

Comment out the DHT22 item and uncomment the DHT11 item. Afterwards, the code looks as follows. This selects the DHT11 sensor to read from in the sketch.

Modified DHT11 Arduino sketch code

Save the sketch. As in a previous part of this tutorial series, the example sketch is write only. Save it to your Arduino sketch folder. For example save it as DHTtester_DHT11. Finally upload the sketch to the target Arduino board with the DHT11 circuit connected.

Test the Arduino DHT11 Sketch

Open the Arduino IDE serial monitor window. Make sure that the baud rate at the bottom of the serial monitor window is set to 9600 baud. The serial monitor window displays the current humidity and temperature. New humidity and temperature values appear in serial monitor window approximately every two seconds. The following image shows the humidity and temperature readings in the serial monitor window.

Arduino IDE Serial Monitor Window Showing Temperature and Humidity Readings from the DHT11 Sensor
Arduino IDE Serial Monitor Window Showing Temperature and Humidity Readings from the DHT11 Sensor

Breath on the sensor and the humidity reading goes up, assuming the relative humidity is not already very high. Touch the sensor with your fingers and the humidity and temperature readings go up. Response time of the DHT11 sensor is fairly slow. It takes a few seconds for readings to stabilize.

Books that may interest you:

C Programming with Arduino Book Ultimate Arduino MEGA 2560 Hardware Manual Ultimage Arduino Uno Hardware Manual