Deriving the formula from 'scratch' for charging a capacitor
simulate this circuit – 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}$$
As an alternative to the classical texts approaching, please consider the following alternative derivation (and interpretation) that I developed a few years ago in order to motivate the students. It is related to the discretization of the RC circuit when the input is a voltage step (magnitude V) for \$t \geq 0\$.
Applying the Euler forward method to this first order differential equation, with \$k = 0,1,2,..., N\$ (where \$N\$ is the number of intervals and \$\Delta t\$ is very small):
$$ \frac{v_c(k+1)- v_c(k)}{\Delta t} \approx \frac{dv_c(t)}{dt}\mid _{t=k\Delta t} $$ or $$ v_c(k+1)= v_c(k)+(\frac{V-v_c(k)}{RC})\Delta t $$ Physical interpretation:
\$ \frac{V-v_c(k)}{R} \$: The current taken as constant on \$\Delta t\$ interval.
\$(\frac{V-v_c(k)}{R})\Delta t\$: Change of capacitor charge at the end of interval (ie \$\Delta q(k+1)\$).
\$(\frac{V-v_c(k)}{RC})\Delta t\$: Change of capacitor voltage at the end of interval (ie \$\Delta v_c(k+1)\$).
Rearranging:
$$ v_c(k+1)= \frac{V \Delta t}{RC} + (1-\frac{\Delta t}{RC})v_c(k)$$
Its possible to develop the terms of this difference equation for \$k \ge 1\$, considering, in this particular case, \$v_c(0) = 0\$: $$v_c(1) = \frac{V\Delta t}{RC}$$ $$v_c(2)= \frac{V \Delta t}{RC}+(1-\frac{\Delta t}{RC})\frac{V\Delta t}{RC}=\frac{V\Delta t}{RC}\left [1+(1-\frac{\Delta t}{RC}) \right] $$ $$v_c(3)=\frac{V \Delta t}{RC}\left [1+(1-\frac{\Delta t}{RC})+(1-\frac{\Delta t}{RC})^2 \right]$$ $$v_c(4)=\frac{V \Delta t}{RC}\left [1+(1-\frac{\Delta t}{RC})+(1-\frac{\Delta t}{RC})^2+(1-\frac{\Delta t}{RC})^3 \right]$$ ... $$v_c(k)=\frac{V \Delta t}{RC}\left [1+(1-\frac{\Delta t}{RC})+ ... +(1-\frac{\Delta t}{RC})^{k-1}\right]$$
The sum of first \$k\$ terms for a finite geometric series, leads to:
$$v_c(k)=V\left [1-(1-\frac{\Delta t}{RC})^k\right]$$
Since that \$k=\frac{t}{\Delta t}\$:
$$(1-\frac{\Delta t}{RC})^{\frac{t}{\Delta t}}=\left [(1-\frac{\Delta t}{RC})^{\frac{-1}{\frac{\Delta t}{RC}}} \right ]^{-\frac{t}{RC}}$$
Returning in the expression for \$v_c(k)\$ and taking the limit when \$\Delta t \rightarrow 0 \$ (ie conversion from discrete time to continuous time): $$\lim_{\Delta t\rightarrow 0}v_c(k)=V \left \{1-\left [\lim_{\Delta t\rightarrow 0}(1-\frac{\Delta t}{RC})^{\frac{-1}{\frac{\Delta t}{RC}}} \right ]^{\frac{-t}{RC}} \right \}$$
Remembering an alternative definition of Euler's number (\$e\$):
$$\lim_{\Delta t\rightarrow 0}(1-\frac{\Delta t}{RC})^{\frac{-1}{\frac{\Delta t}{RC}}} = e$$ Finally: $$v_c(t)=V(1-e^{\frac{-t}{RC}})$$