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:
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
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.