AN1016 Detecting Small Capacitive Sensors Using the MCP6291 and PIC16F690 Devices Author: Kumen Blake and Steven Bible Microchip Technology Inc. INTRODUCTION Target Audience This application note is intended for hardware and firmware design engineers that need to accurately detect 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 Description This application note shows how to use a PICmicro® microcontroller and minimal external circuitry to detect small capacitances. The design is based on an operational amplifier (op amp) integrator. A capacitive humidity sensor is used to illustrate this type of application. The design is measured to verify the theory and design choices. Alternatives and modifications to this design are briefly discussed. References to documents that treat these subjects in more depth and breadth have been included in the “References” section. The appendices give detailed information that supports the text of this application note. INTEGRATOR SOLUTION This section describes a design that accurately measures small capacitances. It uses dual slope integration to measure the sensor’s capacitance. Using an integrator for measuring small capacitive sensors has three main advantages: • Any sensor parasitic capacitance (i.e., case-toground 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 Diagram Figure 1 shows the block diagram of the integrator solution. The “Square Wave Source” voltage (VINT) is converted to a square wave current (IINT). IINT is then passed to an integrator comprised of an op amp and the sensor capacitor (CSEN). The “Integrator” outputs a voltage triangle wave (VSEN) whose slope depends on CSEN. The “Threshold Crossing Detector” tells when VSEN is above or below two reference voltages: a lower voltage (VRL) and a higher voltage (VRH). The “Magnitude Control” firmware routine changes the polarity of VINT so that VSEN goes past both VRL and VRH by the desired amount. The “Timing Count” firmware routine counts the time elapsed for VSEN to go from VRL to VRH (t1), and to go from VRH to VRL (t2). The “Calculations” firmware routine calculates CSEN then the relative humidity (RH) from that capacitance. Related Demo Board The measurements for this application note were made on the Humidity Sensor PICtail™ Demo Board, which is discussed in the user’s guide (DS51594) [15]. This board is further described by: • Order Number: PIC16F690DM-PCTLHS • Assembly Number: 102-00084R1 © 2005 Microchip Technology Inc. DS01016A-page 1 AN1016 is a triangle wave whose slope depends on CSEN. The firmware, comparator and reference (VREF) in U1 control the circuit as described before. Square Wave Source VINT The power supply voltages (VDD_DIG and VDD) were assumed to vary between 3.0V and 5.5V. This design uses 1% resistors for low cost. The SR latch and Timer1 in U1 time the rise and fall times of the VSEN triangle wave. (square wave) Voltage-to-Current Conversion IINT (square wave) Integrator VDD_DIG C1 VSEN (triangle wave) 100 nF Threshold Crossing Detector Magnitude Control U1 PIC16F690 P1 RINT VINT 6.65 MΩ IINT P4 Timing Counter Timer1 FIGURE 1: Integrator Block Diagram. Figure 2 shows the timing of the main waveforms. The supply voltages are VDD and ground (0V). The current IINT has a positive value of IINTP, and a negative value of –IINTM (IINTP and IINTM are nearly equal magnitudes). IINT VDD –IINTM VRH t1 t2 100 nF Comparator VRH U2 MCP6291 VREF CCM 100 nF VDD RCM1 20 kΩ RCM2 20 kΩ Op Amp Integrator Circuit. EQUATION 1: VRL 0V Timing Diagram. Circuit Figure 3 shows the circuit. The PICmicro® microcontroller (U1) outputs a logic level at pin P1, making the voltage, VINT, either 0V or VDD. The components external to U1 form an inverting (Miller) integrator. VSEN DS01016A-page 2 SR Latch The voltage VSEN will have a constant positive (negative) slope when VINT is 0V (VDD): VDD VREF C2 2: RINT is chosen to minimize the effort to calculate CSEN. VRL 0V VCM Note 1: CCG is the sensor’s case-to-ground parasitic capacitance. CCG should be placed at the op amp’s inverting input pin to improve the op amp’s stability and eliminate any dynamic current through CCG. IINTP VSEN CSEN CCG FIGURE 3: 0V VDD FIGURE 2: VSEN P3 Calculations VINT P2 V INT – V CM I INT = ----------------------------R INT Δ V SEN I INT ---------------- = ------------- , I INT is constant Δt C SEN V INT – V CM I INT = ----------------------------- , RINT C SEN V INT is constant The voltage reference, VREF, is set to one of two levels: a lower reference voltage, VRL (0.125VDD), and an upper reference voltage, VRH (0.500VDD). VRL was selected to be within the op amp’s output voltage © 2005 Microchip Technology Inc. AN1016 range. VRH was selected to be within the comparator’s common mode input voltage range when VDD goes as low as 3.0V. The comparator detects where VSEN is located relative to VRH and VRL. The voltage, VSEN, is a triangle waveform that goes outside the levels, VRL and VRH; this allows the circuit to settle after changing directions, and gives time for code overhead. The firmware sets the logic level at pin P1 low (VINT = 0V) when VSEN needs to increase, and sets it high (VINT = VDD) when VSEN needs to decrease. During measurements, the microcontroller pins are put into the states shown in Table 1. TABLE 1: PIN STATES EQUATION 4: 1 C SEN ≥ -------------------------------------------2 π R INT f GBWP ε LG Where: fGBWP = op amp’s Gain Bandwidth Product εLG = allowable error due to loop-gain For instance, given a 0.5% accuracy requirement, and using the MCP6291 for U2, gives: • • • εLG = 0.005 fGBWP = 10 MHz CSEN > 0.6 pF Pin States CSEN Extraction Equations P1 P2 Positive Slope (measure t1) 0 input Negative Slope (measure t2) 1 input The measurements return timer counts, k1 and k2, which are related to the measurement times (t1 and t2) as follows: Measurement Steps Note 1: P2 is high impedance, and is always connected to the comparator. The IINT values shown in Figure 2 (IINTP and –IINTM) have opposite signs and approximately equal magnitudes. The magnitudes are not always equal because VCM is not always equal to VDD_DIG/2. This produces elapsed times (t1 and t2) that are only approximately equal as show in Equation 2. EQUATION 2: V RH – V RL V RH – V RL t2 = ----------------------------- = ---------------------------- • C SEN R INT ΔVSEN ⁄ Δt V DD – V CM Ensuring Op Amp Stability and Accuracy The op amp (U2) can behave poorly, or even oscillate, if CSEN is not properly constrained. The parasitic capacitance from the op amp’s inverting input to ground (the sensor’s CCG and the op amp’s CCM) also affects its stability. It is recommended that a unity gain stable op amp (such as the MCP6291) be used and that CSEN be set as follows: EQUATION 3: 2 C SEN ≥ ----------------------------------2 π RINT f GBWP Where: = Timer Counts: t1 V RH – VRL C SEN R INT k 1 = ------------- = -------------------------- • ------------------------V CM T CLK T CLK t2 VRH – V RL C SEN RINT k 2 = ------------- = ---------------------------- • ------------------------VDD – VCM T CLK T CLK Where: TCLK V RH – V RL V RH – VRL t1 = ----------------------------- = -------------------------- • C SEN R INT ΔVSEN ⁄ Δt V CM fGBWP EQUATION 5: op amp’s Gain Bandwidth Product To maintain accuracy, the op amp feedback loop needs sufficient loop-gain. This translates to the following requirement on CSEN: © 2005 Microchip Technology Inc. = microcontroller’s instruction period The measurement timer counts will be averaged together before calculating CSEN; the reason why is illustrated in Equation 6. Since the error in VCM is in the denominator of the k1 and k2 equations, and the nominal VCM is VDD/2, we have: EQUATION 6: VCM Error Analysis Equations: k k 1 = -----------1+ε k k 2 = ----------1–ε k k 1 + k 2 = -------------21–ε Where: k = ideal count (when VCM = VDD/2 exactly) ε = relative error (caused by VCM error) = = VCM_Error VCM = VCM_Error VDD – VCM VCM_Error VDD / 2 DS01016A-page 3 AN1016 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 the original error in k1. For example, reducing the k1 error to +2% gives a -0.04% error in (k1 + k2)/2. The extraction equations are below in Equation 7. These equations assume CSEN is constrained as described in Equation 3 and Equation 4. The constant, B1, is the circuit’s resolution in units of pF / count. RINT (see Figure 3) was chosen to make it easy to convert k1 and k2 into CSEN (making B1 = 0.100 pF / count). Pre-calculated Constant: VDD ⁄ 2 T CLK B1 = -------------------------- • ------------V RH – V RL RINT ( coded form ) The following assembly code modules (for the PIC16F690) make up the Humidity Sensor project: The HS1101LF humidity sensor from Humirel is described in detail in its data sheet [1]. It has a relative humidity (RH) accuracy of about ±2%, and its nominal capacitance ranges from 162 pF to 193 pF. 195 190 CSEN (pF) 185 180 175 170 165 160 20 30 40 The circuit and microcontroller need to be initialized. It is necessary to drive VSEN to a known point before starting the capacitance measurements; it could be either above or below VRL when starting. Modular Code HUMIDITY SENSOR 10 Additional Requirements Extra delay before starting each measurement improves the accuracy. It gives the op amp time to settle, and allows the firmware time to prepare for the next measurement. The firmware actually multiplies the sum (k1 + k2) by the pre-calculated constant B1/2. 0 This algorithm is implemented in the firmware for the Humidity Sensor PICtail™ Demo Board [15]. The firmware can be downloaded from Microchip’s website (www.microchip.com). Averaging (8 times) is included in this algorithm. The timer counts need to be converted to CSEN, then to RH. EQUATION 7: Extraction Equation: k1 + k 2 C SEN ≈ ----------------- • B 1 2 B1 ≈ ( k 1 + k 2 ) ⎛ ------⎞ , ⎝ 2⎠ FIRMWARE 50 60 RH (%) 70 80 90 100 FIGURE 4: HS1101LF Humidity Sensor’s Nominal Capacitance with TA = +25°C. • 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 Communications (SSC), a synchronous serial communications 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 web site (www.microchip.com); and are contained in the 00084R1.zip file. Since CSEN changes by about 31 pF across the full RH range, and has a nominal value of 180 pF, it follows that: • 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 DS01016A-page 4 © 2005 Microchip Technology Inc. AN1016 Top Level Algorithm Capacitance Module Figure 5 shows the flow chart for the top level program. This implementation includes averaging eight CSEN readings together. Table 2 shows the algorithm for the capacitance module, and includes the PICmicro microcontroller’s pin states. The pin assignments on the Humidity Sensor PICtail™ Demo Board [15] used for the measurements in this application note are: Main • • • • Initialize Call “capacitance.asm” Accumulate CSEN P1 = RC2 (VINT) P2 = RC1 (VSEN) P3 = RA4/T1G P4 = RC4/C2OUT Pin P2 is configured as the comparator’s input during the measurements. This gives the comparator time to settle before the measurements are made. TABLE 2: CAPACITANCE ALGORITHM Algorithm Steps Is count = 8 ? No Yes Divide Accumulated CSEN by 8 Call “humidity.asm” Pin States P1 P2 1 input 0 input 1 input Initialize VSEN (Note 1) (Move VSEN to < VRL) Set VREF to VRL Detect when VSEN < VRL Delay Positive VSEN Slope (Note 2) (Move VSEN from < VRL to > VRH) Start count k1 when VSEN = VRL Set VREF to VRH FIGURE 5: Top Level Flow Chart. The algorithm shown does not include any accuracy improvements. The user can add code to either correct the reference levels (VRL and VRH), using the internal ADC, or calibrate the entire circuit’s errors (including temperature drift). Stop count k1 when VSEN = VRH Delay Negative VSEN Slope (Move VSEN from > VRH to < VRL) Start count k2 when VSEN = VRH Set VREF to VRL Stop count k2 when VSEN = VRL Delay Note 1: 2: VRL = 0.125VDD and VRH = 0.500VDD. These are lower range levels in the PIC16F690’s VREF (CVREF). The counts k1 and k2 increment once for each PICmicro instruction cycle (TCLK = 0.5 μs) Relative Humidity Module Once CSEN has been calculated and averaged, the relative humidity (RH) for the HS1101LF sensor can be estimated. The conversion is accomplished using a piecewise linear interpolation table [11]. Appendix A: “Piecewise Linear Interpolation Table” contains details on the design of this table. © 2005 Microchip Technology Inc. DS01016A-page 5 AN1016 DESIGN MODIFICATIONS AND ALTERNATIVES Possible Modifications 100 nF SIMPLE MODIFICATIONS To produce different resolutions (pF / count), change the RINT value. It makes the code simpler when the resolution is a simple multiple of 1 pF. One possible set of 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 / count More than one resolution in the same circuit can be obtained by switching between several RINT resistors on the Printed Circuit Board (PCB). It is also possible to use multiple microcontroller pins, one for each RINT on the PCB. The RINT values not being used would have their pins set to hi-Z. The parasitic capacitance (CPAR) in parallel with CSEN is caused by board and op amp package stray capacitances. It is typically about 0.5 pF; the calculated CSEN should be corrected (have CPAR subtracted) by this amount. CPAR can be measured by leaving having CSEN open (0 pF). Many of the errors over relative humidity, supply voltage and temperature will be consistent over time. This makes it possble to calibrate out these errors; see “Error Analysis”. OP AMP INTEGRATOR WITH REDUCED CURRENT The circuit in Figure 6 achieves greater resolution by attenuating the square wave (VA). The components RA1, RA2 and RA3 form an attenuator with a DC bias point at VDD/2 and a gain of 0.0100 V/V. Thus, the current magnitudes IINTP and IINTM will be 100 times smaller than those produced by the circuit in Figure 3. This, in turn, produces longer integration times. This circuit has the following key performance numbers: • 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 tighter tolerances for RA1, RA2 and RA3; Figure 6 uses 1% resistors for low cost. VDD_DIG C1 P1 U1 PIC16F690 RA1 VA 1MΩ RA3 20 kΩ P4 Timer1 P2 P3 SR Latch Comparator VREF RA2 20 kΩ VSEN CSEN VINT RINT 6.65 MΩ IINT CCG C2 100 nF U2 MCP6291 CCM 100 nF VCM VDD RCM1 20 kΩ RCM2 20 kΩ FIGURE 6: Op Amp Integrator Circuit with Reduced Current. Note 1: CCG is the sensor’s case-to-ground parasitic capacitance. CCG should be placed at the op amp’s inverting input pin to improve the op amp’s stability and eliminate any dynamic current through CCG. 2: RINT is chosen to minimize the effort to calculate CSEN. Other Ciruits A quick overview of different methods to measure capacitance is found in AN990, “Analog Sensor Conditioning Circuits - An Overview” (AN990) [5]. Those designs include an R-C decay and an oscillator. The R-C decay method [6, 8] is very low cost and easy to implement. It is difficult to use this method for small capacitive sensors because of the microcontroller’s parasitic pin capacitance and leakage currents. It is quite popular to use a 555 timer and the capacitive sensor to form an oscillator circuit, which works well for larger capacitors. Smaller capacitors see greater inaccuracies due to the 555 timer’s parasitic pin capacitance and leakage currents. Also, 555 timers from different vendors behave quite differently. It is also possible to create other oscillator circuits [7]. They can be quite accurate with proper calibration, and they can be either simple or complicated. DS01016A-page 6 © 2005 Microchip Technology Inc. AN1016 ERROR ANALYSIS The design in this application note is accurate enough to make a detailed error analysis worth the effort. The dominant error sources are covered in this section. They will be covered in the same sequence they propagate through the circuit and algorithm. Their impact on RH accuracy, and possible improvements, will be sumarized at the end. Ratiometric Design The circuit was designed to be ratiometric. This is accomplished by making IINT, VCM, VRL and VRL proportional to VDD. Using a ratiometric design makes the measurement times independent of power supply voltage (VDD), eliminating one source of measurement error. Current (IINT) Imbalance When IINTP and IINTM are not equal, the timer counts, k1 and k2, are not equal. This causes an error (ε) in the calculated counts k1 and k2. “CSEN Extraction Equations” discusses this phenomenon in detail. Errors in VCM (VCM_Error in Equation 6) contribute to the current imbalance. The common mode voltage setting resistors (RCM1 and RCM2) and the op amp (U1) dominate the VCM errors. If RCM1 is 1% low and RCM2 is 1% high, then the relative error (ε) would would be +2%. If op amp U1’s 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 input bias current (IB_OA). This produces a relative error ε = IB_OA / IINT. This error is largest, for CMOS input op amps, at high temperatures. The attenuator in Figure 6 also causes a current (IINT) imbalance. A mismatch between RA2 and RA3 produces this current mismatch. Errors in Average Count (k) The relative error (ε) in k1 and k2 causes a smaller error of ε2 in the average count, k = (k1 + k2)/2; see “CSEN Extraction Equations”. Errors in the average count, k, are produced by the relative error in the following: • • • • VREF levels (VRH – VRL) RINT Comparator CMRR (change in offset vs. VSEN) Oscillator frequency The op amp’s gain-bandwidth product can have a significant effect on the errors for small CSEN values; see Equation 4. The smaller CSEN is, the larger this error is. Errors in Calculating CSEN The parasitic capacitance CPAR will cause an error of about 0.5 pF if no correction is made, and about ±0.1 pF if the correction is made. The nominal value of B1 is not exactly 0.1 pF / count; it is approximately 0.10025 pF / count. This error (+0.25%) has been designed to smaller than most errors. The designed circuit’s measurement resolution is 0.1 pF / count. The quantization error cannot be better than 1/2 this value (0.05 pF / count). Errors in Calculating RH “Humidity Sensor” gives basic information on the Humirel’s HS1101LF capacitive RH sensor. As explained there, the circuit in Figure 3 has a RH resolution of about 0.32% / count (3.2% / pF). Also, a 1% error in measuring CSEN produces a 6% RH error. In addition, there is a ±2% error in the nominal RH value, and a ±6% error due to temperature variations (at -40°C and +85°C). RH is calculated from CSEN using a piece-wise linear (PWL) lookup table [11]. This table has been designed to make the firmware simple and quick by using 64 lookup table rows. This has the added benefit of producing a very accurate estimate of RH (better than ±0.01% error). Overview of Errors Table 3 includes all of the errors mentioned in this section. These errors are at room temperature (+25°C). It also shows how the errors propagate through the circuit 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 • • • • Note that when we subtract VRL from VRH, the comparator’s offset voltage is cancelled (because it is constant). © 2005 Microchip Technology Inc. DS01016A-page 7 AN1016 TABLE 3: ERROR ANALYSIS AT ROOM TEMPERATURE Error Sources Special Conditions Cause Effects Worst Case Imbalance ε Error in k Error Error in CSEN Error in RH (Note 2) (Note 3) (Note 1) Current (IINT) Imbalance RCM1 and RCM2 — ±1% ±2.0% VOS_OA — ±3 mV ±0.2% IB_OA (Note 4) — ≈ 20 pA ±0.004% ±0.05% ±0.05% ±0.3% 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 fGBWP internal ±1% — ±1% ±1% ±6% external ±0.01% — ±0.01% ±0.01% ±0.1% for HS1101LF –0.002% — –0.002% –0.002% –0.01% at CSEN = 0.6 pF –0.5% — –0.5% –0.5% N/A not corrected 0.5 pF — — 0.5 pF 1.6% Calculating CSEN CPAR 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% HS1101LF, Nominal Error — ±3% — — — ±3% HS1101LF, Drift per Year — ±1% — — — ±1% PWL Lookup Table — ±0.01% — — — ±0.01% Calculating RH Note 1: 2: 3: 4: 5: It is assumed that VDD is at its lowest value (3.0V for this design) when converting errors in mV to percentage errors. The error in VCM is given as a percentage of VDD/2, which is the same as the relative error ε. 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. This error becomes bigger as the ambient temperature increases. At +125°C, IB_OA’s contribution to the imbalance ε is ±5 nA or ±1.1%, causing the RH error to be ±0.6%. 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. Possible Improvements The VREF levels (VRH and VRL) can be corrected, in some microcontrollers, by internally connecting an ADC to the VREF output. Since the ADC has better accuracy than the VREF ladder, the measurement can be improved. Most of the remaining errors can be corrected with appropriate calibration procedures and calculations. The calculated RH can be corrected for errors across temperature; both the sensor and the circuit can be calibrated at the same time. Once the decision is made to calibrate the errors, there is no need to correct the VREF levels using the internal ADC. Components with tighter tolerance will directly improve the RH accuracy. The resistor, RINT, and the oscillator are two important examples. DS01016A-page 8 © 2005 Microchip Technology Inc. AN1016 MEASURED RESULTS Fine Resolution Measurements The basic circuits in Figure 3 and Figure 6 were measured with different RINT values. First, known capacitors were measured to validate the accuracy of these designs. Then the HS1101LF relative humidity sensor was measured and compared to another, calibrated humidity sensor. The circuit in Figure 6 was measured first; it has the 40 dB attenuation of the square wave. This made it possible to measure the parasitic capacitance, CPAR, and other small capacitances. The CSEN resolution is 0.001 pF / count. The measurements were made on the Humidity Sensor PICtail™ Demo Board, which is discussed in the user’s guide (DS51594) [15]. This board is further described by: Figure 7 shows the VSEN waveform across time. T1G is the Timer 1 Gate waveform; it shows when the comparator decides that VSEN has reached either VRL or VRH. CSEN was 166.0 pF and RINT was 6.65 MΩ. • Order Number: PIC16F690DM-PCTLHS • Assembly Number: 102-00084R1 VINT and VSEN Waveforms with CSEN = 166 pF. Figure 8 shows the measurement discrepancy between the values read from an HP4285A LCR meter and the circuit in Figure 6. These measurements were taken across a range of allowed CSEN values. The parasitic capacitance CPAR was measured by leaving CSEN open; the value for the Humidity Sensor PICtail™ Demo Board [15] turned out to be approximately 0.27 pF. CSEN Measurement Discrepancy FIGURE 7: 10% 8% Modified Circuit 6% Not Corrected 4% 2% 0% -2% Corrected: -4% CPAR = 0.270 pF -6% B1 = 0.09499 pF / count -8% -10% 1p 10p 1.E+00 1.E+01 CSEN (F) FIGURE 8: Discrepancy. © 2005 Microchip Technology Inc. 100p 1.E+02 CSEN Measurement DS01016A-page 9 AN1016 LESSONS LEARNED The circuit in Figure 3 does not attenuate the square wave, making it possible to measure larger capacitors. The CSEN resolution is 0.1 pF / count. Several important lessons were learned in the process of building, measuring and debugging this design. Figure 9 shows the measurement discrepancy between the values read from an HP4285A LCR meter and the circuit in Figure 3. These measurements were taken across a range of allowed CSEN values. Ratiometric Design CSEN Measurement Discrepancy Normal Resolution Measurements 10% 8% 6% 4% 2% 0% -2% -4% -6% -8% -10% 1p 1.E+00 • 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) Not Corrected Corrected: CPAR = 0.270 pF B1 = 0.09987 pF / count 10p 1.E+01 FIGURE 9: Discrepancy. 100p 1.E+02 CSEN (F) 1n 1.E+03 10n 1.E+04 HS1101LF Sensor Measurements The circuit in Figure 3 was used to measure the HS1101LF relative humidity sensor. The measurement resolution is 0.1 pF / count (0.6% RH change per count) and RINT is 6.65 MΩ. The curves in Figure 10 show how the sensor reacted when it was breathed on for about half second; the result is the impulse response of the sensor. 190 18% 185 16% 180 14% RH 175 12% CSEN 170 10% 165 8% -5 0 5 10 15 20 25 30 35 40 45 Time (s) FIGURE 10: Response. DS01016A-page 10 Relative Humidity Sensor Capacitance (pF) 20% breathed on sensor • The lower CVREF range is more accurate than the upper range (±1.04% vs. ±1.56%). • The comparator’s 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 amp’s 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.125VDD and VRH = 0.500VDD in the lower CVREF range. 22% 195 Reference Voltages Chosen The reference voltages (VRH and VRL) need to be selected carefully. The analog components need to stay within their allowed ranges, but VRH and VRL need to be as far apart as possible for accuracy reasons: CSEN Measurement 200 This design assumes VDD ranges from 3.0V to 5.5V (e.g., two lithium batteries). To avoid supply rejection errors, and to make the design simpler to implement, a ratiometric approach was very helpful. The implementation is as follows: Choosing the Microcontroller The PIC16F690 has several key features that help this design: • 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. HS1101LF Impulse © 2005 Microchip Technology Inc. AN1016 Calibration Capacitive relative humidity sensors are quite sensitive to capacitive measurement errors. It can be costly to use components with tighter tolerances. The designs in this application note are a big help because they have low parasitic capacitance in parallel with the sensor, and they are immune to sensor case to ground capacitances. The most effective way to overcome the sensitivity problem is to calibrate the errors in production. All of the errors can be corrected in the same step. Miscellaneous Using dual slope integration minimizes errors due to the VCM error (IINT imbalance). The op amp’s gain-bandwidth product affects the relative error for small CSEN values. Select an op amp that gives an acceptable error at the smallest CSEN value that will be measured. RINT is selected last to make the code simpler to implement. It is chosen so that B1 becomes a convenient number (e.g., 0.1 pF / count). It is important to use a greater number of bits for the averaging (accumulation) of CSEN. The counter in this design is a 16-bit value, and the accumulator is a 24-bit value. This avoids truncation errors, and can prevent counter results being interpreted as negative values. SUMMARY This application note shows hardware and firmware design engineers how to use a PICmicro® microcontroller and an op amp integrator to accurately measure small capacitances. Simple firmware is included that produces capacitance and relative humidity values for the HS1101LF relative humidity sensor. Measurements verify the theory and design choices. An error analysis points to possible improvements in the design. Other alternatives and modifications to this design are also covered. Key lessons learned help the user focus on the important aspects of the given design. The references and appendices give detailed information that supports the text of this application note. 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 Oscillator Circuits For Sensor Applications”, Jim Lepkowski; Microchip Technology Inc., DS00866, 2003. [5] AN990, “Analog Sensor Conditioning Circuits – An Overview”, Kumen Blake, Microchip Technology Inc., DS00990, 2005. [6] AN512, “Implementing Ohmmeter/Temperature Sensor”, Doug Cox; Microchip Technology Inc., DS00512, 1997. [7] AN895 “Oscillator Circuits For RTD Temperature Sensors”, Ezana Haile and Jim Lepkowski; Microchip Technology Inc., DS00895, 2004. Capacitive Sensors [8] AN611, “Resistance and Capacitance Meter Using a PIC16C622”, Rodger Richey; Microchip Technology Inc., DS00611, 1997. [9] Ricardo Jimenez and Claudia Angulo, “Measure Relative Humidity With a PIC® MCU”, Design Briefs, Electronic Design, March 17, 2005, p. 7980. [10] AN1014, “Measuring Small Changes in Capacitive 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 and Steven Bible; Microchip Technology Inc., 2004. [12] MCP6291/2/3/4/5 Data Sheet, “1.0 mA, 10 MHz Rail-to-Rail Op Amp,” Microchip Technology Inc., DS21812, 2004. [13] PIC16F685/687/689/690 Data Sheet, “20-Pin Flash-Based, 8-Bit CMOS Microcontrollers with nanoWatt Technology”, Microchip Technology Inc., DS41262, 2005. [14] PIC16C745/765 Data Sheet, “8-Bit CMOS Microcontrollers with USB”, Microchip Technology Inc., DS41124, 2000. [15] “Humidity Sensor PICtail™ Demo Board User’s Guide”, Microchip Technology Inc., DS51594, 2005. © 2005 Microchip Technology Inc. DS01016A-page 11 AN1016 APPENDIX A: PIECEWISE LINEAR INTERPOLATION TABLE In order to convert CSEN to RH, the firmware uses a piecewise linear interpolation table [11]. This table is designed specifically for Humirel’s HS1101LF relative humidity sensor [1]. First, the calculated CSEN value is limited in range and converted to a 10-bit integer: EQUATION A-1: CSN = 0, = 1023, C SEN < 130.0 pF C SEN > 232.3 pF C SEN – 130.0 pF = ----------------------------------------, otherwise 0.1 pF 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 and 15) are the input to the linear interpolation formula using the coefficients for the current row. The entries in the table give errors less than ±0.01%. EQUATION A-2: X = A RHO + A RH1 ( CSN – ACSN ) RH = max { min { X, 100% }, 0% } DS01016A-page 12 TABLE A-1: HS1101LF COEFFICIENT LOOKUP TABLE ACSN ARH0 (%) ARH1 (% / LSb) 0 0 0 0 1 16 0 0 ··· ··· ··· ··· 17 272 0 0 18 288 0 0 19 304 -3.08 0.250 20 320 0.92 0.259 21 336 5.06 0.267 22 352 9.33 0.276 23 368 13.75 0.285 24 384 18.31 0.294 25 400 23.00 0.302 26 416 27.84 0.310 27 432 32.80 0.318 28 448 37.89 0.325 29 464 43.09 0.332 30 480 48.40 0.337 31 496 53.79 0.341 32 512 59.26 0.345 33 528 64.77 0.347 34 544 70.32 0.347 35 560 75.88 0.346 36 576 81.41 0.344 37 592 86.91 0.339 38 608 92.33 0.332 39 624 97.64 0.325 40 640 100 0 41 656 100 0 ··· ··· ··· ··· INTRVL 62 992 100 0 63 1008 100 0 © 2005 Microchip Technology Inc. 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 Microchip’s 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 our products. Attempts to break Microchip’s code protection feature may be a violation of the Digital Millennium Copyright Act. If such acts allow unauthorized access to your software or other copyrighted work, you may have a right to sue for relief under that Act. Information contained in this publication regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip’s products as critical components in life support systems is not authorized except with express written approval by Microchip. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights. 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. 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 Company’s 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, Microchip’s quality system for the design and manufacture of development systems is ISO 9001:2000 certified. © 2005 Microchip Technology Inc. DS01016A-page 13 WORLDWIDE SALES AND SERVICE AMERICAS ASIA/PACIFIC ASIA/PACIFIC EUROPE Corporate Office 2355 West Chandler Blvd. Chandler, AZ 85224-6199 Tel: 480-792-7200 Fax: 480-792-7277 Technical Support: http://support.microchip.com Web Address: www.microchip.com Australia - Sydney Tel: 61-2-9868-6733 Fax: 61-2-9868-6755 India - Bangalore Tel: 91-80-2229-0061 Fax: 91-80-2229-0062 China - Beijing Tel: 86-10-8528-2100 Fax: 86-10-8528-2104 India - New Delhi Tel: 91-11-5160-8631 Fax: 91-11-5160-8632 Austria - Wels Tel: 43-7242-2244-399 Fax: 43-7242-2244-393 Denmark - Copenhagen Tel: 45-4450-2828 Fax: 45-4485-2829 China - Chengdu Tel: 86-28-8676-6200 Fax: 86-28-8676-6599 India - Pune Tel: 91-20-2566-1512 Fax: 91-20-2566-1513 France - Paris Tel: 33-1-69-53-63-20 Fax: 33-1-69-30-90-79 China - Fuzhou Tel: 86-591-8750-3506 Fax: 86-591-8750-3521 Japan - Yokohama Tel: 81-45-471- 6166 Fax: 81-45-471-6122 Germany - Munich Tel: 49-89-627-144-0 Fax: 49-89-627-144-44 China - Hong Kong SAR Tel: 852-2401-1200 Fax: 852-2401-3431 Korea - Gumi Tel: 82-54-473-4301 Fax: 82-54-473-4302 China - Qingdao Tel: 86-532-8502-7355 Fax: 86-532-8502-7205 Korea - Seoul Tel: 82-2-554-7200 Fax: 82-2-558-5932 or 82-2-558-5934 Atlanta Alpharetta, GA Tel: 770-640-0034 Fax: 770-640-0307 Boston Westborough, MA Tel: 774-760-0087 Fax: 774-760-0088 Chicago Itasca, IL Tel: 630-285-0071 Fax: 630-285-0075 Dallas Addison, TX Tel: 972-818-7423 Fax: 972-818-2924 Detroit Farmington Hills, MI Tel: 248-538-2250 Fax: 248-538-2260 Kokomo Kokomo, IN Tel: 765-864-8360 Fax: 765-864-8387 Los Angeles Mission Viejo, CA Tel: 949-462-9523 Fax: 949-462-9608 San Jose Mountain View, CA Tel: 650-215-1444 Fax: 650-961-0286 Toronto Mississauga, Ontario, Canada Tel: 905-673-0699 Fax: 905-673-6509 China - Shanghai Tel: 86-21-5407-5533 Fax: 86-21-5407-5066 China - Shenyang Tel: 86-24-2334-2829 Fax: 86-24-2334-2393 China - Shenzhen Tel: 86-755-8203-2660 Fax: 86-755-8203-1760 China - Shunde Tel: 86-757-2839-5507 Fax: 86-757-2839-5571 China - Wuhan Tel: 86-27-5980-5300 Fax: 86-27-5980-5118 China - Xian Tel: 86-29-8833-7250 Fax: 86-29-8833-7256 Malaysia - Penang Tel: 60-4-646-8870 Fax: 60-4-646-5086 Philippines - Manila Tel: 63-2-634-9065 Fax: 63-2-634-9069 Italy - Milan Tel: 39-0331-742611 Fax: 39-0331-466781 Netherlands - Drunen Tel: 31-416-690399 Fax: 31-416-690340 Spain - Madrid Tel: 34-91-708-08-90 Fax: 34-91-708-08-91 UK - Wokingham Tel: 44-118-921-5869 Fax: 44-118-921-5820 Singapore Tel: 65-6334-8870 Fax: 65-6334-8850 Taiwan - Hsin Chu Tel: 886-3-572-9526 Fax: 886-3-572-6459 Taiwan - Kaohsiung Tel: 886-7-536-4818 Fax: 886-7-536-4803 Taiwan - Taipei Tel: 886-2-2500-6610 Fax: 886-2-2508-0102 Thailand - Bangkok Tel: 66-2-694-1351 Fax: 66-2-694-1350 10/31/05 DS01016A-page 14 © 2005 Microchip Technology Inc.