## A Humidity Meter

The project is to construct a meter displaying atmospheric relative humidity in percent, using the Honeywell HIH-4030 sensor. available on a breakout board from SparkFun Electronics as SEN-09569. We use a 4-digit 7-segment display from SparkFun, COM-09764, and a PIC16F690 to control the meter. This project exercises the Synchronous Serial Port (SSP) and the analog-to-digital converter of the PIC. This project is very easy if we reuse the program concepts and the experience with the Serial Peripheral Interface (SPI) of previous projects. The display is managed in exactly the same way.

The relative humidity is the percentage that the partial pressure of the water vapor is of the saturation vapor pressure of water at the ambient temperature. The saturation vapor pressure of water at 20°C is 2.339 kPa or 17.55 torr or 0.339 psi. At 40% relative humidity, the water vapor pressure is 0.936 kPa. The ideal gas law can be used to find the mass of water in any desired volume. The absolute humidity is, in fact, the amount of water in a given volume of air, often expressed in gram per cubic metre. In this case we find 6.9 g/m3. See the Wikipedia article on "humidity". Note that air does not "hold" water vapor, but the gases act independently to a very good approximation. As the temperature decreases, the water vapor pressure decreases linearly according to p = nRT/V, but the saturation pressure decreases exponentially, so the relative humidity increases, reaching 100% at the dew point.

When operating from a 5 V supply, the HIH-4030 outputs 0.958 V at 0% humidity, and the output voltage increases by 0.03068 V for each percent of relative humidity. The output voltage range is then 0.958 to 4.026 V for 0% to 100% humidity. The 10-bit ADC of the PIC then outputs binary 00C4 to 0338 with a 5 V reference, since the step size is 0.00488 V. The PIC will deliver this in a convenient left-justified form as 3100 to CE40. To find the relative humidity, we first measure the output of the sensor, then subtract 3100 to get a binary number proportional to the relative humidity. We then sum the contribution from each 1-bit. The highest-order bit, b10, corresponds to a voltage difference of 2.5 V and a relative humidity of 2.5/0.03068 = 81.5%. Each succeeding bit corresponds to half this amount. For example, if the output is 3.00 V, then the left-justified binary value is 9980. Subtracting 3100 gives 6880, or 01101000100000. Adding the contributions from the 1-bits gives 66.6% relative humidity. This is good practice on how to do numerical calculations without floating-point available. We need a routine that will decimally add the digits ADD1, ADD2, ADD3, and ADD4 to the registers DIG1, DIG2, DIG3, DIG4. Such a routine is shown at the right. First, the DIGn registers are cleared. For each bit shifted out, the ADDn registers are loaded with the corresponding decimal digits, and the addition routine is called. When this has been done for all the bits, the four DIGn are sent to the display in the order 1, 2, 3, 4. DIG1 will be the most significant digit, the leftmost one (which is slightly counter-intuitive, but the display works like that). It is a nice feature to blank one or two leading digits. Sending 0x78 to a digit will blank it. The display will show 0.0 for zero relative humidity.