Electronic Design

Measure Relative Humidity With A PIC MCU

A relative humidity meter can be built using a sensor with a capacitive response and a PIC microcontroller (MCU), as shown in the figure. The capacitance range of the sensor, the HS1101 from Humirel (www.humirel.com), varies from 162 to 202 pF. Functioning as an oscillator, a TLC555 CMOS timer performs the conversion from capacitance to frequency. The timer's frequency equation is given by:

F = 1.44/(RA + 2RB) × CX

where resistors RA and RB equal 421 kΩ and 42.4 kΩ, respectively. CX is the transducer's capacitance. The 1-MΩ resistor connected to the timer's Control-Voltage input is used to unbalance the internal temperature coefficient that matches the sensor's temperature coefficient.

The MCU measures the period of the input signal and converts it to a digital relative-humidity reading. This is achieved by using the MCU's internal counter (TMR1) and the capture/compare/pulse-width modulation module (CCP). Because the transducer's capacitance range is too narrow, the period range of the signal will be too small. Therefore, an 8.000-MHz crystal is used to get 0.5 µs per instruction in the MCU. In addition, the CCP module is configured to divide the input signal by four. This will increase the counting pulses' range within the different periods provided by the TLC555. (The full code listing, 9808.zip, is available at the top of this article.)

The PIC's CCP module can detect rising or falling edges every four or 16 pulses. In this case, it was configured for every four rising edges. This detection is registered in the flag named CCP1IF in register PIR1. The flag is set when the first rising edge is detected, and TMR1 is enabled via software to start counting. Next, the flag must be erased to wait until detection of the fourth rising edge, and then stop the Timer. The total count, which represents a period, must now be transferred from the files TMR1L and TMR1H to the files CCPR1L and CCPR1H, respectively.

With this data, we proceed to converting the period to a humidity percentage. This is done by subtracting from the result the number that corresponds to a humidity of 0%; in this case, 62 decimal (3EH). This was determined from the equation supplied by the manufacturer and from the frequency generated by the TLC555 timer. The 16-bit subtraction is done by using the two's complement from the said number and adding this result to the data obtained from the TMR1. The result of this arithmetic operation corresponds to a humidity percentage. Now, the MCU proceeds to perform the conversion from binary to BCD code.

The binary to BCD conversion uses three files: units, tens, and hundreds. The result of the humidity percentage is sent to the units file, where it will have a subtraction of 10. The accumulator, W, stores this result.

By testing the carry bit from the status register, we will know if the result is greater than 10. If it's lower than 10, the result only has units and there's no need to continue doing more comparisons. This way, the conversion will be located in the units file. On the other hand, if the result is greater than 10, the tens file is incremented, and the same process of subtracting 10 to the units file must continue while the tens file is being incremented.

When the tens file is equal to 10, the hundreds file is incremented. The same process is performed until the number in the units file is lower than 10. When this conversion ends, the result in each file will be in BCD code. The BCD-to-seven-segments conversion is performed using a subroutine called OPTABLE.

The circuit has a four-digit multiplexed common-cathode LED display. It requires a multiplexing routine to display the readings correctly. This multiplexing is done at 200 Hz by switching the three 2N2222 transistors to actuate one display at a time.

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.