I was finally able to make the final decision on what driver to use for my DHT22 / AM2302 sensor.
After trying out a few solutions, I initially settled on the Adafruit one, but I didn't feel comfortable enough with it, due to the long time it takes to get a good and reliable reading. (you may have to read it a couple of times and average it out, adding to the time)
This is a problem with most solutions, because the DHT, when triggered, is blurting out it's data pretty fast, and you need to capture all of it. The problem of course with our Pi and Debian is that it's not a real-time system. The house-holding tasks of the OS can interrupt the data capture, and you may have to do it all over again.
Unless you go pretty deep into the kernel, and prioritize the reading task and/or disable interrupts for a while, this is more of a hit and miss proposition. The Adafruit driver is typical for this solution on a Pi and that means it's slow. You also need to wrap some good code around it, to avoid the pitfalls in our program. If you just want to have the temperature and relative humidity for kicks, that's probably OK, but if you want to do something more serious, you need to be aware of the inherent issues with these DHT devices.
A few months ago, I found a promising solution from one of the forum contributors, and that appealed to me. http://www.raspberrypi.org/forums/viewt ... 50#p506650
Daniel uses the SPI interface to capture the burst of data from the DHT, and this interface does not get interrupted by the OS. It acts like a logic analyzer or digital scope. Although the code worked fine, as soon as I embedded it in my application, I experienced OS crashes due to the number of open files. At the time, I could not figure it out and decided to use the Adafruit solution.
Last week, I wanted to try the SPI solution again, and to my dismay, had the same open file errors. This time I got some more help and together we were able to identify and correct the problem I was having. http://www.raspberrypi.org/forums/viewt ... on#p637420
My application is a web-based thermostat program that is pretty complex, mostly because I want it to be as fool- & fail-proof as possible. There are several posts on this Forum about the hardware I designed for this project.
At this moment I use a couple of DS18B20 sensors for the temperatures (inside, outside and attic), but wanted to add humidity for the inside. Because the temperature drives the HVAC system, I want it to be as robust and reliable as possible. Before I include any code that is not mine, I want to understand it, and test it myself before I start to use it.
This is also a great learning experience as well for me. Because there are so many posts in the various Forums on the Web, this one included, I also wanted to document my findings, so we can all share the collected "wisdom".
Let's start with the datasheet. There are many around, but this one is one of the best: http://www.dlnmh9ip6v2uc.cloudfront.net ... /RHT03.pdf
If you click on the link, it will download the PDF.
You'll probably want to read the document first, but here is a quick summary:
The DHT needs to be triggered before it will send the information.
This is how that works:
Code: Select all
Initialization: Pi pulls up for Sensor pulls up for 20-40 uSec 80 uSec to Ack _____ _____ ____________ ____ | | | | | | |___________________| |______________| |_______| Sensor pulls low 50uSec Start data Pi pulls Low for >1Ms for 80 uSec to Ack transmission
Code: Select all
Bit transmission for all 40 bits: 26-28 uSec for a "0" 70 uSec for a "1" _____ _________ ________________________ | | | | | |_________| |__________| |___________>>> 50uSec Low 50uSec Low 50uSec Low to signal start of bit transmission
Daniel (danjperron) decided to use the SPI bus, and really only uses the MISO and MOSI pins. This is how the ports need to connect to the DHT.: Note the use of a diode to split the signal in a driving and receiving portion. Also note the pull-up resistor of 1K. In many documents, you will find a resistor of 4K7, and that is what I initially used as well. If you look at the signals with a scope, you will see that the 4K7 is not sufficient to overcome the capacitance on the pin to pull it high quick enough. Here is the signal with the 4K7: And here with a 1K resistor. A significant improvement, which will aid us in the capturing and decoding of the bits later on. In both screenshots, the A-trace (top) is the MISO signal, and the B-trace (bottom) is the MOSI signal. The MISO signal is what is coming from the DHT, and the MOSI is the "clock signal" we're sending to it.
Next we're going to examine the wake-up sequence, but because I can only upload 3 files, I'll continue in the next post.