Since my work with the wireless temperature sensor I have been looking for a combination temperature humidity sensor. I have always been fascinated by the weather and how to measure various aspects of it. When I was very young I learned that human hair expands and contracts with humidity. I built a very crude device using one of my sisters long hairs that measured humidity. I calibrated it using a very hot shower. My sister wasn’t too happy about my pulling one of her hairs nor was my mother all that happy with all the hot water I used to calibrate the device. But, when I was finished the device actually worked.
This isn’t extremely accurate nor can you hook this up to a computer. At least I haven’t thought of a way to do it. But, I found a DHT22 or AM2302 on ebay. This device seemed to be perfect. It measures both humidity and temperature. Up to this point my wireless sensors only measured temperature. With this new device I could add humidity measurements.
I ordered some of the devices and started reading the data sheets. I found a couple of data sheets that were all in Chinese. This was very hard for me to understand. I did some more digging and found a data sheet in English. This data sheet talked about the protocol that the DHT22 uses and how to hook it up.
The protocol is a little strange. It uses a single bidirectional data line. The host processor must pull this line low for about 18ms to get the attention of the DHT22. After this the DHT22 then takes over the line and sends out various pulses to send the humidity and data information to the host processor. I have done this type of work a few times before. I have done custom serial protocols for home automation companies here in the valley.
To decode this serial protocol I used the change notification interrupt on the PIC. This is a very handy feature. When enabled the PIC generates an interrupt on each edge either up or down to an I/O pin. With this I was able to time the various pulses from the DHT22 and thus decode the data stream from the device. In the data sheet it talks about the timing used to generate a 1 bit and a 0 bit. I used a timer to time this and used a threshold to tell me if the wave form from the DHT22 was a 1 bit or a 0. I just had to then collect the data bits into bytes and then into an array of bytes.
Once I had collected the array of bytes I was able to then generate a check sum and verify that the packet was correct. Once I had a correct packet I could then put the temperature and humidity information into a packet to be sent out using the transmitter on the wireless temperature humidity sensor.
This device automatically goes into a low power mode but I found that this power down mode was still about 50ua. I’m use to running down in the 5ua region. I attached a MOSFET to the DHT22 to allow the host processor to power down the DHT22 thus reducing the power requirements on the device. I’m using a CR2032 coin cell and want the device to be as low power as possible. This gives me the flexibility to turn the device off and thus reduce the power.
The code is simple to understand. There is a state machine in the change notification interrupt that goes through the protocol from the DHT22 and decodes each of the bits. After decoding the bits it collects these bits into bytes and then into an array of bytes.