Capacitive sensor technology is everywhere now — ranging from touch-sensitive buttons and humidity sensors to screens with multi-touch detection.
In most cases, ready-made modules are used for electronics applications. Development with I²C, PWM and the like is convenient after all and hides the complexity of measuring electrical capacitance. Or rather, as I would like to show in this post:
Physical Background#
Charging a capacitor#
The voltage of a capacitor connected to a DC voltage source can be calculated using this formula:
$$ u_{rel}(t) = 1 - e^{-\frac{t}{\tau}} $$
Here, \( \tau := R \cdot C \) is referred to as the time constant of the RC circuit. In this equation, the capacitance \( C \) is the only unknown variable. The charging resistance \( R \) is predefined, while the relative capacitor voltage \( u_{rel} \) and elapsed time \( t \) can be measured.
The relative capacitor voltage is defined as the ratio of absolute capacitor voltage to supply voltage. A value of \( u_{rel}(t) = 50\% \) therefore means that the capacitor has reached half of the supply voltage.
In order to minimize amplification of measurement errors, it is recommended to start the measurement after \( u_{rel}(t) \ge 50\% \).
This corresponds to approximately \( t \ge 0.7 \cdot \tau \).
It should however be noted that the measurement should not be performed too late.
From when \( t = 2 \cdot \tau \), changes to \( u_{rel} \) only occur very slowly, so the exact time can easily be disturbed by external interference.
Moreover, it is always beneficial to measure as quickly as possible.
The equation of the charging graph can be solved for the time, \( t = m_C \cdot \tau \):
$$ \begin{align*} u_{rel}(t) &= 1 - e^{-\frac{t}{\tau}} \\ e^{-\frac{t}{\tau}} &= 1 - u_{rel}(t) \\ -\frac{t}{\tau} &= \ln(1 - u_{rel}(t)) \\ t = \boxed{m_C} \cdot \tau &= \boxed{- \ln(1 - u_{rel}(t))} \cdot \tau \\ \Rightarrow m_C(u_{rel}) &= - \ln(1 - u_{rel}) \end{align*} $$
Discharging a capacitor#
Just as when charging a capacitor, there is also a close relationship between the voltage progression and the time constant while discharging:
$$ u_{rel}(t) = e^{-\frac{t}{\tau}} $$
Due to the close relationship between the two functions, it is also recommended to perform the measurement between \( t = 0.7 \cdot \tau \) and \( t = 2 \cdot \tau \).
This equation can also be solved for the time \( t = m_D \cdot \tau \):
$$ \begin{align*} u_{rel}(t) &= e^{-\frac{t}{\tau}} \\ e^{-\frac{t}{\tau}} &= u_{rel}(t) \\ -\frac{t}{\tau} &= \ln(u_{rel}(t)) \\ t = \boxed{m_D} \cdot \tau &= \boxed{- \ln(u_{rel}(t))} \cdot \tau \\ \Rightarrow m_D(u_{rel}) &= - \ln(u_{rel}) \end{align*} $$
Practical considerations#
Peripheral devices are easiest to obtain for standard voltages such as 5V or 3.3V, plus higher voltages are generally less susceptible to disruptive external influences. This is why I opted for a supply voltage of 5V.
In my particular case, I wanted to use hardware integrated in a microcontroller that’s designed to trigger on a voltage level of 1.1V. So in this case \( u_{rel}(t) = \frac{1.1 V}{5 V} = 22\% \). This results in the values \( m_C(22\%) \approx 0.248 \) and \( m_D(22\%) \approx 1.514 \).
Thus, the value \( m_D \) is in a reasonable range between 0.7 and 2, so I decided to measure during discharge. For a discharge resistance of 1 MΩ and a desired measuring accuracy of 5 pF, we obtain \( t = m \cdot \tau = m \cdot R \cdot C \approx 1.514 \cdot (1~\cdot~10^6[\Omega]) \cdot (5~\cdot~10^{-12}[F]) = 7.57~\cdot~10^{-9}[s] = 7.57\mu s \).
It is therefore sufficient to have a processor fast enough to measure time spans in the microsecond range — nowadays an easy task for any CPU.
Real World Implementation#
A simple voltage divider is enough to perform the measurement in practice:
J in the diagram is routed to the microcontroller. There, an analog comparator and a digital output are required. If necessary, several pins must be linked for this purpose. C is the capacitor to be measured, RC the charging resistor and RD the discharging resistor.
Measurement during discharge#
C must first be charged so that measurements can be taken during discharge. The charging graph belongs to an exponential function. In theory, 100% are never truly reached, but in practice, it is sufficient to charge over 99% of the capacity, which takes \( 5 \cdot \tau \) seconds.
RC should be dimensioned to prevent the current from J to C from becoming too high. For my microcontroller, it is recommended to limit output currents to 20mA. At 5V therefore \( R_C \geq \frac{5V}{20mA} = 250\Omega \), therefore I choose the next standard size from the E12 series, RC = 270Ω.
With C ≤ 1nF this results in \( 5 \cdot \tau \leq 5 \cdot 270[\Omega] \cdot 1[nF] = 1.35[\mu s] \). It is therefore more than sufficient to load C for 2μs. As RC and RD constitute a voltage divider, it is also important that the ratio of RC to RD is as small as possible. If we make RD = 1MΩ, \( \frac{R_C}{R_C + R_D} \approx 0.00027 \lt 0.1\% \) falls within a very good range.
The actual measurement procedure#
Thus it is clear how a measurement can be carried out in practice:
- Charge C
- Use J as digital output and apply 5V
- Wait 2μs
- Discharge C
- Start timer (minimum resolution 7.57μs)
- Use J as analog comparator (high impedance, trigger voltage 1.1V)
- Wait for the comparator to be triggered, then retrieve the timer value
- Using \( t = m \cdot \tau = m \cdot R \cdot C \), calculate the capacitance: \( C = t \cdot (m \cdot R)^{-1} \approx t \cdot (1.514 \cdot 10^6)^{-1} \Rightarrow C \approx 6.604 \cdot 10^{-7} \cdot t \)
Example calculation for t = 7.57μs:
$$ \begin{align*} C &\approx 6.604 \cdot 10^{-7} \cdot 7.57~\cdot~10^{-6}[F] \\ &\approx 50~\cdot~10^{-13}[F] \\ &= 5 [pF] \end{align*} $$
As with any analog measurement, it is advisable to calculate an average value from several individual measurements. This greatly improves measurement precision.
tl;dr … Summary#
I have based the design on only three parameters: The supply voltage (5V), my target for measurement accuracy (5pF) and the maximum expected capacitance (1nF).
Measurement mode and trigger voltage level
In my case the trigger voltage is fixed at 1.1 V. Measurement during discharge is hence perfect, as \( m_D = - \ln(0.22) \approx 1.514 \) is in the desired range.
If the trigger level hadn’t been given, I would have targeted a value between 0.7 and 2 for \( m_D \), for example: \( m_D = 1.35 = - \ln(u_{rel}) \Rightarrow u_{rel} \approx 0.259 \Rightarrow U = u_{rel} \cdot 5V \approx 1.3V \).
Charging Resistor
The charging resistor should only be as large as necessary to limit the charging current, which in my case means at least 250Ω. The next standard resistor from the E12 series at 270Ω is therefore a reasonable choice.
Charging Duration
The smaller the charging resistance, the lower the time constant τ. This means the capacitor is charged faster (5 τ) and therefore ready for measurement sooner. Here τ is made up of maximum capacity and charging resistance: \( 5 \cdot \tau \leq 5 \cdot 270[\Omega] \cdot 1[nF] \), i.e. less than 2 μs.
Discharging Resistor
Maximizing the discharging resistor lessens the effect of the voltage divider. As a guideline, the value should not be less than 1000 times the size of the charging resistor.
Timer Resolution
The greater the discharging resistor, the lower are the resolution requirements for time measurement: \( t = m \cdot R \cdot C \Rightarrow R = \frac{t}{m \cdot C} \), where t is the tick duration of the timer and C is the desired measurement accuracy. My discharge resistor 1MΩ therefore requires at least one timer tick every 7.57μs to achieve 5pF accuracy.
Variations#
Measurement while charging#
If the measurement should take place while the capacitor is charging, you must of course adjust the \( m_C \) value instead. Otherwise the same logic applies:
Now C has to be discharged before the measurement. J therefore has to be used as a digital output (open drain) at the 0V level. To prevent the controller behind J from being overloaded, RD is now used to limit current. RC now has to be large enough to ensure that the voltage at the capacitor is as low as possible during discharge.
The same values forRC and RD also work here, just reversed. The timings do not change.
Setup without voltage divider#
The second circuit variant supports both measurement modes and allows RC and RD to be chosen independently of each other. This does, however, use up a second controller pin.
For example, C can be brought up to supply voltage by RC and then JC coupled to the internal comparator at high impedance. While charging, JD should present high impedance or be brought to supply voltage itself. JD as an open drain can then slowly discharge the capacitor via RD to the point where the comparator triggers.
The only thing that matters for RC is the electric current rating of JC. RD can be dimensioned as freely as the resolution of the available timer supports.
Conclusion#
Although the measuring circuit is only supplied with DC voltage, the theoretical background of a capacitance measurement is quite complicated. The practical application is simple, however:
Der theoretische Hintergrund einer Kapazitätsmessung ist unerwartet kompliziert, obwohl die Messschaltung nur mit Gleichspannung versorgt wird. Die praktische Anwendung ist jedoch einfach:
With this knowledge, designing a suitable circuit and developing the corresponding soft- or firmware should pose no difficulties anymore.
Image sources#
- “green circuit board” via Pickpik