Popup

Wait! Don’t Go Yet! 👋

Become a Member Today and Unlock Access to All eBooks! 😍

Thousands of eBooks at your fingertips. Read, learn, and grow anytime, anywhere ✨

Raspberry Pi: DHT11/DHT22 Temperature and Humidity (Python)

Learn how to interface the DHT11 and DHT22 digital temperature sensors with the Raspberry Pi to get temperature and humidity readings. You’ll learn how to connect the sensors to the Raspberry Pi, include the required libraries, and write a Python script that displays the current temperature and humidity on the shell/terminal.

Prerequisites

Before proceeding with this tutorial, please verify the following prerequisites.

  1. Get familiar with the Raspberry Pi board—if you’re not familiar with the Raspberry Pi, you can read our Raspberry Pi Getting Started Guide here.
  2. You must know how to run and create Python files on your Raspberry Pi. We like to program our Raspberry Pi via SSH using an extension on VS Code. We have a detailed tutorial about that subject: Programming Raspberry Pi Remotely using VS Code (Remote-SSH).
  3. Know how to use the Raspberry Pi GPIOs so that you know how to wire the circuit properly. Read the following tutorial: Raspberry Pi Pinout Guide: How to use the Raspberry Pi GPIOs?

Introducing DHT11/DHT22 Sensors

The DHT11 and DHT22 sensors are used to measure temperature and relative humidity. These are very popular among makers and electronics hobbyists.

DHT11/DHT22 Temperature and Humidity Sensors

These sensors contain a chip that does analog to digital conversion and spit out a digital signal with the temperature and humidity. This makes them very easy to use with any microcontroller.

DHT11 vs DHT22

Top 6

Raspberry Pi eBooks

From Zero to Professional

Raspberry Pi Projects

The DHT11 and DHT22 are very similar but differ in their specifications. The following table compares some of the most important specifications of the DHT11 and DHT22 temperature and humidity sensors. For a more in-depth analysis of these sensors, please check the sensors’ datasheet.

DHT11DHT22
dht11 bbdht22 bb
Temperature range0 to 50 ºC +/-2 ºC-40 to 80 ºC +/-0.5ºC
Humidity range20 to 90% +/-5%0 to 100% +/-2%
ResolutionHumidity: 1%
Temperature: 1ºC
Humidity: 0.1%
Temperature: 0.1ºC
Operating voltage3 – 5.5 V DC3 – 6 V DC
Current supply0.5 – 2.5 mA1 – 1.5 mA
Sampling period1 second2 seconds
Price$1 to $5$4 to $10
Where to buyCheck pricesCheck prices

The DHT22 sensor has a better resolution and a wider temperature and humidity measurement range. However, it is a bit more expensive, and you can only request readings with 2 seconds interval.

The DHT11 has a smaller range, and it’s less accurate. However, you can request sensor readings every second. It’s also a bit cheaper.

Despite their differences, they work similarly, and you can use the same code to read temperature and humidity. You just need to select in the code the sensor type you’re using.

DHT Pinout

DHT sensors have four pins as shown in the following figure. However, if you get your DHT sensor in a breakout board, it comes with only three pins and with an internal pull-up resistor on pin 2.

DHT22 Temperature and Humidity Sensor using Arduino IDE

The following table shows the DHT22 and DHT11 pinout. When the sensor is facing you, pin numbering starts at 1 from left to right

DHT pinConnect to
13.3V
2Any digital GPIO; also connect a 4.7k Ohm pull-up resistor
3Don’t connect
4GND

DHT Sensor Breakout Board

If you have a DHT11 or DHT22 sensor on a breakout board, it only comes with three pins and has an internal pull-up resistor on the data pin.

DHT22 sensor

In this case, wiring is even simpler, and you don’t need to wire an external resistor. The DHT breakout boards usually have labels on the pins: GND, VCC, and DATA.

DHT pinConnect to
GNDGND
VCC3V3 (OUT)
DATGPIO 4 (or any other digital pin)

Parts Required

Raspberry Pi with DHT22 Circuit

Here’s a list of parts you need to build the circuit (if you don’t have a DHT breakout board, you need a 4.7kOhm resistor):

Wiring the DHT11/DHT22 Sensor to the Raspberry Pi

Wire the DHT22 or DHT11 sensor to the Raspberry Pi as shown in the following schematic diagram. If you have a DHT breakout board, ignore the resistor.

Raspberry Pi DHT Sensor Wiring

In this example, we’re connecting the DHT data pin to GPIO 4. However, you can use any other suitable digital GPIO. Learn more about the Raspberry Pi GPIOs here.

Getting DHT11/22 Temperature and Humidity

There are different ways to get temperature and humidity readings from the DHT11 or DHT22 sensors using the Raspberry Pi with Python. We’ll use the Adafruit_CircuitPython_DHT Python library.

Update your Raspberry Pi

First, update and upgrade your Raspberry Pi, if any updates are available. Run the following command:

sudo apt update && sudo apt upgrade

If there’s a need to update, it will ask you if you want to continue. Click Y and Enter to proceed. You may need to wait a few minutes if it needs to update.

Create a Virtual Environment

We’ll install the DHT library in a virtual environment. Creating a virtual environment will isolate the Python libraries we’re using, in this case, the DHT library, from the rest of the system.

We’ll create our virtual environment in a directory on our Desktop. Enter the following command in a Terminal window to move to the Desktop:

cd ~/Desktop

Create a folder for your project. This is where we’ll create the virtual environment and install the library. We’ll create a folder called dht_test.

mkdir dht_test

Move to the newly created folder:

cd ~/Desktop/dht_test

Create a virtual environment for this directory called myenv. This must be the same directory where we’ll install the DHT library. Replace myenv with the desired name for your virtual environment.

