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.
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.
The equation for a charging capacitor is
V_O(t) = V_S*(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
P=V_O/V_S, we can simplify this equation to
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:
If we take two measurements along the curve, we now have a system of equations:
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)
delta_t=t_2-t_1 and combine the equations above to get
Solve for C:
Photo by Windell Oskay