STM32F0 Capacitance meter!

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.

A pretty simple circuit I designed in LTSpice to produce two riding edges at different specified times in the charge cycle.

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: 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.

Testing out the first capacitor

Voila! A reported capacitance of 91.8nF! The capacitor doesn’t state a tolerance but its within 10% regardless.

Testing the 1nF ceramic capacitor.

Another good reading! 1.03nF is once again within 10%.

Testing the 680nF polycap.

Another good result! 641nF.

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.

555CircuitSimulation of what is going on with the 555 timer side of the design.

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!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s