Raspberry Pi: Measure Humidity and Temperature with DHT11/DHT22

With the Raspberry Pi and some sensors, it is easy to measure the temperature without much effort. In addition, however, the humidity in certain situations or projects (such as a weather station) can be enlightening. Sensors such as the DHT11 and DHT22 are not only available for a few euros, but they can also measure the temperature as well as the humidity.


As the sensors already carry (almost) everything, apart from a resistor, not many additional accessories are required. I have used this:

You can see the difference between DHT11 and DHT22 here. As a result, the DHT11 (blue) is cheaper, but the DHT22 (white) is more precise and lasts longer. For applications in “extreme” areas (0-20% or 80-100% humidity), the DHT22 should be used, as it also supports these areas in contrast to the DHT11. For example, I have a DHT22 sensor in my fridge to measure and log temperature and humidity. In general, the 11er model should also be enough.


Setup of Raspberry Pi Humidity Sensor

Raspberry Pi Humidity/Temperature Sensor ConstructionThe left pin of the sensor is connected to 3V3 of Pi (pin1), the second sensor pin via a pull-up resistor (4.7k – 10kΩ) with a free GPIO of the raspberry (GPIO4, pin7) and the right senior pin comes at GND (Pin6) from the Pi. The second pin from the right of the sensor remains free.

The structure is identical for DHT11 or DHT22 since the pins are assigned the same way.


Raspberry Pi Humidity Software Installation and Testing

First of all, some packages have to be installed:

sudo apt-get update
sudo apt-get install build-essential python-dev python-openssl git

Now the library for the sensors can be loaded. I use a pre-built Adafruit library that supports a variety of sensors:

git clone https://github.com/adafruit/Adafruit_Python_DHT.git && cd Adafruit_Python_DHT
sudo python setup.py install

This creates a Python library that we can easily integrate into our projects.

If everything went well, we can already read the temperature and humidity. The easiest way is to first use the demo files:

cd examples
sudo ./AdafruitDHT.py 11 4

The first parameter (11) indicates which sensor was used (22 for the DHT22) and the second, to which GPIO it is connected (not the pin number, but the GPIO number). This produces an output like the following:

$ sudo ./AdafruitDHT.py 11 4
Temp=24.0*  Humidity=41.0%

Attention: The sensors are only ready every two seconds. Be careful not to start a query every second.


To integrate the Raspberry Pi humidity library into other (Python) projects, you only need the following:


Log data permanently

For weather stations and similar projects for monitoring the temperature and humidity, it is useful to store these values at regular time intervals so that they can be analyzed later. One possibility would be to save in local database, but this has the disadvantage that under certain circumstances, the SD card could be charged too much.

Therefore, I have chosen a different method that stores the data on an external server. This service is free for normal use and it is very easy to save data, as shown here:

11 Responses

  1. Found the answer. The line in the tutorial “https://github.com/adafruit/Adafruit_Python_DHT.git & sudo python setup.py install” . After it ran and I got the errors. I changed Directory cd Adafruit_Python_DHT the sudo python setup.py install. Then sudo ./Adafruit.py 11 4 and it worked!

    • The example shown is using API calls to Thingspeak to publish the values. It could be a whole tutorial on its own.

  2. I set DHT11 with my Raspberry.
    Normally seems it shows correct results eg. 22*/46%, but after 10 lines goes mad eg. 7*/105%. Than again turns to normal. Is it about a bad sensor?

    • The manufacturers do not recommended that you read data from the sensor more than once per 2 seconds. If you do you may get incorrect readings.

  3. Thanks
    I have done what you have done, (Raspberry Pi 3 B), but after running
    pi@raspberrypi:~/projects/Adafruit_Python_DHT/examples $ sudo ./AdafruitDHT.py 22 4
    it prompts the following error
    Failed to get reading. Try again!

    please help.

  4. hi, i would like to work on raspberry pi ans some sensors like temp, humidity, Nitrogin and PM 10 so when ordering the pi what else i should order, do i need A/D converter and which one , also in order to send data do i need other components to carry out with my work, like what are the components required with the pi

  5. pi@raspberrypi:~/foxfiles/Adafruit_Python_DHT $ git clone https://github.com/adafruit/Adafruit_Python_DHT.git && Adafruit_Python_DHT on setup.py install
    Cloning into ‘Adafruit_Python_DHT’…
    remote: Enumerating objects: 308, done.
    remote: Total 308 (delta 0), reused 0 (delta 0), pack-reused 308
    Receiving objects: 100% (308/308), 91.74 KiB | 0 bytes/s, done.
    Resolving deltas: 100% (168/168), done.
    bash: Adafruit_Python_DHT: command not found
    pi@raspberrypi:~/foxfiles/Adafruit_Python_DHT $

    When I run the example, I get the error ImportError: No module named ‘Adafruit_DHT’. That is probably related to the bash error above. Any help would be appreciated. Thanks.


Leave a Comment

Your email address will not be published.

Subscribe to Raspberry Pi Tutorials and don't miss any new Tutorial!