Skip to content
FREE CA & US Shipping for CAD$89+ Orders | FREE CA Shipping on Applicable Products

Country/region

Cart

Tutorial: Getting started with the DHT11 temperature and humidity sensor

Introduction

DH11 Temperature and Humidity Sensor

In this tutorial we will be looking at how to get started with the popular DHT11 temperature and humidity sensor. This is an extremely easy to use module that only requires three connections. Namely the VIN and GND, and a single digital output. Setting this module up couldn’t be easier, so let’s right into it.

Required hardware

Setting up the hardware

We chose to interface the DHT11 temperature and humidity sensor with the Nano microcontroller (because it is one of our favorite microcontrollers!). The Nano is easy to use, powerful and has a small footprint. To make this project more elegant, we will not be using a breadboard, as is common practice. Instead we will use the NTEA-LG, which is a breakout board designed specifically for the Nano. This particular breakout board is the largest of the three types in this series. Which is useful as we will be using the male header breakouts, which only exist on the LG version. That being said, feel free to use a standard breadboard and a few female-to-male dupont wires.

Note that we didn’t list any dupont wires, as the DHT11 temperature and humidity sensor already comes with the required wires.

Finally, the connection couldn’t be simpler. Pop in the Nano into the NTEA-LG and connect the following wires:

  • VCC pin (labelled “+”) on the sensor to the 3.3V pin on the Nano.
  • GND pin (labelled “-“) on the sensor to GND pin on the Nano.
  • Digital pin (labelled “out”) on the sensor to D2 on the Nano.

Note that we are using the male header breakout on the NTEA and not the screw terminal breakout. However, as mentioned above you can substitute this breakout with a breadboard and make the same connections. … If you want a very quick-n-dirty way, simply plug the female dupont wires directly into the pins of the Nano (no breadboard)!

Don’t forget to plug in the Mini-B USB into the Nano and your computer!

You might be wondering why we are using the 3.3V pin to power the sensor. The sensor accepts both 3.3V as well as 5V, so why not use 5V? From our experimentation we found that the current draw vs. VCC voltage is as follows:

VCC [V] Current draw [mA]
3.3 1.8
5.0 3.2
Table 1: Current consumption with different input voltages.

We can see that the current draw is lower using the 3.3V pin. If applicable, one should always keep the power budget in mind. Now in this case it doesn’t really matter as using either voltage results in minor differences, both of which are far away from the current rating of the Nano. But we still consider this to keep up good habits 🙂

Setting up the software

It is assumed that the user has already installed the IDE software and is familiar how to select the correct settings in order to upload scripts to the Nano. If the user is not familiar with these steps, see our tutorial on first time using the Nano microcontroller.

We are not going to go into the details of the script, what each lines means, etc. For this getting started tutorial we are interested simply in getting the sensor to output some data. After that, the user can expand on the script and make more complicated projects!

Simply copy the following script into the IDE software.

// Project title: "Tutorial: Getting started with the DHT11 temperature and humidity sensor"
// Date of last edit: Dec 5 2021, PTSolns
// For bugs or questions please send us an email at contact@PTSolns.com
// Overview: This is the script for the DHT11 sensor. Set up the hardware as outlined in the tutorial.
//
// Requries the following Arduino libraries:
// - DHT Sensor Library. When you install this one, library manager pop-up window should ask you if you want to also 
//   install the library below.
// - Adafruit Unified Sensor Lib.

 
#include "DHT.h"

 
#define DHTPIN 2        // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11   // DHT 11

 
DHT dht(DHTPIN, DHTTYPE);

 
void setup() {
  Serial.begin(9600);

 
  dht.begin();
}

 
void loop() {
  // Reading humidity and temperature (in C)
  float h = dht.readHumidity();
  float t = dht.readTemperature();

 
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }

  
  Serial.print(F(" Humidity: "));
  Serial.print(h);
  Serial.print(F("%  Temperature: "));
  Serial.print(t);
  Serial.println(F("C "));
  
  // Wait a few seconds between measurements.
  delay(2000);
}

Results and conclusion

Upon uploading the script and opening the serial monitor, the user should see something similar to the following.

Seems to be working well! You can see on the 6th line on the output the humidity increased sharply. That’s when I breathed onto the sensor while cupping it in my hands. Temperature accordingly rose as well. So there is that! Of course this is just a basic getting started tutorial, but there is more that can be done with this sensor. Enjoy!