python3 -m venv myenv

Then, you can run the following command to check that the virtual environment is there.

ls -l
Create a Virtual Environment Raspberry Pi Python

Activate the virtual environment:

source myenv/bin/activate

Your prompt should change to indicate that you are now in the virtual environment.

Activate Python Virtual Environment

Installing the Adafruit_CircuitPython_DHT Library

Now that we are in our virtual environment, we can install the library. Run the following command:

python3 -m pip install adafruit-circuitpython-dht

After a few seconds, the library will be installed (ignore any yellow warnings about deprecated packages).

Installing the Adafruit_CircuitPython_DHT Library

Python Code for Raspberry Pi DHT11/DHT22

The following script gets temperature and humidity from the DHT sensors and prints the readings on the Python shell. Create a new Python file with a name of your choice, for example, dht_basic.py, and copy the following code.

# Complete Project Details: https://ebokify.com/raspberry-pi-dht11-dht22-python/
# Based on Adafruit_CircuitPython_DHT Library Example

import time
import board
import adafruit_dht

# Sensor data pin is connected to GPIO 4
sensor = adafruit_dht.DHT22(board.D4)
# Uncomment for DHT11
#sensor = adafruit_dht.DHT11(board.D4)

while True:
    try:
        # Print the values to the serial port
        temperature_c = sensor.temperature
        temperature_f = temperature_c * (9 / 5) + 32
        humidity = sensor.humidity
        print("Temp={0:0.1f}ºC, Temp={1:0.1f}ºF, Humidity={2:0.1f}%".format(temperature_c, temperature_f, humidity))

    except RuntimeError as error:
        # Errors happen fairly often, DHT's are hard to read, just keep going
        print(error.args[0])
        time.sleep(2.0)
        continue
    except Exception as error:
        sensor.exit()
        raise error

    time.sleep(3.0)

To create and run Python scripts on the Raspberry Pi, we like to use VS Code with an SSH extension: Programming Raspberry Pi Remotely using VS Code (Remote-SSH).

How the Code Works

Continue reading to learn how the code works, or skip to the Demonstration section.

Importing libraries

Start by importing the required libraries: the time module to add delays to our code, the board module to interact with the Raspberry Pi GPIOs, and the adafruit_dht to read from the DHT sensor.

import time
import board
import adafruit_dht

DHT Sensor

On the following line, we initialize our DHT sensor. We are using a DHT22 connected to GPIO 4 (you can use any other suitable GPIO as long as you change the code on the following line):

sensor = adafruit_dht.DHT22(board.D4)

If you’re using a DHT11, it should be like this:

sensor = adafruit_dht.DHT11(board.D4)

From now on, we’ll refer to our DHT sensor as the sensor in our code.

While loop

Then, we have a loop that keeps the program running indefinitely, until you stop the program. Inside the loop, we’ll try to read the sensor and display the readings.

while True:
    try:

Reading Temperature and Humidity

Getting the temperature in Celsius degrees and humidity is as easy as using the temperature and humidity properties of the sensor object. So, you can get the temperature like this:

temperature_c = sensor.temperature

By default, it will get the temperature in Celsius degrees. We add a line to convert it to Fahrenheit degrees.

temperature_f = temperature_c * (9 / 5) + 32

We save the humidity value on the humidity variable:

humidity = sensor.humidity

After getting sensor readings, we’ll display them on the shell using the print() function.

print("Temp={0:0.1f}ºC, Temp={1:0.1f}ºF, Humidity={2:0.1f}%".format(temperature_c, temperature_f, humidity))

If we can’t get valid temperature and humidity readings, we handle the exceptions in the except blocks:

except RuntimeError as error:
    # Errors happen fairly often, DHT's are hard to read, just keep going
    print(error.args[0])
    time.sleep(2.0)
    continue
except Exception as error:
    sensor.exit()
    raise error

We get new temperature readings every three seconds. You can adjust the delay time by changing the argument of the sleep() method.

time.sleep(3)

Demonstration

Save your Python file in the same folder as your virtual environment (in our case Desktop/dht_test). Then run it on your Raspberry Pi. Run the following command (make sure you are on the correct path—the same folder as the virtual environment):

python dht-basic.py

The virtual environment must be active to run the script. If the virtual environment is not active, you can rerun the following command to activate myenv.

source myenv/bin/activate

You should get new temperature and humidity readings on the Python Shell or on the Raspberry Pi Terminal every three seconds.

Raspberry Pi Print Temperature and Humidity DHT22 DHT11 Sensor

You can stop the execution of the program by pressing CTRL+C.

You may also like reading: Raspberry Pi: BME280 Temperature, Humidity and Pressure Sensor (Python).

Wrapping Up

In this tutorial, you learned how to interface the DHT11 and DHT22 digital temperature and humidity sensors with the Raspberry Pi and how to write a Python program to get and display readings. This is one of the most basic projects to get you started with the DHT11 or DHT22 sensor.

We hope you found this tutorial useful. If you’re a beginner to the Raspberry Pi, you can get started with the following tutorials:

You can check all our Raspberry Pi projects on the following link:

Share your love

🚀 Discover the world of electronics and innovation!

✨ Create, program, and experiment with all your creative ideas with ease.

Spotpear

Leave a Reply

Your email address will not be published. Required fields are marked *

Secure Payments
Securing online payments is a shared responsibility, and everyone can contribute.
Free Shipping
You get unlimited free shipping on eligible items with Ebokify, with no minimum spend.
24/7 Support
Sales gifts are helpful tools often used to show appreciation to clients for their purchase.
Gifts & Sales
Our customer care service is offered in the form of 1st or 2nd level support.