Introduction
I have been using Arduino UNO for various projects. Unfortunately it lacks a proper analog output, and requires a low pass filter to convert its pulse width modulated (PWM) signal to analog voltage. It is a simple RC circuit, but the values of the resistor and capacitor need to be selected carefully to avoid fluctuations. This is a mathematical analysis of the circuit to derive the exact equation for the output voltage.
The RC circuit
The circuit is very simple, and it consists of only two components.Kirchhoff’s Voltage Law results in the following equation: \[\begin{equation} -v(t)+i(t)R+ v_c(t)=0,\tag{1} \end{equation}\] where the current \(i\) can be eliminated using the charge conservation equation: \[\begin{equation} i(t)=\frac{d Q}{dt}=C\frac{d v_c}{dt}. \tag{2} \end{equation}\] Therefore the equation we need to solve becomes \[\begin{equation} v(t)+R C \frac{d v_c}{dt}+v_c(t)=0 \tag{3}. \end{equation}\]
Solving the DE
There are multiple ways of solving Eq. (3). For example, if we are interested in the steady state solutions only, we can simply use fourier series expansion. In this analysis, I will want to keep track of the initial conditions and provide a complete solution using Laplace transformation, which is defined as follows: \[\begin{eqnarray} F(s)=\mathcal{L}\left[f(t)\right]=\int_0^\infty dt \, e^{-s \,t} f(t), \tag{4} \end{eqnarray}\] where I follow the convention of denoting the functions in the \(s\) space in capital letters. The transformation of the derivatives can be derived from the definition: \[\begin{eqnarray} \mathcal{L}\left[\frac{df}{dt}\right]=\int_0^\infty dt\left[e^{-s \,t}\frac{df}{dt} \right]=\int_0^\infty dt\left[\frac{d}{dt}\left[e^{-s \,t} f(t) \right] -f(t)\frac{d}{dt}e^{-s \,t}\right]=sF(s)-f(0) \tag{5} \end{eqnarray}\]
Eq. (3) in the \(s\) domain reads: \[\begin{equation} -V+R C \left(sV_c -v_c(0)\right)+ V_c =0 \tag{6}, \end{equation}\] where \(v_c(0)\) is the initial value of the voltage across the capacitor. We first need to calculate \(V\), which is the transformed PWM signal. The PWM can be parameterized by two parameters: The period \(T\) and the duration of maximum value \(\tau\), as illustrated in Fig. 2.
Note that Fig. 2 is nothing but a periodic placement of the following function: \[\begin{equation} f_0(t) = \begin{cases} v_0 & \text{for $0<t<\tau$,} \\ 0 & \text{elsewhere} \end{cases}\tag{7} \end{equation}\]
The square wave can be written as \[\begin{equation} v(t)=\sum_{n=-\infty}^\infty f_0(t-n T) \tag{8}. \end{equation}\] We can now transform it as follows: \[\begin{eqnarray} V&=&\mathcal{L}\left[v(t)\right]=\int_0^\infty dt\left[e^{-s \,t} \sum_{n=0}^\infty f_0(t-n T) \right] =\sum_{n=0}^\infty e^{-s \,n T} \int_{-nT}^\infty dt'\left[e^{-s \,t'} f_0(t') \right]\nonumber\\ &=&\sum_{n=0}^\infty e^{-s \,n T} \int_{0}^\tau dt'e^{-s \,t'} f_0(t') =v_0\sum_{n=0}^\infty e^{-s \,n T} \frac{1-e^{-s\tau}}{s}\tag{9}, \end{eqnarray}\] where the range for \(f_0\) integration is truncated to \(0-\tau\) since \(f_0\) vanishes elsewhere. If we really wanted, we could evaluate the sum \(\sum_{n=0}^\infty e^{-s \,n T} =\frac{1}{1-e^{-s T}}\), and if \(\tau=T/2\), this would result in a compact expression for \(V\):
\[\begin{eqnarray} V\Bigr|_{\tau=T/2}&=&\frac{v_0}{s} \frac{1-e^{-s T/2}}{1-e^{-s T}}=\frac{v_0}{s} \frac{1-e^{-s T/2}}{(1-e^{-s T/2})(1+e^{-s T/2})} =\frac{v_0}{s} \frac{1}{1+e^{-s T/2}}. \tag{10} \end{eqnarray}\] However, since we want to look at a generic PWM case, we’d better keep the sum as is. In fact, it will be more useful when we inverse Laplace transform. We can extract one more nugget of information from Eq. (9) by looking at the limit: \[\begin{eqnarray} \lim_{s\rightarrow 0}sV(s)&=& =\lim_{s\rightarrow 0}s v_0\sum_{n=0}^\infty e^{-s \,n T} \frac{1-e^{-s\tau}}{s} = v_0\lim_{s\rightarrow 0}\frac{1-e^{-s\tau}}{1-e^{-s T}}\simeq v_0\frac{\tau}{T}\tag{11}, \end{eqnarray}\] which is the average value (duty cycle) of the wave.
Inserting the expression from Eq. (9) into Eq. (6) and solvig for \(V_c\), we get \[\begin{eqnarray} V&=& \frac{v_0}{s(RC\, s+1)}\sum_{n=0}^\infty e^{-s \,n T} (1-e^{-s\tau})+ \frac{RC v_c(0)}{RC\, s+1}\nonumber\\ &=&v_0\left[\frac{1}{s} -\frac{1}{s+\frac{1}{RC}}\right]\sum_{n=0}^\infty \left[e^{-s \,n T} -e^{-s(nT+\tau)}\right]+ v_c(0)\frac{1}{ s+\frac{1}{RC}} \tag{12}. \end{eqnarray}\]
In order to inverse transform \(V_c\), we will make use of the following property: \[\begin{equation} \mathcal{L}^{-1}\left[e^{-\alpha s}F(s)\right]=u(t-\alpha)f(t-\alpha) \tag{13}, \end{equation}\] where \(u(t)\) is the unit step function. Furthermore, we know \(\mathcal{L}^{-1}\left[\frac{1}{s}\right]=u(t)\) and \(\mathcal{L}^{-1}\left[\frac{1}{ s+\frac{1}{RC}}\right]=u(t)e^{-\frac{t}{RC}}.\) Using these identities, we get:
\[\begin{equation} v_c(t)=\mathcal{L}^{-1}\left[V_c\right]=\sum_{n=0}^\infty \left[u(t-nT)(1-e^{-\frac{t-nT}{RC}})-u(t-nT-\tau)(1-e^{-\frac{t-nT-\tau}{RC}})\right]+v_c(0) u(t)e^{-\frac{t}{RC}} \tag{13}. \end{equation}\]
Visualizing the signals
In the interactive plot below \(\frac{\tau}{T}\), \(\frac{RC}{T}\) and \(v_c(0)\) values can be adjusted.