Deriving the formula from 'scratch' for charging a capacitor
simulate this circuit &ndash; Schematic created using CircuitLab

It's a pretty straightforward process. There are three steps:


Write a KVL equation. Because there's a capacitor, this will be a differential equation.
Solve the differential equation to get a general solution.
Apply the initial condition of the circuit to get the particular solution. In this case, the conditions tell us whether the capacitor will charge or discharge.


Let's go through this. Instead of using an actual step function, I'm going to use a DC input and assume the capacitor starts out discharged. First, you write a KVL equation:

$$V_i = v_R + v_C$$

In circuit analysis, we like to use current instead of charge. So instead of \$Q = CV\$, we use \$i = C \frac {dV}{dt}\$. The resistor and capacitor share the same current, so:

$$i_R = i_C = C \frac {dv_C}{dt}$$

You can put this into the KVL equation:

$$v_R = Ri_R = Ri_C = RC \frac {dv_C}{dt}$$
$$V_i = RC \frac {dv_C}{dt} + v_C$$

This is a first-order linear differential equation. Using, some algebra, you can rearrange it into a solvable form:

$$RC \frac {dv_C}{dt} = V_i - v_c$$
$$\frac {dv_C} {V_i - v_C} = \frac {dt} {RC}$$

Integrating both sides gives:

$$-\ln (V_i - v_C) = \frac t {RC} + C_0$$

You can get rid of the \$\ln\$ by moving the negative sign and making both sides a power of \$e\$:

$$V_i - v_C = e^{-t/RC + C_0} = e^{-t/RC}e^{C_0}$$

\$C_0\$ is a constant of integration, so \$e^{C_0}\$ is also a constant. Let's rename it to \$C_1\$ for convenience:

$$V_i - v_C = C_1e^{-t/RC}$$

The differential equation is solved, but there's still an unknown (\$C_1\$). You can find its value if you know the initial condition of the circuit. In this case, I said that the capacitor started out discharged (\$v_C = 0\$ at \$t = 0\$), so let's use that:

$$V_i - 0 = C_1e^{-0/RC} = C_1 \cdot 1$$
$$C_1 = V_i$$

Now you can find the fully-solved equation:

$$V_i - v_C = V_ie^{-t/RC}$$
$$v_C = V_i - V_ie^{-t/RC}$$
$$v_C = V_i(1 - e^{-t/RC})$$

Is this correct? At \$t=0\$, you have:

$$v_C = V_i(1 - 1) = 0$$

and at \$t=\infty\$, you have:

$$v_C = V_i(1 - 0) = V_i$$

So the capacitor starts out discharged, ends up fully-charged, and in between there's an exponential decay. That's correct!

For the discharge, \$V_i = 0\$ and the initial condition is that the capacitor is charged to a nonzero value, which I'll call \$V_0\$. You can use these to solve for \$C_1\$ again:

$$0 - V_0 = C_1e^{-0/RC}$$
$$C_1 = -V_0$$
$$-v_C = -V_0e^{-t/RC}$$
$$v_C = V_0e^{-t/RC}$$