I’m not a world traveler but I’ve been to a number of places that differ radically in temperature and humidity. I don’t care what anyone says, “dry heat” means nothing when the thermometer is above 110 degrees. Likewise, -40 degrees is plenty cold in anybody’s book. But humidity actually does play a factor because a dry cold doesn’t feel the same as a wet cold and dry heat doesn’t feel the same as wet heat. The worst humidity I can remember was when I was at the Air Force technical school in Biloxi, Mississippi. We would send our fatigues to the base laundry and they would literally be as stiff as boards when we got them back. To get them on we first had to pry apart the leg and sleeve openings. But the nice crease from all that starch would literally wilt within ten minutes of being out in the summer sun and humidity. Now if that intro really whet your appetite to build a temperature and humidity measuring machine then you’re in luck because that’s what this project is all about.
Humidity and Temperature Modules
As you can see in the pictures, there are basically two physical versions of the DHT-11/DHT-22/AM2302 modules commonly available. In the first picture we have the bare module with four pins. As can be seen, pin 3 has no connection. This module requires that a resistor (typically 4.7k) be added from VCC to the data pin in order to provide a high logic level whenever the data pin is not being driven by the PIC. In the other picture we can see that the sensor comes already mounted on a small PC board and has just the necessary three pins for power and data. It also already has a 4.7k resistor connected between VCC and the data line on the board.
One other thing to keep in mind is that both physical versions are available for each of the sensors. The DHT-11 and the DHT-22/AM2302 function in the same basic way but the DHT-22/AM2302 has better specs and wider ranges for the temperature and humidity, including negative temperatures.
The hardware connections for the basic module are shown in the diagram above. If you buy one of the modules that is already mounted on a PC board then the pin functions should be labeled on the board and you won’t need the additional 4.7k resistor. The interface to the 1602 LCD display is based on the hardware and software detailed in Episode 1 so that detail is not shown here. The only difference is that the PIC pins used to interface to the LCD and the shift register were moved so that one special purpose PIC pin could be used for this application. If you look back to the IR Remote Decoder post you can see that this is essentially the same setup that we used to interface to an IR receiver module. In fact, the software has similarities as well because both modules transmit a serial data stream and we time the pulses using a PIC timer and the External Interrupt input pin.
The data waveform timing is show above. Unlike the IR detector where all we did was monitor for incoming data pulses, the DHT sensors require a trigger pulse from the PIC before they will send data. That is similar to the ultrasonic sensor project except that the DHT only uses one pin for both the trigger and the response. Because it is a 1-wire interface the PIC software needs to change the definition of the External Interrupt pin on the fly so that it can function as both input and output during the communications sequence. Each time an update is desired the PIC will define the pin as an I/O output and then change it back to the External Interrupt input in order to receive the data from the DHT. All of that is reflected in the waveform. Initially, the PIC sends out a low pulse at least 18 ms in length. After that it switches the pin back to an input and the 4.7k pull-up resistor detailed in the hardware section ensures that the DHT now sees the required high logic level before it will respond. Similar to the IR sensor in Episode 2, the DHT will send a start of message pulse (80 us low followed by 80 us high). The subsequent data pulses always begin with a 50 us low followed by either a 26 us high pulse (data bit = 0) or by a 70 us high pulse (data bit = 1).
The complete data stream from the DHT consists of 40 bits. The first 16 bits are humidity data, the second 16 bits are temperature data, and the last 8 bits are a message checksum. In the DHT-11, the first 8 bits are an integer value for the humidity and the second 8 bits are zeros. The third 8 bits are an integer value for the temperature and the fourth 8 bits are zero. That makes life pretty easy in the software. Because the DHT22/AM2302 is more accurate and has a wider range for the temperature and humidity, the data format is defined differently. For the humidity, the 16 data bits represent both an integer and a fractional value (to one decimal place). For instance, a received decimal value of 652 represents a humidity of 65.2%. The 16 bits for the temperature are formatted the same way except that the most significant bit represents the sign of the data. The data is not in 2’s complement format so a positive temperature will have the same numeric value as the equivalent negative temperature.
The software links are listed below. While it is targeted for the 12F683, it is easily ported to bigger versions of the PIC. Mostly it requires changing names like TRISIO to TRISA, and GPIO to PORTA. You will also need to change the line that identifies the PIC version (LIST=) and the INCLUDE file but those are intuitive changes. The __CONFIG line may also need tweaking just because one or two of the labels used are spelled differently in some of the INCLUDE files. Just make sure that the PIC you use has a pin that allows an External Interrupt input (usually labeled EXT). As we’ve seen before, the 16F688 (14-pin PIC) could be used with the same physical pin connections as the 12F683 if you want more I/O pins.
There are actually three versions of the software provided. All three use the same code in the interrupt handler to capture the bit stream from whichever version of the sensor we are using. The differences in the main part of the software are in which data bits are captured and used. The DHT-11 version simply captures the first 8 bits and the third 8 bits and then parses them into integer values for the humidity and temperature display. The parsing is simply done by repetitive subtraction to determine the ten’s digit. The remainder then represents the unit’s digit.
The second and third versions are both for the DHT-22/AM2302. The difference is in how the data bits are parsed. This module is a bit trickier to work with because the fractional part of the values requires the sensor to output 10 significant bits instead of just the 8 bits used by the DHT-11 for each value. Given that the PIC instructions only work for 8-bit data, it means that the code complexity needs to increase in order to properly handle all 10 bits. In the “Table” version of the software we simplify the code by just capturing the 8 most significant bits and then using that value to index into lookup tables for the display digits. That method means that we lose two bits of fractional data (0.3) in our resolution. For this type of project that is a reasonable tradeoff. The other version of software retains the full 10-bit precision but at the expense of more complex logic for data capture and parsing. Both versions are provided as examples of different methods you can use in your projects.
If you are concerned about the possibility of an occasional bad data transmission, then you would want to include code to verify the 8 bits of checksum at the end of each bit stream. To do that you would capture each byte of data individually, add the first four bytes together (ignore the carry bit), and then verify that the result matches the fifth byte.
The values displayed for the humidity and temperature may not change much while you are watching them so it’s nice to have something that lets you know that the measurements are being taken as planned. To do that I provided a variable called “Heartbeat” that gets toggled between an ASCII space (blank) and a star.
Here is a picture that shows typical results. It is of the DHT-22/AM2302 and shows the fractional part of the data. The DHT-11 version has the same display except it does not include the fractional digits. That’s it for this post. Check out my other electronics projects.