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.

Accessories

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:

3 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!

    Reply

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

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