Raspberry Pi / DHT11 - Measure Humidity and Temperature

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


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 outpu, for example using this test php file, test_dht11.php file

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.

//<? php
// uncomment the above line - instructables doesn't like the php start command

// executes python file to read DHT11 temperature sensor //and extracts the temperature and humidity values $temperature=0; $humidity=0; $my_pos=0; $exec_msg="sudo /var/www/AdafruitDHT.py 11 22 2>&1"; $test = shell_exec($exec_msg); //extracts temperature $my_pos = strpos($test, "Temp=",0); $temperature = substr($test, $my_pos+5, 4); echo "\n ".$temperature; //extracts humidity $my_pos = strpos($test, "Humidity=",$my_pos); $humidity = substr($test, $my_pos+9, 4); echo "\n ".$humidity; ?>

Be the First to Share


    • Plywood Contest

      Plywood Contest
    • Fandom Contest

      Fandom Contest
    • Halloween Contest

      Halloween Contest



    3 years ago

    I think there has been a formatting issue somewhere with the php code. I have added it as direct code in this instructable. Just uncomment </php

    Re running from the var/www/html directory, have you ensured that the php file has 755 properties and that the file owner is www-data or whichever user is running your web server.


    4 years 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





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


    Reply 4 years ago

    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


    Reply 3 years ago

    Hello this blog of yours is really awesome i'm currently working on a project and while i'm at it i am currently using this blog of yours as a guide, unfortunately i am experiencing the error "rust you" in my raspberry pi 2 whenever i tried to pick up the data and display it in the html. What are the things that i need to configure to make this one work or do you have any suggestion doing this in another way


    Reply 4 years ago

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


    Reply 4 years ago

    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


    Reply 4 years ago

    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"