# Measuring capacitance by measuring rising time

I have built a circuit to measure a capacitor's capacitance using the τ = R·C formula (see schematic below).

simulate this circuit – Schematic created using CircuitLab

When I open the switch (which is normally closed,) I want the capacitor to charge. When the voltage across said capacitor reaches ~0.67·Vsupply the op-amp output goes from low to high.

My issue is that I don't get the right voltage accross the capacitor when opening the switch (800-900 mV at most.)

Is it because I'm missing a resistor at the base of the transistor? When looking at examples of transistors used for switching, they say to adapt the resistor's value to the desired current at the emitter. Since the branch is only connected to the op-amp's (+) input I'm under the impression that the current is near 0 mA so I have trouble coming up with a resistor value that isn't totally random.

EDIT: I modified my circuit according to Mattman944's answer. Now the circuit is a lot more repeatable than before, every time I flick the switch I measure the same time before reading a rising edge on the output. The only downside to this is that the time between opening the switch and reading the rising edge has changed drastically.

Using a 22 nF capacitor, I would usually measure a difference in time of 22-28 ms. By using the formula τ=R.C => C=τ/R = 22-28 nF, which is close to the real value (though not repeatable at all.) Now the time between flicking the switch and reading a rising edge on the output has decreased to 84 μs, thus supposedly measuring a capacity of 84 pF instead of 22 nF.

Could it be possible that changing the circuit the way I did may have changed the way I'm supposed to convert elapsed time into capacitance?

• I'd change the transistor to a NPN. Emitter to ground, collector above the cap, and base to V+ trough the switch and a base resistor. Also add a pull-down on the base to make sure the transistor stays off when the switch is open. Commented Aug 26, 2022 at 8:11
• I'll try this right away, why change from PNP to NPN though ? Is there a difference other than the polarity needed to switch them between both types ? Commented Aug 26, 2022 at 8:15
• With the setup I propose you can drive it into saturation, reducing C-E drop even further. With the PNP that 1 M resistor is severely restricting base current. Note that this is only for small caps. Shorting a large cap trough a transistor with nu current limit will pop the transistor. Commented Aug 26, 2022 at 8:17
• Definitely use a MOSFET, as per @Mattman944 answer. But switch contacts bounce will make a real circuit more inaccurate as the final bounce may not be for long enough to discharge a larger capacitor. If you're only doing this in simulation, you might not see a problem that a real circuit would. Commented Aug 26, 2022 at 8:57
• There are much simpler ways to measure the capacitance of a capacitor.... Commented Aug 26, 2022 at 17:06

The PNP doesn't work well because to get the E-B to conduct, you need a voltage less than zero at the Emitter, so you can't discharge all the way.

An NPN will work better, but still may not discharge all the way to zero. A better way it to use a small N-channel MOSFET, it will discharge the cap all the way to zero.

For large caps, put a small resistor in series with the collector/drain.

simulate this circuit – Schematic created using CircuitLab

Edit: I built the MOSFET circuit, my V+ is 5V. Using a DMM, my 1M resistor measures 0.977M, the cap measures 21.5 nF, giving a time constant of 21.0 mS.

The voltage at one time constant is 63.2%, or 3.16 V. I used the scope cursors to measure the time to get to 3.16 V, it was 21.4 mS. Not exact, but well within the measurement error of my setup.

• Thank you for your concise answer, would you mind the edit to my question regarding the consequences of the modifications you proposed ? Commented Aug 26, 2022 at 10:12
• How are you measuring the output transition? A scope? MCU? If you are using an MCU, then control the gate with a GPIO to eliminate switch bounce. Commented Aug 26, 2022 at 13:19
• TIL another difference between BJTs and FETs Commented Aug 26, 2022 at 16:28
• @Mattman944 I'm currently using an oscilloscope to measure the output, though I plan on using a microcontroller later on when I fix a few issues coming from the set of MCUs I have available. I will keep your advice in mind when the time comes. Commented Aug 29, 2022 at 6:40

It is somewhat easier to make the measurement repeatedly, and then average the results. Thus, you'd probably want a multivibrator that continuously charges and discharges the capacitor between two limit voltages.

The circuit below uses an LM393, LM339, LM2901 or any other open-collector/open-drain comparator for this purpose. The components within the dashed line implement an LM393 model in the simulation only.

Note that LM393's input voltage range spans 0V to VCC-1.5V, so 0..3.5V when powered from 5V.

The circuit will also work with a comparator with a rail-to-rail push-pull output - in that case, remove R5. If the comparator also has rail-to-rail input range, then R2 and R3 should be 47k, and R4 has to be adjusted for a reasonable operating voltage range.

simulate this circuit – Schematic created using CircuitLab

The capacitor and output voltages look as follows:

With open-collector-output comparators, the threshold voltages can be made more accurate by adding a common-emitter buffer stage that swings almost rail-to-rail.

simulate this circuit

The capacitor and output voltages look as follows:

There is a multitude ways to set up capacitor measurement circuits, of course. An approach similar to yours, but using a constant current (vs. a constant voltage through a resistor), converts capacitance directly to voltage:

1. The capacitor is connected as a timing element in a triangle wave generator. The reference ("amplitude") current source used in the generator is voltage controlled (a VCCS).
2. The triangle wave is differenced to yield a square wave with amplitude proportional to the slope of the triangle wave.
3. The square wave is rectified.
4. The rectifier output is regulated to a constant voltage by closing the loop back to the first stage's VCCS. This fixes the slope of the triangle wave. The larger the capacitor, the proportionally larger current it takes to obtain this fixed slope.

The current source control voltage is thus directly proportional to capacitance, and constitutes the output of the circuit.