Software Install (Updated)

Author Gravatar Image LADY ADA
The Python and C code to work with Adafruit's DHT sensors is available on Github at https://github.com/adafruit/Adafruit_Python_DHT.

We use some C code to talk to the DHT sensors since they require extremely fast timing to read, and then wrap the C code in a simple Python library for easy integration into your own programs.

Downloading the Code from Github

The easiest way to get the code onto your Pi or Beaglebone Black is to hook up an Ethernet cable, and clone it directly using 'git', which is installed by default on most distros. Simply run the following commands from an appropriate location (ex. "/home/pi"):
Copy Code
here
git clone https://github.com/adafruit/Adafruit_Python_DHT.git
cd Adafruit_Python_DHT
  1. git clone https://github.com/adafruit/Adafruit_Python_DHT.git
  2. cd Adafruit_Python_DHT

Installing the Library

To install the Python library on either the Raspberry Pi or Beaglebone Black you will first need a few dependencies. Execute the following command to install these dependencies (assuming you're using Raspbian/Occidentalis on the Pi and Debian on the Beaglebone Black):
Copy Code
here
sudo apt-get update
sudo apt-get install build-essential python-dev
  1. sudo apt-get update
  2. sudo apt-get install build-essential python-dev
If you see an error that a package is already installed or at the latest version, don't worry you can ignore it and move on.

Next, to install the library execute:
Copy Code
here
sudo python setup.py install
  1. sudo python setup.py install
This should compile the code for the library and install it on your device so any Python program can access the Adafruit_DHT python module.

Testing the Library

To test the Python library you can run some of the example programs in the examples folder. The AdafruitDHT.py example is a simple program which takes from the command line parameters the type of sensor (11, 22, or 2302) and GPIO pin connected to the sensor, and displays a single reading from the sensor.

First navigate to the examples folder by executing:
cd examples
Now to run the example on a Raspberry Pi with an AM2302 sensor connected to GPIO #4, execute:
sudo ./AdafruitDHT.py 2302 4
Make sure to run the command as root with the sudo command as shown above or else the program will fail to run (root access is required for reading and writing the GPIO pins).

Alternatively, to run the example on a Beaglebone Black with a DHT22 sensor connected to pin P8_11, execute:
sudo ./AdafruitDHT.py 22 P8_11
Again make sure the command is run as root with sudo!

After the program executes you should see both the temperature and humidity displayed once. If you see an error that the sensor could not be read, double check you have the right GPIO pin connected to the data line of the DHT sensor and specified in the last parameter.

Note that sometimes you might see an error that the sensor can't be read and to try again, even if you have your connections setup correctly. This is a limitation of reading DHT sensors from Linux--there's no guarantee the program will be given enough priority and time by the Linux kernel to reliably read the sensor. If this occurs, run the program again (or call the read function again in your code) to try to get a new reading. In testing on both the Raspberry Pi & Beaglebone Black, about 75% of the read requests should generally succeed and return a result (assuming the board is not under heavy load).

Examine the source code for AdafruitDHT.py and simpletest.py to see simple examples of reading the DHT sensors from Python code.

C Language Code

If you'd like to access the DHT sensors using C/C++, you can use the C code that powers the Python library in your own program. Look at the source/Raspberry_Pi or source/Beaglebone_Black directory to see the C code for the Raspberry Pi or Beaglebone Black respectively. The code exposes a single function like pi_dht_read which performs the bitbang read of the DHT sensor.

Feel free to copy this code into your own program to read DHT sensors. Note that you will need to include all the .c & .h files in the directory, and the common_dht_read.c & common_dht_read.h files in the parent source directory.
Last updated on 2015-05-04 at 04.27.56 PM Published on 2012-08-24 at 03.19.06 PM
$6.50
Adafruit Assembled Pi Cobbler Breakout + Cable for Raspberry Pi
ADD TO CART
$55.00
BeagleBone Black Rev C - 4GB Flash - Pre-installed Debian
ADD TO CART
$11.95
Miniature WiFi (802.11b/g/n) Module: For Raspberry Pi and more
ADD TO CART
$15.00
AM2302 (wired DHT22) temperature-humidity sensor
ADD TO CART
$5.00
DHT11 basic temperature-humidity sensor + extras
ADD TO CART
$9.95
DHT22 temperature-humidity sensor + extras
ADD TO CART
$55.00
Element 14 BeagleBone Black Rev C - 4GB - Pre-installed Debian
ADD TO CART

RELATED GUIDES