{"id":3373,"date":"2019-08-07T18:53:33","date_gmt":"2019-08-07T16:53:33","guid":{"rendered":"https:\/\/deepbluembedded.com\/?p=3373"},"modified":"2023-08-17T23:52:35","modified_gmt":"2023-08-17T20:52:35","slug":"how-to-measure-capacitor-digital-capacitance-meter-project","status":"publish","type":"post","link":"https:\/\/deepbluembedded.com\/how-to-measure-capacitor-digital-capacitance-meter-project\/","title":{"rendered":"Digital Capacitance Meter | How To Measure A Capacitor With Microcontroller?"},"content":{"rendered":"

\"Digital<\/p>\n

Digital Capacitance Meter<\/strong><\/h3>\n

How To Measure A Capacitor With A Microcontroller? <\/strong><\/h3>\n

This project shows you how to measure the capacitance of a capacitor with a microcontroller using the analog to digital converter & timer modules. The effective measurement range for the digital capacitance meter we’ll be building is from 1nF to 100uF and the resolution about 0.5nF to a few hundreds at the high end of the range.<\/span><\/p>\n

You’ll learn all the basics of this interesting topic and how to pick the design parameters to meet your needs. In case you need a higher measurement range or more accuracy or whatever. You’ll get to know which parameters affect which property and how to decide on these trade-offs. Let’s now get started!\u00a0<\/span><\/p>\n

Required Components For This Project<\/span><\/strong><\/p>\n\n\n\n\n\n\n\n\n\n\n\n\n
Qty.<\/strong><\/span><\/td>\nComponent Name<\/strong><\/span><\/td>\nBuy<\/strong> On Amazon.com<\/span><\/td>\n<\/tr>\n
1<\/td>\nPIC16F877A <\/span><\/p>\n

or PIC18F2550<\/span><\/td>\n

Add<\/a><\/p>\n

Add<\/a><\/td>\n<\/tr>\n

1<\/td>\nBreadBoard<\/span><\/td>\nAdd<\/a><\/td>\n<\/tr>\n
1<\/td>\nAlphanumeric LCD 16×2<\/span><\/td>\nAdd<\/a><\/td>\n<\/tr>\n
1<\/td>\nResistors Kit<\/span><\/td>\nAdd<\/a>\u00a0 \u00a0\u00a0Add<\/a><\/td>\n<\/tr>\n
1<\/td>\nCapacitors Kit<\/span><\/td>\nAdd<\/a><\/td>\n<\/tr>\n
1<\/td>\nJumper Wires Pack<\/span><\/td>\nAdd<\/a>\u00a0 \u00a0 Add<\/a><\/td>\n<\/tr>\n
1<\/td>\nLM7805 Voltage Regulator (5v)<\/span><\/td>\nAdd<\/a><\/td>\n<\/tr>\n
1<\/td>\n8MHz Crystal Oscillator<\/span><\/td>\nAdd<\/a><\/td>\n<\/tr>\n
1<\/td>\nPICkit2 or 3 Programmer<\/span><\/td>\nAdd<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\n

1 Theoretical Background<\/strong><\/h3>\n

The working principle of a digital capacitance meter relies on the fact that a capacitor follows a specific equation (formula) during the charging and discharging process. Hence, we can apply a voltage through a known resistor R<\/strong> and monitor how long does it take a capacitor to hit a specific voltage threshold, usually expressed in terms of tau \u03c4<\/strong> which is commonly known as the Time Constant for capacitor charging and discharging (where \u03c4 = R x C).<\/span><\/p>\n

\"How<\/p>\n

The time it takes a capacitor’s voltage Vc<\/strong> to reach 63.2% from the supply voltage is equal to one time constant (\u03c4 which is also equal to R x C). Provided that we’ll be selecting a convenient R<\/strong> for this process, the only unknown to solve for is the capacitance of the inserted capacitor C<\/strong>. And thereafter, it’s a straight-forward process to find it out.<\/span><\/p>\n

To conclude this, a digital capacitance meter applies a fixed voltage source Vs<\/strong> to the unknown (to be measured) capacitor C<\/strong> through a fixed known resistor R<\/strong>. And it measures the time it takes the voltage across the capacitor Vc<\/strong> to build up from 0v to a specific threshold voltage (i.e at 1\u03c4, 2\u03c4 or even fractions like 1.5\u03c4). Assuming a voltage threshold of 0.632Vs, then the measured time is equal to 1\u03c4 which is equal to = R x C. Given the value of R, we can solve for C and print out the capacitance value!<\/span><\/p>\n

 <\/p>\n


\n

2 Using ADC (Method1)<\/strong><\/h3>\n

2.1 Basic Idea<\/strong><\/h4>\n

\"Digital<\/p>\n

This method utilizes the analog to digital converter (ADC) module inside microcontrollers to measure and monitor the voltage across the unknown capacitor C<\/strong> (to be measured). A couple of io pins are used as well, one for the start measurement input signal of a push button. And another one (pin 1) for charging the capacitor through a fixed value resistance R (usually large enough to get slow charging curve). And the last io pin is pin3 and it’s used to accelerating the discharging process (through small resistor 330\u03a9). As you might have noticed, the capacitor has to be fully discharged before initiating a measurement process.<\/span><\/p>\n

When both (pin1 & 2) are LOW, during the discharging phase, the equivalent resistance through which the capacitor is discharging drops dramatically as it’s the parallel net resistance of (330\u03a9 and R). Resulting in an incredibly fast discharging process. You can use only 1 pin however, the same pin1 using only R also for discharging but it’s gonna take sooo long to fully discharge (it’s 5tau = 5\u03c4 = 5*R*C).<\/span><\/p>\n

The Procedure is As Follows<\/strong><\/p>\n

1 The Microcontroller Starts A Timer Module in Timer Mode<\/span><\/p>\n

2 The ADC will start running in the free-running mode (Auto-Trigger Mode) for continuous successive conversions. Until the measured voltage is larger than or equal to 3.16v (63.2% of 5v).\u00a0<\/span><\/p>\n

3 The MCU Stops The Timer And The ADC. Save the time (which is equal to tau \u03c4)<\/span><\/p>\n

4 Using the time constant we’ve measured in the last step, it’s easy to solve for the capacitance value C<\/strong> using this formula ( \u03c4 = R x C ).<\/span><\/p>\n

These steps are repeated whenever the user presses the START push button.<\/span><\/p>\n

2.2 Calculations<\/strong><\/h4>\n

Picking The Resistor (R)<\/strong><\/p>\n

It doesn’t seem so tricky but it actually is! The value of the R resistor which we’ll be using for charging the unknown capacitor, somehow, controls a variety of other parameters. First of which is the time constant (tau \u03c4 = R x C). The smaller R is, the smaller tau is and the faster it’s gonna take the voltage across the capacitor to hit the 63.2% voltage threshold. Well, where is the problem in that?<\/span><\/p>\n

Obviously, the ADC conversion rate is rather limited by device specifications and moreover, the CPU takes time to service ADC interrupts to compare the current voltage level of Vc whether it’s below 63.2% (3.16v) or not. Hence, we might miss (overshoot) the target voltage threshold and get a wrong measure for tau and consequently wrong capacitance value.<\/span><\/p>\n

Add to that, a large resistance for R will result in a significant increase in tau and the charing time. Yeilding a very long time required to measure a capacitor. For example, consider R=100k\u03a9, and you’re willing to measure a capacitor C=1000\u03bcF. Do you know how long it’s gonna take to complete the measurement process? Well, it’s also 1tau (which is = R x C). That’s a complete 100 seconds!<\/span><\/p>\n

For this project, we’ll stick to R=100k\u03a9. And limiting the C measurement range up to 10\u03bcF, at which the measurement time is a maximum of 1 second. That’s acceptable for now!<\/span><\/p>\n

2.3 Code Snippets<\/strong><\/h4>\n

The main routine will be something like this<\/p>\n

void main(void) {\r\n  IO_Init();\r\n  ADC_Init();\r\n  LCD_Init();\r\n  LCD_Clear();\r\n  TMR1_Init();\r\n  while(1) \r\n  {\r\n    if(Start_Btn)\r\n    {\r\n      TMR1 = 0x0000;\r\n      TMR1_C = 0;\r\n      Control_PIN = 1; \/\/ Charge Through R\r\n      Discharge_D = 1; \/\/ The Small Resistor is disconnected (High-Impedance or Z)\r\n                       \/\/ Setting The Pin To Input Makes It High-Impedance (disconnected)\r\n      TMR1ON = 1;      \/\/ Start Timer1\r\n      GO_nDONE = 1;    \/\/ Start ADC\r\n    }\r\n    \/\/ Update The LCD Display\r\n    Update_Display();\r\n    __delay_ms(100);\r\n  }\r\n  return;\r\n}<\/pre>\n

Calculating the time constant (tau) is done in the ISR (interrupts service routine) whenever the ADC finds out that the Vc has crossed the 3.16v voltage threshold. Afterward, the unknown capacitance is easily calculated by dividing the resultant tau by R (the 100k resistor). And the result will be printed on the LCD inside the update_display() function. The full code listing and testing are provided at the end of this article.<\/span><\/p>\n

 <\/p>\n


\n

3 Using Analog Comparator (Method2)<\/strong><\/h3>\n

3.1 Basic Idea<\/strong><\/h4>\n

This method utilized the analog comparator module within a microcontroller to perform voltage comparison to monitor the capacitor charging voltage. And when the voltage across the capacitor crosses a specific threshold, the comparator’s output bit changes which fires an interrupt!<\/span><\/p>\n

We’ll also use a timer module to measure the time between the beginning of the charging process and when a comparator change interrupt occurs. Then we’ll calculate the capacitance using time constant formula as in the first method of ADC.<\/span><\/p>\n

[ Further Explanation For This Method Will Be in Another Article .. Coming Soon ]<\/span><\/p>\n

 <\/p>\n


\n

4 Implementing A Capacitance Meter System<\/strong><\/h3>\n

For this project, we’ll be implementing the ADC-based measurement system. With PIC18F2550 microcontroller, R=100k, and Alphanumeric 16×2 LCD module for display.<\/span><\/p>\n

Check these tutorials as a reference<\/p>\n