• Tetra Quark

On this page

  • Introduction
    • The RC circuit
  • Solving the DE
    • Interactive plot

Other Formats

  • PDF

The math of low pass filtered PWM

Electronics
Published

March 17, 2021

Abstract
This article explores the mathematical principles behind low-pass RC filters, with a focus on their response to Pulse Width Modulation (PWM) signals. Through interactive visualizations, we demonstrate how these fundamental circuits process digital signals, showing the relationship between the time constant (RC), duty cycle, and the resulting output voltage. Readers can experiment with different parameters to understand how the filter smooths out PWM signals into analog voltages, making this complex topic both accessible and practical. This understanding is essential for anyone working with digital-to-analog conversion, motor control, or signal processing applications.
Keywords

Electromagnetism, Magnetism

\(\require{cancel}\)

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: a resistor and a capacitor. The input is a pulse-width modulated signal, and the output is the filtered voltage across the capacitor.

Figure 1: A low pass filter.

Kirchhoff’s Voltage Law results in the following equation: \[\begin{equation} -v(t)+i(t)R+ v_c(t)=0, \label{eq:kirch} \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}. \label{eq:current} \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 \label{eq:kirch2}. \end{equation}\]

Solving the DE

There are multiple ways of solving Eq. \(\ref{eq:kirch2}\). 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), \label{eq:laplacedef} \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) \label{eq:laplacederivative} \end{eqnarray}\]

Eq. \(\ref{eq:kirch2}\) in the \(s\) domain reads: \[\begin{equation} -V+R C \left(sV_c -v_c(0)\right)+ V_c =0 \label{eq:kirch2s}, \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 Figure 2.

Figure 2: A square wave.

Note that Figure 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} \label{eq:buildingblock} \end{equation}\]

The square wave can be written as \[\begin{equation} v(t)=\sum_{n=-\infty}^\infty f_0(t-n T) \label{eq:sqwave}. \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}\label{eq:laplaceV}, \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}}. \label{eq:laplaceVspecial} \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. \(\ref{eq:laplaceV}\) 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}\label{eq:laplaceVzero}, \end{eqnarray}\] which is the average value (duty cycle) of the wave.

Inserting the expression from Eq. \(\ref{eq:laplaceV}\) into Eq. \(\ref{eq:kirch2s}\) 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}} \label{eq:kirch2sf}. \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) \label{eq:invl}, \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}} \label{eq:invlx}. \end{equation}\]

\(\nextSection\)

Interactive plot

Below is an interactive plot where you can adjust the parameters of the PWM signal and the RC-circuit.
\(\frac{\tau}{T}\)
\(\text{number of periods}\)
\(\tau\)
\(V_0\)
Code
pwmPlot = {
  // Your existing plot div will be created and managed by Observable
  const element = DOM.element('div');
  element.style.width = "100%";
  element.id = "pwmPlot";
  return element;
}
Figure 3: An interactive plot of the input signal PWM and the voltage accross the capacitor(\(v_c\)).