DHT11 Temperature and Humidity Sensor and the Raspberry Pi

3

The DHT11 is a low-cost temperature and humidity sensor. It isn’t the fastest sensor around but its cheap price makes it useful for experimenting or projects where you don’t require new readings multiple times a second. The device only requires three connections to the Pi. +3.3v, ground and one GPIO pin.

DHT11 Specifications

The device itself has four pins but one of these is not used. You can buy the 4-pin device on its own or as part of a 3-pin module.

DHT11 Sensor Dimensions

The modules have three pins and are easy to connect directly to the Pi’s GPIO header.

  • Humidity : 20-80% (5% accuracy)
  • Temperature : 0-50°C (±2°C accuracy)

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

Hardware Setup

DHT11 Module

The 4-pin device will require a resistor (4.7K-10K) to be placed between Pin 1 (3.3V) and Pin 2 (Data).

The 3-pin modules will usually have this resistor included which makes the wiring a bit easier. For this reason I got hold of the module which I could then attach to the Pi with a piece of 3-way Dupont cable.

Different suppliers may wire the module pins differently so check the PCB markings to identify Vcc (+), data and Ground (-).

The 3 pins should be connected to the Pi as shown in the table below :

DHT PinSignalPi Pin
13.3V1
2Data/Out11 (GPIO17)
3not used
4Ground6 or 9

Your data pin can be attached to any GPIO pin you prefer. In my example I am using physical pin 11 which is GPIO 17. Here is a 4-pin sensor connected to the Pi’s GPIO header. It has a 10K resistor between pin 1 (3.3V) and 2 (Data/Out).

DHT11 connected to Raspberry Pi

Python Library

The DHT11 requires a specific protocol to be applied to the data pin. In order to save time trying to implement this yourself it’s far easier to use the Adafruit DHT library.

The library deals with the data that needs to be exchanged with the sensor but it is sensitive to timing issues. The Pi’s operating system may get in the way while performing other tasks so to compensate for this the library requests a number of readings from the device until it gets one that is valid.

Software Setup

To start with update your package lists and install a few Python libraries :

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

Then clone the Adafruit library from their repository :

git clone https://github.com/adafruit/Adafruit_Python_DHT.git
cd Adafruit_Python_DHT

Then install the library for Python 2 and Python 3 :

sudo python setup.py install
sudo python3 setup.py install

Hopefully at this point the library is installed and ready to be used within a Python script.

Adafruit Example Python Script

Adafruit provide an example script that you can use to check your sensor is operating correctly.

cd ~
cd Adafruit_Python_DHT
cd examples

Then :

python AdafruitDHT.py 11 17

The example script takes two parameters. The first is the sensor type so is set to “11” to represent the DHT11. The second is the GPIO number so for my example I am using “17” for GPIO17. You can change this if you are using a different GPIO pin for your data/out wire.

You should see an output similar to this :

Temp=22.0* Humidity=68.0%

Using the Library In Other Python Scripts

Once the Adafruit library is installed and you’ve got the example script to work you can use the sensor in your own scripts. Simply import the module, setup a few variables and call the “read_retry” function :

import Adafruit_DHT

# Set sensor type : Options are DHT11,DHT22 or AM2302
sensor=Adafruit_DHT.DHT11

# Set GPIO sensor is connected to
gpio=17

# Use read_retry method. This will retry up to 15 times to
# get a sensor reading (waiting 2 seconds between each retry).
humidity, temperature = Adafruit_DHT.read_retry(sensor, gpio)

# Reading the DHT11 is very sensitive to timings and occasionally
# the Pi might fail to get a valid reading. So check if readings are valid.
if humidity is not None and temperature is not None:
  print('Temp={0:0.1f}*C  Humidity={1:0.1f}%'.format(temperature, humidity))
else:
  print('Failed to get reading. Try again!')

This script can be downloaded directly to your Pi using :

wget https://bitbucket.org/MattHawkinsUK/rpispy-misc/raw/master/python/dht11.py

and run using :

python dht11.py

Buying a DHT11

The DHT11 is available from many electrical component sellers. Here are a selection :

Some may only sell the device itself so you may need to shop around if you would prefer the 3-pin module.

Final Thoughts

The DHT11 is probably best suited for projects where rapid data readings are not required and the environment is not expected to see sudden changes in temperature or humidity. A weather station would be an idea project but a central heating controller may require something different.

For projects that need a faster temperature reading rate (<2 sec) I would probably use a DS18B20 as they respond much faster to changes in temperature. But with both these sensors costing <$5 you can probably afford to buy both and experiment.

Share.

3 Comments

  1. Thanks worked perfectly as a Python3 script on Raspberry Pi Zero W running Jessie version 8. Thanks very much. I had used the PigPio library before, but prefer sticking to ordinary GPIO on the CPU starved RPi Zero W.

  2. Some great tutorials here. I got the DHT11 to work OK. Earlier I got an OLED 1306 screen to work (they cost less than £2) I’d love to output the temperature and humidity data to the OLED 1306. What about a tutorial on that – or point me in the right direction. Thanks

Leave A Reply

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