Raspberry Pi / DHT11 - Measure Humidity and Temperature

Published

Introduction: Raspberry Pi / DHT11 - Measure Humidity and Temperature

I wanted to measure temperature and humidity using my Raspberry Pi. I chose a DHT11 sensor because it is robust and cheap. Configuring it is also well documented but there are a number of pitfalls on the way which I would like to focus on.

The DHT11 has 4 pins. The left most is for Vcc or positive (+) pin which is connected to the Raspberry Pi 3.3V pin. The next pin across is the data pin which must be connected to a GPIO pin on the Raspberry Pi. These two pins must be connected using a 4.7K resistor.

The 3rd pin from the left is not used. The rightmost and fourth pin is the ground or negative pin which must be connected to one of the ground pins on the Raspberry Pi.

Step 1: Components Required

1. Raspberry Pi

2. DHT11

3. 4.7k resistor

4. Various breadboard wires

5. Breadboard

Step 2: Connecting Things

Connect the Raspberry Pi and DHT11 as follows:

DHT11 (+ pin) <-> RaspberryPi (3.3V pin)

DHT11 (data pin) <-> RaspberryPi (GPIO pin - I used GPIO22)

DHT11 (3rd pin) No connection

DHT11 (- pin) ------ Raspberry Pi (gnd pin)

Step 3: Installing the Software

From the RaspberryPi command line, do the following: (NOTE, do not omit SUDO)

sudo git clone https://github.com/adafruit/Adafruit_Python_DHT.g...

You should see - Cloning into 'Adafruit_Python_DHT'... remote: Counting objects: 249, done. remote: Total 249 (delta 0), reused 0 (delta 0), pack-reused 249 Receiving objects: 100% (249/249), 77.01 KiB, done. Resolving deltas: 100% (142/142), done.


cd Adafruit_Python_DHT/

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

ls

You should see - Adafruit_DHT examples ez_setup.py LICENSE README.md setup.py source

cd Adafruit_DHT/

You should see -Beaglebone_Black.py common.py __init__.py platform_detect.py Raspberry_Pi_2.py Raspberry_Pi.py Test.py

sudo python setup.py install

(Note, if you bypass this step then you may see an encounter error - Traceback (most recent call last):

File "./AdafruitDHT.py", line 24, in import Adafruit_DHT ImportError: No module named Adafruit_DHT)

cd examples

sudo ./AdafruitDHT.py 11 22 (11 = DHT11 and 22=GPIO22 which you selected earlier)

You should see Temp=18.0* Humidity=46.0% (i.e. the temperature and humidity for your environment)

Step 4: What to To With the Output

So, as we saw, the output is "Temp=18.0* Humidity=46.0%"

There are a number of ways in which you can harness this output. Here is a test_dht11.php file (please rename it test_dht11.php) I wrote to show how it might be done. Firstly, I relocated the script AdafruitDHT.py to /var/www/.

To test and run the php script, change to /var/www then sudo php test_dht11.php

The output shows two numbers representing temperature and humidity. These numbers can be written to a database, or compared to warning limits and send an alert etc.

Share

    Recommendations

    • Water Contest

      Water Contest
    • Creative Misuse Contest

      Creative Misuse Contest
    • Oil Contest

      Oil Contest

    5 Discussions

    0
    user
    BobL90

    7 months ago

    Hey there, this is great.

    I got the expected output when I run the AdafruitDHT.py file.

    Only problem is, when i copy it to the var/www/html directory, and use the php file you wrote, I get the output

    "rust you" ....?

    I have no idea where that's coming from haha. I can add lines into the php file to make it display text as expected, but.... yeah. I don't know. Also, is there a reason you have an extra

    "

    <?php

    $test="";

    "

    As the first two lines in the php file? it wasn't working for me when I left that in.

    4 replies

    Hi, the $test = ""; line is to initialise the variable for use later. In terms of running the python file, please ensure that you have placed it in the directory referred to in the php script, in my case /var/www/. You can also check that the php file has the correct permissions (755 on Linux) and you can trying running the php file directly from the command line, i.e. sudo php/test_dht11.php - hope that works

    Awesome, thanks for the tutorial. I got it working :)

    Yeah, I can run it from the command line and get the correct settings, but when I run it from the browser I get that long message about sudo

    ok, I outputed the full $test variable and the entire message is:
    "We trust you have received the usual lecture from the local System Administrator. It usually boils down to these three things: #1) Respect the privacy of others. #2) Think before you type. #3) With great power comes great responsibility. sudo: no tty present and no askpass program specified"