Raspberry Pi DHT22 Temperature & Humidity Sensor Project

In today’s project, a temperature and humidity detector will be constructed using the DHT22/AM2302 temperature and humidity sensor. Using some simple Python code and a Raspberry Pi, the DHT22 will allow real-time temperature and humidity readings to be read to the Pi, with reasonable accuracy. The Raspberry Pi DHT22 sensor makes for an affordable, easy-to-use and reliable sensor due to its simple 4-pin wiring and greater accuracy than its brother, the DHT11 sensor. From its temperature range to accuracy, the DHT22 simply outperforms the DHT11 in almost every single aspect so including this sensor in a weather station is highly recommended! Due to the Raspberry Pi having GPIO pins, which only accept digital input, this makes the DHT22 a suitable sensor to use since it has a built-in analogue-to-digital converter (ADC). This essentially means that the information from the sensor can be directly fed into the Raspberry Pi GPIO pins without the need for an external ADC chip in between. However, for a basic temperature and humidity sensor, there definitely are other sensor modules and variants on the market that could be substituted for the DHT22. Once again, any Python IDE installed on your Raspberry Pi should be compatible for this project but in this example, Thonny will be used. Alternatively, other IDEs such as mu IDE or even Nano that is built-in with the Raspberry Pi Terminal will work just fine. 

Raspberry Pi editor interface

Components Required for DHT22 Project

For this project, the components required are quite minimal and aside from the DHT22 itself, you should be able to find all these components in your electronics workshop:

  • Raspberry Pi (any model should work)
  • Breadboard
  • Male-Female Jumper Wires (3)
  • DHT22 Temperature & Humidity Sensor
  • 10KΩ resistor

DHT22 Raspberry Pi Wiring

In the DHT22 Raspberry Pi wiring diagram below, you may notice that a 10KΩ pull-up resistor is added between the VCC and data pin of the DHT22 temperature and humidity sensor. This resistor is needed to sustain a HIGH signal to the microcontroller (Raspberry Pi) and maintain constant communication between the sensor and the Pi. Although the DHT22 sensor has a 4-pin configuration, it is to note that the third pin from the left is not connected to anything. If you are using the DHT22 sensor module, it will only have three pins and a pull-up resistor is not required, enabling you to directly connect the sensor module to the Raspberry Pi.

If you are brand new to the Raspberry Pi, connecting wires to the Pi may be a bit daunting at first due to none of the 40 pins having any labels to indicate what pin it may be. This means that it is recommended that you always follow a pinout guide to identify pins and to additionally double-check your wiring before powering up the Pi to avoid any shorts.

Other than that, the wiring for this project is fairly simple as aside from the main sensor, not a lot of extra components are needed. 

  • Connect VCC (Pin 1) to 5v (Pin 04) on your Raspberry Pi,
  • the Data pin (Pin 2) to GPIO04 (Pin 07)
  • and GND (Pin 4) to GND (Pin 06) on your Raspberry Pi
  • Connect a 10KΩ resistor between the VCC (Pin 1) and Data pin (Pin 2) on your DHT22 sensor. 

connect dht22 to raspberry pi

Once you’ve completed the wiring following the DHT22 Raspberry Pi hookup circuit diagram, you’re ready to get started with running the code and getting this project up and running!

Raspberry Pi DHT22 Python Code

Before you start uploading the code on your Python IDE, there are a few libraries and software that you will need to download and install to your Raspberry Pi, if you have not done so before. For this project to work, you will first need to ensure that you have the latest version of Python 3 installed on your Pi and since we are using a DHT22 sensor, the Adafruit DHT library will be required to install. The following commands should be run one at a time via your Raspberry Pi Terminal:

					sudo apt-get install python3-dev python3-pip
sudo pip3 install Adafruit_DHT
dht22 python raspberry pi

Now, in a Python IDE, you can upload the following project code:

					import Adafruit_DHT
while True:
    humidity, temperature = Adafruit_DHT.read_retry(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))
        print("Sensor failure")

About Raspberry Pi DHT22 Sensor Code

The first line of the code is to import the Adafruit DHT library into our code in order for the DHT22 sensor to work properly. This is the same library that was needed to be installed earlier. Without this library, several of the commands used in this project code will not be able to work with the sensor.

In the next two lines, the variable DHT_SENSOR is set up and declared to work with our sensor, the DHT22 sensor. Furthermore, the GPIO pin (GPIO04) that the sensor is connected to is declared under variable DHT_PIN.

The next block involves a giant while statement, which essentially means that the next few lines will only run when the code is uploaded and the program is working. Two important data variables are now created, humidity and temperature, to work with the DHT22 sensor using another crucial function, Adafruit_DHT.read_retry. This function consists of two arguments, which are the two variables declared earlier, in order to match up our sensor and its respective GPIO pin. This function allows us to read data directly from the sensor and together with the humidity & temperature variables, allows us to store that data within those variables. 

With the if-else statement next, the code essentially states that if humidity and temperature data is being received by the Pi, the readings will be printed using the print() function. However, if no readings are coming in, a failure message is printed to the IDE. This code allows us to print temperature data in degrees Celsius and humidity in percentage.

Next steps

Due to how easy-to-use, accurate and affordable the DHT22 sensor is, it makes an excellent addition to any project that involves temperature and humidity readings. Plus, it can easily be connected to a Raspberry Pi GPIO pin and with the Adafruit DHT library, can easily be programmed via Python. Moving forward, a potential next step could be transferring the temperature and humidity readings to be displayed on a display such as an LCD (Liquid Crystal Display) or OLED (Omitting Light Emitting Diode) screen. Even further, one could possibly think of uploading real-time data from the sensor to a web server, so it could be accessed remotely. With the Raspberry Pi’s capabilities, the possibilities to expand upon this project are endless and building a weather station can easily be done.

Scroll to Top