Yet another project with my STM32F0 discovery board… A simple capacitance meter! The value of capacitance can be determined by the time that it takes for a capacitor to charge through the resistor. A precise way to do this would involve using two comparators, 4 precision resistors (three for a precise biasing source and one to be used as a charge resistor), and a voltage regulator. By using a precise DC source and setting the comparison points at perfect intervals would give a much better result than the method I’m using. As I threw this circuit together in a few minutes, I’m using a 555 timer in monostable mode. The internals of a 555 timer actually involve two comparators and a few resistors to produce multiple comparison voltage levels.
The 555 timer just makes it easier than the above circuit by outputting a pulse of the duration required for the capacitor to charge from 0V to 2/3Vcc. The standard equation for charging a capacitor through a resistor is: Vo = Vs(1-e^(-t/RC)) (Source: http://en.wikipedia.org/wiki/RC_circuit though easily derivable with some laplace transforms!). Knowing that the output is high for the time it takes to charge to 2/3Vcc and dividing Vcc from both sides gives: 2/3 = 1-e^(-t/RC). Rearranging this for C gives: -t/(R*ln(1-2/3)) = C. Doing this calculation on the STM32F0 is easy and could be implemented in fully floating point math without the need of the math library. It becomes easier however if the math library is included as it makes understanding how the calculation works much easier as opposed to having some random constant present in the calculation!
The results that I’ve got so far seem pretty accurate too. I’ve tested a 680nF poly-something cap, a 100nF polyester film capacitor and a 1nF ceramic capacitor. Note, the breadboard actually adds a fair few pF of stray capacitance throwing a constant error into all of my measurements.
Obviously, this method is flawed by the precision of the charge resistor, power supply stability, timer clock drift and processing time but the results should at least be usable enough for merely detecting the round about value of the capacitor.
As can be seen in the simulation above, the 555 timer charges the capacitor up to 2/3Vcc. During this time, the output is high. The time will be proportional to the size of the capacitor and with the previously derived expression, the value of capacitance can be determined.
Reading the capacitance with no capacitor in also gives a result of 576pF. Hardly good for any precision measurements! If larger valued capacitors would like to be read, the prescaler should be increased to ensure the timer doesn’t overflow. In my program, I haven’t included an overflow management section of code so if you use my code, you’ve been warned! In my example, the trigger is connected to PA0 (TIM2 channel 1) and the pulse output is connected to PA1 (TIM2 channel 2).
You can find my code on Github!