01016 ahumi rel

Upload: arturo53

Post on 02-Mar-2016

8 views

Category:

Documents


0 download

DESCRIPTION

Sensor de Humedad

TRANSCRIPT

  • 2005 Microchip Technology Inc. DS01016A-page 1

    AN1016

    INTRODUCTION

    Target AudienceThis application note is intended for hardware andfirmware design engineers that need to accuratelydetect small capacitance values.

    Goals Detect small capacitances (e.g., 0.5 pF to 6.5 nF) Use minimal number of external components Give simple firmware solution Highlight design tradeoffs and alternatives

    DescriptionThis application note shows how to use a PICmicromicrocontroller and minimal external circuitry to detectsmall capacitances. The design is based on an opera-tional amplifier (op amp) integrator. A capacitivehumidity sensor is used to illustrate this type ofapplication.

    The design is measured to verify the theory and designchoices. Alternatives and modifications to this designare briefly discussed.

    References to documents that treat these subjects inmore depth and breadth have been included in theReferences section.The appendices give detailed information that supportsthe text of this application note.

    Related Demo BoardThe measurements for this application note were madeon the Humidity Sensor PICtail Demo Board, whichis discussed in the users guide (DS51594) [15]. Thisboard is further described by:

    Order Number: PIC16F690DM-PCTLHS Assembly Number: 102-00084R1

    INTEGRATOR SOLUTIONThis section describes a design that accuratelymeasures small capacitances. It uses dual slopeintegration to measure the sensors capacitance. Usingan integrator for measuring small capacitive sensorshas three main advantages:

    Any sensor parasitic capacitance (i.e., case-to-ground stray) is forced to the correct voltage by the op amp.

    The parasitic capacitance in parallel is much smaller than other methods.

    The measured waveform has a constant slope, which improves the timing accuracy.

    Block DiagramFigure 1 shows the block diagram of the integratorsolution. The Square Wave Source voltage (VINT) isconverted to a square wave current (IINT). IINT is thenpassed to an integrator comprised of an op amp andthe sensor capacitor (CSEN). The Integrator outputs avoltage triangle wave (VSEN) whose slope depends onCSEN. The Threshold Crossing Detector tells whenVSEN is above or below two reference voltages: a lowervoltage (VRL) and a higher voltage (VRH).

    The Magnitude Control firmware routine changes thepolarity of VINT so that VSEN goes past both VRL andVRH by the desired amount. The Timing Countfirmware routine counts the time elapsed for VSEN to gofrom VRL to VRH (t1), and to go from VRH to VRL (t2).The Calculations firmware routine calculates CSENthen the relative humidity (RH) from that capacitance.

    Author: Kumen Blake and Steven BibleMicrochip Technology Inc.

    Detecting Small Capacitive Sensors Using the MCP6291 and PIC16F690 Devices

  • AN1016

    DS01016A-page 2 2005 Microchip Technology Inc.

    FIGURE 1: Integrator Block Diagram.Figure 2 shows the timing of the main waveforms. Thesupply voltages are VDD and ground (0V). The currentIINT has a positive value of IINTP, and a negative valueof IINTM (IINTP and IINTM are nearly equal magnitudes).

    FIGURE 2: Timing Diagram.

    CircuitFigure 3 shows the circuit. The PICmicro microcon-troller (U1) outputs a logic level at pin P1, making thevoltage, VINT, either 0V or VDD. The componentsexternal to U1 form an inverting (Miller) integrator. VSEN

    is a triangle wave whose slope depends on CSEN. Thefirmware, comparator and reference (VREF) in U1control the circuit as described before.

    The power supply voltages (VDD_DIG and VDD) wereassumed to vary between 3.0V and 5.5V. This designuses 1% resistors for low cost. The SR latch andTimer1 in U1 time the rise and fall times of the VSENtriangle wave.

    FIGURE 3: Op Amp Integrator Circuit.

    The voltage VSEN will have a constant positive(negative) slope when VINT is 0V (VDD):

    EQUATION 1:

    The voltage reference, VREF, is set to one of two levels:a lower reference voltage, VRL (0.125VDD), and anupper reference voltage, VRH (0.500VDD). VRL wasselected to be within the op amps output voltage

    Square Wave

    Voltage-to-Current

    Source

    Conversion

    Integrator

    Threshold CrossingDetector

    Magnitude Control

    Timing Counter

    Calculations

    VINT (square wave)

    VSEN (triangle wave)

    IINT (square wave)

    VINT

    VSEN

    VREF VRLVRH

    0VVDD

    IINT IINTM

    IINTP

    VDD

    0V

    VDD

    0V

    VRH

    VRL

    t1 t2

    Note 1: CCG is the sensors case-to-ground para-sitic capacitance. CCG should be placedat the op amps inverting input pin toimprove the op amps stability andeliminate any dynamic current throughCCG.

    2: RINT is chosen to minimize the effort tocalculate CSEN.

    VINT

    VSEN

    U1

    Comparator

    P1

    VCM

    CSEN

    MCP6291

    VDD

    IINT

    RCM2

    RCM120 k

    20 kCCM

    U2

    VREF

    100 nF

    C2

    RINT6.65 M

    P2

    100 nF

    CCG

    VDD_DIG

    100 nF

    C1

    P4

    P3

    SRLatch

    Timer1

    PIC16F690

    IINTVINT VCM

    RINT-----------------------------=

    VSENt----------------

    IINTCSEN-------------, IINT is constant=

    IINTVINT VCMRINTCSEN

    -----------------------------, VINT is constant=

  • 2005 Microchip Technology Inc. DS01016A-page 3

    AN1016range. VRH was selected to be within the comparatorscommon mode input voltage range when VDD goes aslow as 3.0V. The comparator detects where VSEN islocated relative to VRH and VRL.

    The voltage, VSEN, is a triangle waveform that goesoutside the levels, VRL and VRH; this allows the circuitto settle after changing directions, and gives time forcode overhead. The firmware sets the logic level at pinP1 low (VINT = 0V) when VSEN needs to increase, andsets it high (VINT = VDD) when VSEN needs todecrease. During measurements, the microcontrollerpins are put into the states shown in Table 1.

    TABLE 1: PIN STATES

    The IINT values shown in Figure 2 (IINTP and IINTM)have opposite signs and approximately equal magni-tudes. The magnitudes are not always equal becauseVCM is not always equal to VDD_DIG/2. This produceselapsed times (t1 and t2) that are only approximatelyequal as show in Equation 2.

    EQUATION 2:

    Ensuring Op Amp Stability and AccuracyThe op amp (U2) can behave poorly, or even oscillate,if CSEN is not properly constrained. The parasiticcapacitance from the op amps inverting input to ground(the sensors CCG and the op amps CCM) also affectsits stability. It is recommended that a unity gain stableop amp (such as the MCP6291) be used and that CSENbe set as follows:

    EQUATION 3:

    To maintain accuracy, the op amp feedback loop needssufficient loop-gain. This translates to the followingrequirement on CSEN:

    EQUATION 4:

    For instance, given a 0.5% accuracy requirement, andusing the MCP6291 for U2, gives:

    LG = 0.005 fGBWP = 10 MHz CSEN > 0.6 pF

    CSEN Extraction EquationsThe measurements return timer counts, k1 and k2,which are related to the measurement times (t1 and t2)as follows:

    EQUATION 5:

    The measurement timer counts will be averagedtogether before calculating CSEN; the reason why isillustrated in Equation 6. Since the error in VCM is in thedenominator of the k1 and k2 equations, and thenominal VCM is VDD/2, we have:

    EQUATION 6:

    Measurement Steps

    Pin States

    P1 P2

    Positive Slope (measure t1) 0 inputNegative Slope (measure t2) 1 inputNote 1: P2 is high impedance, and is always

    connected to the comparator.

    t1VRH VRLVSEN t-----------------------------

    VRH VRLVCM

    -------------------------- CSENRINT= =

    t2VRH VRLVSEN t-----------------------------

    VRH VRLVDD VCM---------------------------- CSENRINT= =

    Where:

    fGBWP = op amps Gain Bandwidth Product

    CSEN2

    2RINTfGBWP-----------------------------------

    Where:

    fGBWP = op amps Gain Bandwidth ProductLG = allowable error due to loop-gain

    CSEN1

    2RINTfGBWPLG--------------------------------------------

    Where:

    TCLK = microcontrollers instruction period

    Timer Counts:

    k1t1

    TCLK-------------

    VRH VRLVCM

    --------------------------CSENRINT

    TCLK-------------------------= =

    k2t2

    TCLK-------------

    VRH VRLVDD VCM----------------------------

    CSENRINTTCLK

    -------------------------= =

    VCM Error Analysis Equations:

    Where:

    k = ideal count (when VCM = VDD/2 exactly) = relative error (caused by VCM error)

    =

    =

    VCM_ErrorVCM

    =VCM_Error

    VDD VCM

    VCM_ErrorVDD / 2

    k1k

    1 +------------=

    k2k

    1 -----------=

    k1 k2+k

    1 2--------------=

  • AN1016

    DS01016A-page 4 2005 Microchip Technology Inc.

    For instance, a +5% error in k1 (a -5% error in k2)becomes a -0.25% error in the average ((k1 + k2)/2);this is a very significant improvement in accuracy.Greater accuracy can be achieved by reducing theoriginal error in k1. For example, reducing the k1 errorto +2% gives a -0.04% error in (k1 + k2)/2.

    The extraction equations are below in Equation 7.These equations assume CSEN is constrained asdescribed in Equation 3 and Equation 4. The constant,B1, is the circuits resolution in units of pF / count. RINT(see Figure 3) was chosen to make it easy to convertk1 and k2 into CSEN (making B1 = 0.100 pF / count).

    EQUATION 7:

    The firmware actually multiplies the sum (k1 + k2) bythe pre-calculated constant B1/2.

    HUMIDITY SENSORThe HS1101LF humidity sensor from Humirel isdescribed in detail in its data sheet [1]. It has a relativehumidity (RH) accuracy of about 2%, and its nominalcapacitance ranges from 162 pF to 193 pF.

    FIGURE 4: HS1101LF Humidity Sensors Nominal Capacitance with TA = +25C.Since CSEN changes by about 31 pF across the full RHrange, and has a nominal value of 180 pF, it followsthat:

    A 1 pF change in CSEN is a 0.56% change in its nominal value

    RH changes 3.2% for each 1 pF change in CSEN

    RH changes 6% for each 1% change in CSEN RH changes 0.32% for each increase of 1 in the

    count (k) for Figure 3

    FIRMWAREThis algorithm is implemented in the firmware for theHumidity Sensor PICtail Demo Board [15]. Thefirmware can be downloaded from Microchips website(www.microchip.com).

    Additional RequirementsThe circuit and microcontroller need to be initialized. Itis necessary to drive VSEN to a known point beforestarting the capacitance measurements; it could beeither above or below VRL when starting.

    Averaging (8 times) is included in this algorithm. Thetimer counts need to be converted to CSEN, then to RH.

    Extra delay before starting each measurementimproves the accuracy. It gives the op amp time tosettle, and allows the firmware time to prepare for thenext measurement.

    Modular CodeThe following assembly code modules (for thePIC16F690) make up the Humidity Sensor project:

    main.inc - contains I/O port and global defines used throughout the project

    main.asm - contains the main executive routine including configuration bit assignments

    initialize_f690.asm - initializes the PIC16F690 to known initial values

    capacitance.asm - reads capacitance using a dual slope integration technique; Table 2 shows the algorithm for this module

    humidity.inc - contains PwLI table segment values

    humidity.asm - contains PwLI routine to convert capacitance to %RH humidity

    ssc.asm - contains Synchronous Serial Commu-nications (SSC), a synchronous serial communi-cations protocol between a target PICmicro microcontroller unit and the PICkit 1 Flash Starter Kit or PICkit 2 Starter Kit.

    16f690.lkr - linker script for Humidity Sensor project

    These files can be downloaded from the Microchip website (www.microchip.com); and are contained in the00084R1.zip file.

    Pre-calculated Constant:

    Extraction Equation:

    B1VDD 2

    VRH VRL--------------------------

    TCLKRINT-------------=

    CSENk1 k2+

    2----------------- B1

    k1 k2+( )B12

    ------ , coded form( )

    160

    165

    170

    175

    180

    185

    190

    195

    0 10 20 30 40 50 60 70 80 90 100RH (%)

    CSE

    N (p

    F)

  • 2005 Microchip Technology Inc. DS01016A-page 5

    AN1016Top Level AlgorithmFigure 5 shows the flow chart for the top level program.This implementation includes averaging eight CSENreadings together.

    FIGURE 5: Top Level Flow Chart.The algorithm shown does not include any accuracyimprovements. The user can add code to either correctthe reference levels (VRL and VRH), using the internalADC, or calibrate the entire circuits errors (includingtemperature drift).

    Capacitance ModuleTable 2 shows the algorithm for the capacitancemodule, and includes the PICmicro microcontrollerspin states.

    The pin assignments on the Humidity Sensor PICtailDemo Board [15] used for the measurements in thisapplication note are:

    P1 = RC2 (VINT) P2 = RC1 (VSEN) P3 = RA4/T1G P4 = RC4/C2OUT

    Pin P2 is configured as the comparators input duringthe measurements. This gives the comparator time tosettle before the measurements are made.

    TABLE 2: CAPACITANCE ALGORITHM

    Relative Humidity ModuleOnce CSEN has been calculated and averaged, therelative humidity (RH) for the HS1101LF sensor can beestimated. The conversion is accomplished using apiecewise linear interpolation table [11]. Appendix A:Piecewise Linear Interpolation Table containsdetails on the design of this table.

    Main

    Initialize

    Call

    Accumulate

    Is

    ?count = 8

    No

    Yes

    Divide AccumulatedCSEN by 8

    capacitance.asm

    CSEN

    Callhumidity.asm

    Algorithm StepsPin States

    P1 P2

    Initialize VSEN (Note 1)(Move VSEN to < VRL) 1 input

    Set VREF to VRLDetect when VSEN < VRLDelayPositive VSEN Slope (Note 2)

    (Move VSEN from < VRL to > VRH) 0 inputStart count k1 when VSEN = VRLSet VREF to VRHStop count k1 when VSEN = VRHDelayNegative VSEN Slope

    (Move VSEN from > VRH to < VRL) 1 inputStart count k2 when VSEN = VRHSet VREF to VRLStop count k2 when VSEN = VRLDelayNote 1: VRL = 0.125VDD and VRH = 0.500VDD.

    These are lower range levels in the PIC16F690s VREF (CVREF).

    2: The counts k1 and k2 increment once for each PICmicro instruction cycle (TCLK = 0.5 s)

  • AN1016

    DS01016A-page 6 2005 Microchip Technology Inc.

    DESIGN MODIFICATIONS AND ALTERNATIVES

    Possible Modifications

    SIMPLE MODIFICATIONSTo produce different resolutions (pF / count), changethe RINT value. It makes the code simpler when theresolution is a simple multiple of 1 pF. One possible setof values is:

    RINT = 6.65 M for 0.1 pF / count RINT = 665 k for 1 pF / count RINT = 66.5 k for 10 pF / countMore than one resolution in the same circuit can beobtained by switching between several RINT resistorson the Printed Circuit Board (PCB). It is also possibleto use multiple microcontroller pins, one for each RINTon the PCB. The RINT values not being used wouldhave their pins set to hi-Z.

    The parasitic capacitance (CPAR) in parallel with CSENis caused by board and op amp package straycapacitances. It is typically about 0.5 pF; the calculatedCSEN should be corrected (have CPAR subtracted) bythis amount. CPAR can be measured by leaving havingCSEN open (0 pF).

    Many of the errors over relative humidity, supplyvoltage and temperature will be consistent over time.This makes it possble to calibrate out these errors; seeError Analysis.

    OP AMP INTEGRATOR WITH REDUCED CURRENTThe circuit in Figure 6 achieves greater resolution byattenuating the square wave (VA). The componentsRA1, RA2 and RA3 form an attenuator with a DC biaspoint at VDD/2 and a gain of 0.0100 V/V. Thus, the cur-rent magnitudes IINTP and IINTM will be 100 timessmaller than those produced by the circuit in Figure 3.This, in turn, produces longer integration times.

    This circuit has the following key performancenumbers:

    CSEN needs to be larger than 0.6 pF for a 0.5% accuracy and for stability

    Resolution 0.001 pF / count

    The attenuator increases the equivalent error at VCM.This can be handled by using resistors with tightertolerances for RA1, RA2 and RA3; Figure 6 uses 1%resistors for low cost.

    FIGURE 6: Op Amp Integrator Circuit with Reduced Current.

    Other CiruitsA quick overview of different methods to measurecapacitance is found in AN990, Analog Sensor Condi-tioning Circuits - An Overview (AN990) [5]. Thosedesigns include an R-C decay and an oscillator.

    The R-C decay method [6, 8] is very low cost and easyto implement. It is difficult to use this method for smallcapacitive sensors because of the microcontrollersparasitic pin capacitance and leakage currents.

    It is quite popular to use a 555 timer and the capacitivesensor to form an oscillator circuit, which works well forlarger capacitors. Smaller capacitors see greaterinaccuracies due to the 555 timers parasitic pincapacitance and leakage currents. Also, 555 timersfrom different vendors behave quite differently.

    It is also possible to create other oscillator circuits [7].They can be quite accurate with proper calibration, andthey can be either simple or complicated.

    Note 1: CCG is the sensors case-to-ground para-sitic capacitance. CCG should be placedat the op amps inverting input pin toimprove the op amps stability and elimi-nate any dynamic current through CCG.

    2: RINT is chosen to minimize the effort tocalculate CSEN.

    VA

    VSEN

    U1

    Comparator

    P1

    VCM

    CSEN

    MCP6291

    VDD

    IINT

    RCM2

    RCM120 k

    20 kCCM

    U2

    VREF100 nF

    C2

    RA11M

    P2

    100 nF

    CCG

    VDD_DIG

    100 nF

    C1

    P4

    P3SR

    Latch

    Timer1

    VINT

    PIC16F690 RINT6.65 M

    RA3

    RA220 k

    20 k

  • 2005 Microchip Technology Inc. DS01016A-page 7

    AN1016

    ERROR ANALYSISThe design in this application note is accurate enoughto make a detailed error analysis worth the effort. Thedominant error sources are covered in this section.They will be covered in the same sequence theypropagate through the circuit and algorithm. Theirimpact on RH accuracy, and possible improvements,will be sumarized at the end.

    Ratiometric DesignThe circuit was designed to be ratiometric. This isaccomplished by making IINT, VCM, VRL and VRLproportional to VDD.

    Using a ratiometric design makes the measurementtimes independent of power supply voltage (VDD),eliminating one source of measurement error.

    Current (IINT) ImbalanceWhen IINTP and IINTM are not equal, the timer counts,k1 and k2, are not equal. This causes an error () in thecalculated counts k1 and k2. CSEN Extraction Equa-tions discusses this phenomenon in detail.Errors in VCM (VCM_Error in Equation 6) contribute tothe current imbalance. The common mode voltagesetting resistors (RCM1 and RCM2) and the op amp (U1)dominate the VCM errors. If RCM1 is 1% low and RCM2is 1% high, then the relative error () would would be+2%. If op amp U1s input offset voltage (VOS_OA) is+4.5 mV and VDD is 5.0V, then would be +0.2%.

    The current IINT is also imbalanced by the op amp inputbias current (IB_OA). This produces a relative error = IB_OA / IINT. This error is largest, for CMOS input opamps, at high temperatures.

    The attenuator in Figure 6 also causes a current (IINT)imbalance. A mismatch between RA2 and RA3produces this current mismatch.

    Errors in Average Count (k)The relative error () in k1 and k2 causes a smaller errorof 2 in the average count, k = (k1 + k2)/2; see CSENExtraction Equations.Errors in the average count, k, are produced by the rel-ative error in the following:

    VREF levels (VRH VRL) RINT Comparator CMRR (change in offset vs. VSEN) Oscillator frequency

    Note that when we subtract VRL from VRH, thecomparators offset voltage is cancelled (because it isconstant).

    The op amps gain-bandwidth product can have asignificant effect on the errors for small CSEN values;see Equation 4. The smaller CSEN is, the larger thiserror is.

    Errors in Calculating CSENThe parasitic capacitance CPAR will cause an error ofabout 0.5 pF if no correction is made, and about0.1 pF if the correction is made.

    The nominal value of B1 is not exactly 0.1 pF / count; itis approximately 0.10025 pF / count. This error(+0.25%) has been designed to smaller than mosterrors.

    The designed circuits measurement resolution is0.1 pF / count. The quantization error cannot be betterthan 1/2 this value (0.05 pF / count).

    Errors in Calculating RHHumidity Sensor gives basic information on theHumirels HS1101LF capacitive RH sensor. Asexplained there, the circuit in Figure 3 has a RHresolution of about 0.32% / count (3.2% / pF). Also, a1% error in measuring CSEN produces a 6% RH error.

    In addition, there is a 2% error in the nominal RHvalue, and a 6% error due to temperature variations(at -40C and +85C).

    RH is calculated from CSEN using a piece-wise linear(PWL) lookup table [11]. This table has been designedto make the firmware simple and quick by using 64lookup table rows. This has the added benefit ofproducing a very accurate estimate of RH (better than0.01% error).

    Overview of ErrorsTable 3 includes all of the errors mentioned in thissection. These errors are at room temperature (+25C).It also shows how the errors propagate through thecircuit and the algorithm.

    The dominant errors are:

    VREF accuracy: (VRH VRL) / (VDD/2) RINT The internal oscillator frequency The op amp Gain-Bandwidth Product (fGBWP) for

    very small CSEN values The nominal sensor (HS1101LF) error

  • AN1016

    DS01016A-page 8 2005 Microchip Technology Inc.

    TABLE 3: ERROR ANALYSIS AT ROOM TEMPERATURE

    Possible ImprovementsThe VREF levels (VRH and VRL) can be corrected, insome microcontrollers, by internally connecting anADC to the VREF output. Since the ADC has betteraccuracy than the VREF ladder, the measurement canbe improved.

    Components with tighter tolerance will directly improvethe RH accuracy. The resistor, RINT, and the oscillatorare two important examples.

    Most of the remaining errors can be corrected withappropriate calibration procedures and calculations.The calculated RH can be corrected for errors acrosstemperature; both the sensor and the circuit can becalibrated at the same time. Once the decision is madeto calibrate the errors, there is no need to correct theVREF levels using the internal ADC.

    Error Sources Effects

    Cause SpecialConditions

    Worst CaseError

    (Note 1)

    Imbalance (Note 2)

    Error in k(Note 3) Error in CSEN Error in RH

    Current (IINT) ImbalanceRCM1 and RCM2 1% 2.0%VOS_OA 3 mV 0.2% 0.05% 0.05% 0.3%IB_OA (Note 4) 20 pA 0.004%Average Count (k)VRH VRL (Note 5) 4% 4% 4% 24%RINT 1% 1% 1% 6%Comparator CMRR 0.18% 0.18% 0.18% 1.1%Comparator VOS 10 mV 0% 0% 0%Oscillator internal 1% 1% 1% 6%

    external 0.01% 0.01% 0.01% 0.1%fGBWP for HS1101LF 0.002% 0.002% 0.002% 0.01%

    at CSEN = 0.6 pF 0.5% 0.5% 0.5% N/ACalculating CSENCPAR not corrected 0.5 pF 0.5 pF 1.6%

    corrected 0.1 pF 0.1 pF 0.3%B1 0.25% 0.25% 1.5%Quantization Error 0.05 pF 0.05 pF 0.3%Calculating RHHS1101LF, Nominal Error 3% 3%HS1101LF, Drift per Year 1% 1%PWL Lookup Table 0.01% 0.01%Note 1: It is assumed that VDD is at its lowest value (3.0V for this design) when converting errors in mV to percent-

    age errors.2: The error in VCM is given as a percentage of VDD/2, which is the same as the relative error .3: The error magnitudes are calculated one at a time, except when converting from to 2 (Error in k

    column) for the current imbalance. In the latter case, the relative errors are added together before squaring.

    4: This error becomes bigger as the ambient temperature increases. At +125C, IB_OAs contribution to the imbalance is 5 nA or 1.1%, causing the RH error to be 0.6%.

    5: Both VRL and VRH have a maximum specified error of 0.0104VDD. Since (VRH VRL) has a nominal value of 0.375VDD, each error becomes 0.0278(VRH VRL). The two errors were added in the RMS sense to obtain the 4% value shown above.

  • 2005 Microchip Technology Inc. DS01016A-page 9

    AN1016

    MEASURED RESULTSThe basic circuits in Figure 3 and Figure 6 weremeasured with different RINT values. First, knowncapacitors were measured to validate the accuracy ofthese designs. Then the HS1101LF relative humiditysensor was measured and compared to another,calibrated humidity sensor.

    The measurements were made on the Humidity SensorPICtail Demo Board, which is discussed in the usersguide (DS51594) [15]. This board is further describedby:

    Order Number: PIC16F690DM-PCTLHS Assembly Number: 102-00084R1

    Fine Resolution MeasurementsThe circuit in Figure 6 was measured first; it has the40 dB attenuation of the square wave. This made itpossible to measure the parasitic capacitance, CPAR,and other small capacitances. The CSEN resolution is0.001 pF / count.

    Figure 7 shows the VSEN waveform across time. T1G isthe Timer 1 Gate waveform; it shows when thecomparator decides that VSEN has reached either VRLor VRH. CSEN was 166.0 pF and RINT was 6.65 M.

    FIGURE 7: VINT and VSEN Waveforms with CSEN = 166 pF.Figure 8 shows the measurement discrepancybetween the values read from an HP4285A LCR meterand the circuit in Figure 6. These measurements weretaken across a range of allowed CSEN values. Theparasitic capacitance CPAR was measured by leavingCSEN open; the value for the Humidity Sensor PICtailDemo Board [15] turned out to be approximately0.27 pF.

    FIGURE 8: CSEN Measurement Discrepancy.

    -10%-8%-6%-4%-2%0%2%4%6%8%

    10%

    1.E+00 1.E+01 1.E+02CSEN (F)

    CSE

    N M

    easu

    rem

    ent

    Dis

    crep

    ancy

    1p 10p 100p

    Not Corrected

    Corrected:CPAR = 0.270 pFB1 = 0.09499 pF / count

    ModifiedCircuit

  • AN1016

    DS01016A-page 10 2005 Microchip Technology Inc.

    Normal Resolution MeasurementsThe circuit in Figure 3 does not attenuate the squarewave, making it possible to measure larger capacitors.The CSEN resolution is 0.1 pF / count.

    Figure 9 shows the measurement discrepancybetween the values read from an HP4285A LCR meterand the circuit in Figure 3. These measurements weretaken across a range of allowed CSEN values.

    FIGURE 9: CSEN Measurement Discrepancy.

    HS1101LF Sensor MeasurementsThe circuit in Figure 3 was used to measure theHS1101LF relative humidity sensor. The measurementresolution is 0.1 pF / count (0.6% RH change percount) and RINT is 6.65 M. The curves in Figure 10show how the sensor reacted when it was breathed onfor about half second; the result is the impulseresponse of the sensor.

    FIGURE 10: HS1101LF Impulse Response.

    LESSONS LEARNEDSeveral important lessons were learned in the processof building, measuring and debugging this design.

    Ratiometric DesignThis design assumes VDD ranges from 3.0V to 5.5V(e.g., two lithium batteries). To avoid supply rejectionerrors, and to make the design simpler to implement, aratiometric approach was very helpful. The implemen-tation is as follows:

    The square wave VINT is ratiometric VCM is ratiometric VCM and RINT make the current IINT ratiometric,

    so the voltage VSEN is ratiometric CVREF reference (used for VRH and VRL) is

    ratiometric (the other internal reference is not)

    Reference Voltages ChosenThe reference voltages (VRH and VRL) need to beselected carefully. The analog components need tostay within their allowed ranges, but VRH and VRL needto be as far apart as possible for accuracy reasons:

    The lower CVREF range is more accurate than the upper range (1.04% vs. 1.56%).

    The comparators VCMR range is VDD 1.5V, which is at its worst case value (VDD/2) when VDD = 3.0V (it is not ratiometric).

    The op amps output should stay above 0.2V to 0.3V to maintain accuracy and avoid overdrive recovery problems (about 0.1VDD when VDD = 3.0V).

    For these reasons, the design uses VRL = 0.125VDDand VRH = 0.500VDD in the lower CVREF range.

    Choosing the MicrocontrollerThe PIC16F690 has several key features that help thisdesign:

    Comparator latch makes the firmware simpler and avoids delay in a firmware loop.

    The comparator can be connected internally to the reference (CVREF) and to the board.

    It has an accurate internal oscillator. It operates over the required supply range.

    -10%-8%-6%-4%-2%0%2%4%6%8%

    10%

    1.E+00 1.E+01 1.E+02 1.E+03 1.E+04CSEN (F)

    CSE

    N M

    easu

    rem

    ent

    Dis

    crep

    ancy

    1p 10p 100p 1n 10n

    Not Corrected

    Corrected:CPAR = 0.270 pFB1 = 0.09987 pF / count

    165

    170

    175

    180

    185

    190

    195

    200

    -5 0 5 10 15 20 25 30 35 40 45Time (s)

    Sens

    or C

    apac

    itanc

    e (p

    F)

    8%

    10%

    12%

    14%

    16%

    18%

    20%

    22%

    Rel

    ativ

    e H

    umid

    ity

    breathed on sensor

    RH

    CSEN

  • 2005 Microchip Technology Inc. DS01016A-page 11

    AN1016CalibrationCapacitive relative humidity sensors are quite sensitiveto capacitive measurement errors. It can be costly touse components with tighter tolerances. The designs inthis application note are a big help because they havelow parasitic capacitance in parallel with the sensor,and they are immune to sensor case to groundcapacitances.

    The most effective way to overcome the sensitivityproblem is to calibrate the errors in production. All ofthe errors can be corrected in the same step.

    MiscellaneousUsing dual slope integration minimizes errors due tothe VCM error (IINT imbalance).

    The op amps gain-bandwidth product affects therelative error for small CSEN values. Select an op ampthat gives an acceptable error at the smallest CSENvalue that will be measured.

    RINT is selected last to make the code simpler toimplement. It is chosen so that B1 becomes aconvenient number (e.g., 0.1 pF / count).

    It is important to use a greater number of bits for theaveraging (accumulation) of CSEN. The counter in thisdesign is a 16-bit value, and the accumulator is a 24-bitvalue. This avoids truncation errors, and can preventcounter results being interpreted as negative values.

    SUMMARYThis application note shows hardware and firmwaredesign engineers how to use a PICmicromicrocontroller and an op amp integrator to accuratelymeasure small capacitances. Simple firmware isincluded that produces capacitance and relativehumidity values for the HS1101LF relative humiditysensor.

    Measurements verify the theory and design choices.An error analysis points to possible improvements inthe design. Other alternatives and modifications to thisdesign are also covered. Key lessons learned help theuser focus on the important aspects of the givendesign.

    The references and appendices give detailedinformation that supports the text of this applicationnote.

    REFERENCES

    Humidity Sensors[1] Humirel, Relative Humidity Sensor HS1100LF /

    HS1101LF, Data Sheet, Rev. C, Sept. 2004.

    General Sensors[2] The OMEGA Made in the USA Handbook,

    Vol. 1, OMEGA Engineering, Inc., 2002.[3] The OMEGA Made in the USA Handbook,

    Vol. 2, OMEGA Engineering, Inc., 2002.[4] AN866, Designing Operational Amplifier Oscilla-

    tor Circuits For Sensor Applications, JimLepkowski; Microchip Technology Inc., DS00866,2003.

    [5] AN990, Analog Sensor Conditioning Circuits AnOverview, Kumen Blake, Microchip TechnologyInc., DS00990, 2005.

    [6] AN512, Implementing Ohmmeter/TemperatureSensor, Doug Cox; Microchip Technology Inc.,DS00512, 1997.

    [7] AN895 Oscillator Circuits For RTD TemperatureSensors, Ezana Haile and Jim Lepkowski;Microchip Technology Inc., DS00895, 2004.

    Capacitive Sensors[8] AN611, Resistance and Capacitance Meter

    Using a PIC16C622, Rodger Richey; MicrochipTechnology Inc., DS00611, 1997.

    [9] Ricardo Jimenez and Claudia Angulo, MeasureRelative Humidity With a PIC MCU, DesignBriefs, Electronic Design, March 17, 2005, p. 79-80.

    [10] AN1014, Measuring Small Changes in Capaci-tive Sensors, Kumen Blake and Steven Bible;Microchip Technology Inc., DS01014, 2005.

    Related Literature[11] AN942, Piecewise Linear Interpolation on PIC12/

    14/16 Series Microcontrollers, John Day andSteven Bible; Microchip Technology Inc., 2004.

    [12] MCP6291/2/3/4/5 Data Sheet, 1.0 mA, 10 MHzRail-to-Rail Op Amp, Microchip Technology Inc.,DS21812, 2004.

    [13] PIC16F685/687/689/690 Data Sheet, 20-PinFlash-Based, 8-Bit CMOS Microcontrollers withnanoWatt Technology, Microchip Technology Inc.,DS41262, 2005.

    [14] PIC16C745/765 Data Sheet, 8-Bit CMOS Micro-controllers with USB, Microchip Technology Inc.,DS41124, 2000.

    [15] Humidity Sensor PICtail Demo Board UsersGuide, Microchip Technology Inc., DS51594,2005.

  • AN1016

    DS01016A-page 12 2005 Microchip Technology Inc.

    APPENDIX A: PIECEWISE LINEAR INTERPOLATION TABLE

    In order to convert CSEN to RH, the firmware uses apiecewise linear interpolation table [11]. This table isdesigned specifically for Humirels HS1101LF relativehumidity sensor [1]. First, the calculated CSEN value islimited in range and converted to a 10-bit integer:

    EQUATION A-1:

    The top 6 bits of CSN are used as a pointer (INTRVL)to the correct row of the table. The bottom 4 bits of CSN(CSN ACSN, interpreted as integers between 0 and15) are the input to the linear interpolation formulausing the coefficients for the current row. The entries inthe table give errors less than 0.01%.

    EQUATION A-2:

    TABLE A-1: HS1101LF COEFFICIENT LOOKUP TABLE

    CSN 0, CSEN 130.0 pF=CSEN 130.0 pF

    0.1 pF----------------------------------------, otherwise=

    X ARHO ARH1 CSN ACSN( )+=RH max min X, 100%{ }, 0%{ }=

    INTRVL ACSN ARH0(%)ARH1

    (% / LSb)

    0 0 0 01 16 0 0 17 272 0 018 288 0 019 304 -3.08 0.25020 320 0.92 0.25921 336 5.06 0.26722 352 9.33 0.27623 368 13.75 0.28524 384 18.31 0.29425 400 23.00 0.30226 416 27.84 0.31027 432 32.80 0.31828 448 37.89 0.32529 464 43.09 0.33230 480 48.40 0.33731 496 53.79 0.34132 512 59.26 0.34533 528 64.77 0.34734 544 70.32 0.34735 560 75.88 0.34636 576 81.41 0.34437 592 86.91 0.33938 608 92.33 0.33239 624 97.64 0.32540 640 100 041 656 100 0 62 992 100 063 1008 100 0

  • 2005 Microchip Technology Inc. DS01016A-page 13

    Information contained in this publication regarding deviceapplications and the like is provided only for your convenienceand may be superseded by updates. It is your responsibility toensure that your application meets with your specifications.MICROCHIP MAKES NO REPRESENTATIONS OR WAR-RANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED,WRITTEN OR ORAL, STATUTORY OR OTHERWISE,RELATED TO THE INFORMATION, INCLUDING BUT NOTLIMITED TO ITS CONDITION, QUALITY, PERFORMANCE,MERCHANTABILITY OR FITNESS FOR PURPOSE.Microchip disclaims all liability arising from this information andits use. Use of Microchips products as critical components inlife support systems is not authorized except with expresswritten approval by Microchip. No licenses are conveyed,implicitly or otherwise, under any Microchip intellectual propertyrights.

    Trademarks

    The Microchip name and logo, the Microchip logo, Accuron, dsPIC, KEELOQ, microID, MPLAB, PIC, PICmicro, PICSTART, PRO MATE, PowerSmart, rfPIC, and SmartShunt are registered trademarks of Microchip Technology Incorporated in the U.S.A. and other countries.

    AmpLab, FilterLab, Migratable Memory, MXDEV, MXLAB, PICMASTER, SEEVAL, SmartSensor and The Embedded Control Solutions Company are registered trademarks of Microchip Technology Incorporated in the U.S.A.

    Analog-for-the-Digital Age, Application Maestro, dsPICDEM, dsPICDEM.net, dsPICworks, ECAN, ECONOMONITOR, FanSense, FlexROM, fuzzyLAB, In-Circuit Serial Programming, ICSP, ICEPIC, Linear Active Thermistor, MPASM, MPLIB, MPLINK, MPSIM, PICkit, PICDEM, PICDEM.net, PICLAB, PICtail, PowerCal, PowerInfo, PowerMate, PowerTool, Real ICE, rfLAB, rfPICDEM, Select Mode, Smart Serial, SmartTel, Total Endurance, UNI/O, WiperLock and Zena are trademarks of Microchip Technology Incorporated in the U.S.A. and other countries.

    SQTP is a service mark of Microchip Technology Incorporated in the U.S.A.

    All other trademarks mentioned herein are property of their respective companies.

    2005, Microchip Technology Incorporated, Printed in the U.S.A., All Rights Reserved.

    Printed on recycled paper.

    Note the following details of the code protection feature on Microchip devices: Microchip products meet the specification contained in their particular Microchip Data Sheet.

    Microchip believes that its family of products is one of the most secure families of its kind on the market today, when used in the intended manner and under normal conditions.

    There are dishonest and possibly illegal methods used to breach the code protection feature. All of these methods, to our knowledge, require using the Microchip products in a manner outside the operating specifications contained in Microchips Data Sheets. Most likely, the person doing so is engaged in theft of intellectual property.

    Microchip is willing to work with the customer who is concerned about the integrity of their code.

    Neither Microchip nor any other semiconductor manufacturer can guarantee the security of their code. Code protection does not mean that we are guaranteeing the product as unbreakable.

    Code protection is constantly evolving. We at Microchip are committed to continuously improving the code protection features of ourproducts. Attempts to break Microchips code protection feature may be a violation of the Digital Millennium Copyright Act. If such actsallow unauthorized access to your software or other copyrighted work, you may have a right to sue for relief under that Act.

    Microchip received ISO/TS-16949:2002 quality system certification for its worldwide headquarters, design and wafer fabrication facilities in Chandler and Tempe, Arizona and Mountain View, California in October 2003. The Companys quality system processes and procedures are for its PICmicro 8-bit MCUs, KEELOQ code hopping devices, Serial EEPROMs, microperipherals, nonvolatile memory and analog products. In addition, Microchips quality system for the design and manufacture of development systems is ISO 9001:2000 certified.

  • DS01016A-page 14 2005 Microchip Technology Inc.

    AMERICASCorporate Office2355 West Chandler Blvd.Chandler, AZ 85224-6199Tel: 480-792-7200 Fax: 480-792-7277Technical Support: http://support.microchip.comWeb Address: www.microchip.comAtlantaAlpharetta, GA Tel: 770-640-0034 Fax: 770-640-0307BostonWestborough, MA Tel: 774-760-0087 Fax: 774-760-0088ChicagoItasca, IL Tel: 630-285-0071 Fax: 630-285-0075DallasAddison, TX Tel: 972-818-7423 Fax: 972-818-2924DetroitFarmington Hills, MI Tel: 248-538-2250Fax: 248-538-2260KokomoKokomo, IN Tel: 765-864-8360Fax: 765-864-8387Los AngelesMission Viejo, CA Tel: 949-462-9523 Fax: 949-462-9608San JoseMountain View, CA Tel: 650-215-1444Fax: 650-961-0286TorontoMississauga, Ontario, CanadaTel: 905-673-0699 Fax: 905-673-6509

    ASIA/PACIFICAustralia - SydneyTel: 61-2-9868-6733 Fax: 61-2-9868-6755China - BeijingTel: 86-10-8528-2100 Fax: 86-10-8528-2104China - ChengduTel: 86-28-8676-6200 Fax: 86-28-8676-6599China - FuzhouTel: 86-591-8750-3506 Fax: 86-591-8750-3521China - Hong Kong SARTel: 852-2401-1200 Fax: 852-2401-3431China - QingdaoTel: 86-532-8502-7355Fax: 86-532-8502-7205China - ShanghaiTel: 86-21-5407-5533 Fax: 86-21-5407-5066China - ShenyangTel: 86-24-2334-2829Fax: 86-24-2334-2393China - ShenzhenTel: 86-755-8203-2660 Fax: 86-755-8203-1760China - ShundeTel: 86-757-2839-5507 Fax: 86-757-2839-5571China - WuhanTel: 86-27-5980-5300Fax: 86-27-5980-5118China - XianTel: 86-29-8833-7250Fax: 86-29-8833-7256

    ASIA/PACIFICIndia - BangaloreTel: 91-80-2229-0061 Fax: 91-80-2229-0062India - New DelhiTel: 91-11-5160-8631Fax: 91-11-5160-8632India - PuneTel: 91-20-2566-1512Fax: 91-20-2566-1513Japan - YokohamaTel: 81-45-471- 6166 Fax: 81-45-471-6122Korea - GumiTel: 82-54-473-4301Fax: 82-54-473-4302Korea - SeoulTel: 82-2-554-7200Fax: 82-2-558-5932 or 82-2-558-5934Malaysia - PenangTel: 60-4-646-8870Fax: 60-4-646-5086Philippines - ManilaTel: 63-2-634-9065Fax: 63-2-634-9069SingaporeTel: 65-6334-8870Fax: 65-6334-8850Taiwan - Hsin ChuTel: 886-3-572-9526Fax: 886-3-572-6459Taiwan - KaohsiungTel: 886-7-536-4818Fax: 886-7-536-4803Taiwan - TaipeiTel: 886-2-2500-6610 Fax: 886-2-2508-0102Thailand - BangkokTel: 66-2-694-1351Fax: 66-2-694-1350

    EUROPEAustria - WelsTel: 43-7242-2244-399Fax: 43-7242-2244-393Denmark - CopenhagenTel: 45-4450-2828 Fax: 45-4485-2829France - ParisTel: 33-1-69-53-63-20 Fax: 33-1-69-30-90-79Germany - MunichTel: 49-89-627-144-0 Fax: 49-89-627-144-44Italy - Milan Tel: 39-0331-742611 Fax: 39-0331-466781Netherlands - DrunenTel: 31-416-690399 Fax: 31-416-690340Spain - MadridTel: 34-91-708-08-90Fax: 34-91-708-08-91UK - WokinghamTel: 44-118-921-5869Fax: 44-118-921-5820

    WORLDWIDE SALES AND SERVICE

    10/31/05