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.

The first step in configuring the ADC is to choose the analog input from one of the 12 available on the PIC. Then, the corresponding bits in TRIS and ANSEL or ANSELH must be set. In fact, they are set by default on power-up but it is wise to consider them explicitly. We recall that an analog input will always read as 0 for a digital input, which can be a hard-to-discover bug. Then the ADC clock source should be chosen using the ADCON1 register. A value of 30 selects the independent RC oscillator, which will do in most cases. Most configuration choices are in ADCON0. Bit 0 is ADFW, which should be 0 for left-justified output. The reference voltage is selected by VCFG; 0 selects VCC which is usually adequate, since it is regulated. An external reference voltage cdan also be applied. The next four bits (2,5) select the analog input. Bit 1, ON/DONE is used to trigger a conversion by setting it. When the conversion is complete, it is pulled low and the interrupt flag is set. This bit is tested to determine when a conversion is finished. Bit 0 turns on the ADC when it is set, and is normally left set. ADCON0 is in bank 0, while ADCON1 is in bank 1.

How to interface the display is explained in Elect72, Digital Compass. The humidity sensor is prepared by soldering a 3-pin header in the place provided.


The HIH-4030 datasheet is available on the SparkFun website.

Return to Electronics Index

Composed by J. B. Calvert
Created 1 Aug 2010
Last revised