This post explains the math behind a very specific situation: charging a capacitor from 0 V to a fixed voltage through a resistor of known value. The time at which the capacitor begins charging is recent but unknown. By taking two voltage measurements a known amount of time apart while the capacitor is still charging, you can calculate the value of the capacitor.

### Motivation

The bus capacitance for I2C is generally specified to be 300 pF maximum. This capacitance does not come from a discrete capacitor but rather from the capacitance of the traces, wires, and cables used to connect devices in the bus. How can we measure this capacitance in-place? Luckily, this bus has a pull-up resistor of a known value. We can use the knowledge of the resistor's value, the I2C voltage we're using, and some math to calculate the bus capacitance.

### Derivation

The equation for a charging capacitor is

V

O(t) = VS*(1-e^(-t/(R*C)))

where V__O is the capacitor's voltage, V__S is the source voltage charging the capacitor, R is the resistor value, C is the capacitor value, and the capacitor is assumed to be initially discharged.

With the substitutions `tau=R*C`

and `P=V_O/V_S`

, we can simplify this equation to

P(t)=1-e^(-t/tau)

Note that P(t) ranges from 0 (the capacitor is fully discharged) to 1 (the capacitor is fully charged).

We can rearrange this to solve for t:

t=-tau*ln(1-P(t))

If we take two measurements along the curve, we no have a system of equations:

t

1=-tau*ln(1-P1)t

2=-tau*ln(1-P2)

where t__1 and t__2 are relative to when we started charging the capacitor and P__1 and P__2 is the capacitor's charged percent at those times. (0 < P__1 < P__2 < 1)

Let `delta_t=t_2-t_1`

and combine the equations above to get

delta

t=tau*[ln(1-P1)-ln(1-P_2)]

Solve for C:

C=delta

t/[R*(ln(1-P1)-ln(1-P_2))]

__
__1

Photo by Windell Oskay