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
VO(t) = VS*(1-e^(-t/(R*C)))
where VO is the capacitor's voltage, VS 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 no have a system of equations:
where t1 and t2 are relative to when we started charging the capacitor and P1 and P2 is the capacitor's charged percent at those times. (0 < P1 < P2 < 1)
delta_t=t_2-t_1 and combine the equations above to get
Solve for C:
Photo by Windell Oskay