In this tutorial, we’re going to cover using the the DHT11 (or DHT22) temperature and humidity sensor with the Raspberry Pi.  Using the DHT11 temperature sensor with the Raspberry Pi is a great way to get temperature and humidity readings in your projects. Especially to keep tabs on how hot your Raspberry Pi is, if its in a non-ventilated box in the summer heat.

The DHT11 vs DHT22 Sensors

The DHT11 and DHT22 are fundamentally the same from a wiring and software perspective. However, the DHT22 is a more accurate sensor. In this project we’ll be using the DHT11. If you have a DHT22, just follow along and everything will work the same and you’ll get more accurate readings!  You will need to change the code snippet to reflect your sensor though.

dht11 vs dht22

These sensor are ideal for remote mounting due to the fact that they can operate up to 65 feet (20 meters) apart using a long wire. I should also note that these sensors come as separate components or as modules. We’re going to use a module. The difference is that the module includes the 10K Ohm pull up resistor on the board. If you decide to use the standalone sensor, we have a wiring diagram for that below as well.

DHT11 DHT22 Maximum distance 20 meters

Parts List for this Project

Here’s a quick parts list for this project to get you started:

The DHT11/22 Raspberry Pi Tutorial Video

Wiring the DHT11 or DHT22 to the Raspberry Pi

Before we can begin using the DHT11 Temperature Sensor with the Raspberry Pi, we have to wire it to the Pi.

RELATED: Installing Raspbian for the First Time

Using a Breadboard and Breakout Cable

You don’t need a breadboard for this project, but I feel like it makes things much simpler to understand, especially if you use of these breakout cables. It makes the Pi’s pinout much easier to follow. There’s a link to these in the description.

DHT11 / DHT22 Wiring Diagrams for the Raspberry Pi (Module)

Use the following wiring diagram if you have a DHT11 or DHT22 module with an integtated pull up resistor.

  • DHT11/22 Sensor Vcc+ to Raspberry Pi 5V
  • DHT11/22 Sensor GND to Raspberry Pi GND
  • DHT11/22 Sensor Signal to Raspberry Pi PIN 7 (GPIO PIN 4)

DHT11 / DHT22 Wiring Diagrams for the Raspberry Pi (Sensor Only)

Use the following wiring diagram if you have a DHT11 or DHT22 sensor with and need to add a separate pull up resistor.

  • DHT11/22 Sensor Vcc+ to Raspberry Pi 5V
  • DHT11/22 Sensor GND to Raspberry Pi GND
  • DHT11/22 Sensor Signal to Raspberry Pi PIN 7 (GPIO PIN 4)
  • 10K Ohm Resistor between DHT11/22 PIN 1 and PIN 2

If you wired everything up correctly, you’ll see a red LED glowing on the DHT11 module.  (NOTE: Some cost reduced modules do not include this LED.)

RELATED: DHT11 Sensor Wiki Page

Python Code for Raspberry Pi DHT11/DHT22

Next thing we need to do is install the DHT python library. This is done by entering  the following command:

sudo pip3 install Adafruit_DHT

Note: If you run into problems with the above command, you may not have PIP installed on your Pi.  You can fix that by running the following commands.  These will install PIP and other utilities you may need.

sudo apt-get install python3-dev python3-pip
sudo python3 -m pip install --upgrade pip setuptools wheel

OK. Now let’s take a look at the code we’re going to use. This is some very basic code written in Python. The first section of code imports the DHT library from Adafruit and the system time library.

import Adafruit_DHT
import time

This line defines the sensor object we will use, and the next line is a variable that defines the GPIO pin we are using.

DHT_SENSOR = Adafruit_DHT.DHT11
DHT_PIN = 4

And finally the code loop that goes next.  The “while True:” line will force everything indented after to run in an infinite loop.

First, we capture the temperature and humidity to two aptly named variables,  and then use an IF statement to check to see if it worked. If it worked, we print the temperature and humidity to the screen. If it it failed we let you know to check your wiring.

Lastly, since the DHT11 and DHT22 can only be checked a maximum of once per second, we use the system time.sleep function two pause three seconds between checks.

while True:
    humidity, temperature = Adafruit_DHT.read(DHT_SENSOR, DHT_PIN)
    if humidity is not None and temperature is not None:
        print("Temp={0:0.1f}C  Humidity={1:0.1f}%".format(temperature, humidity))
    else:
        print("Sensor failure. Check wiring.");
    time.sleep(3);

Python Code for DHT11/22 Complete

And here is the completed python code for the DHT11/22 sensors.


import Adafruit_DHT
import time

DHT_SENSOR = Adafruit_DHT.DHT11
DHT_PIN = 4

while True:
    humidity, temperature = Adafruit_DHT.read(DHT_SENSOR, DHT_PIN)
    if humidity is not None and temperature is not None:
        print("Temp={0:0.1f}C Humidity={1:0.1f}%".format(temperature, humidity))
    else:
        print("Sensor failure. Check wiring.");
    time.sleep(3);

To run your code, enter python3 mydht11.py and press enter. If you did everything correctly, you’ll start seeing the temperature and humidity reported on the terminal window every 3 seconds.

The Completed DHT11/DHT22 Project

There are so many possible uses for these sensors. You can use them to capture the temperature of your Pi when its in an enclosure in the hot summer heat, or you can use it to check on the temperature of room in your house when you’re out of town. It’s also very commonly used in creating weather stations.

If you have any problems, leave a comment below and we’ll do our best to help you out!