# 20. Introduction to filters¶

In this lesson, we will explore passive filters and complete a do-it-yourself exercise using an RC filter to do DAC from a PWM signal.

## Impedance¶

Resistors, capacitors, and inductors are linear devices, meaning that there is a linear relationship between current and voltage. For resistors, the current and voltage are in phase. That is, for a sinusoidal input voltage, the current is also sinusoidal with zero phase shift. This makes sense via Ohm’s law; $$V = I R$$. Capacitors, on the other hand, have a different voltage-current relationship,

\begin{align} \frac{\mathrm{d}V}{\mathrm{d}t} = \frac{I(t)}{C}, \end{align}

where $$t$$ is time and $$C$$ is the capacitance. So, for a sinusoidal input voltage, the current through a capacitor is out of phase by $$\pi/2$$. Finally, an inductor (which we directly will not use in this class) is $$\pi/2$$ out of phase in the other direction, with the relationship

\begin{align} V(t) = L\,\frac{\mathrm{d}I}{\mathrm{d}t}, \end{align}

where $$L$$ is the inductance.

Because of the linearity of these elements, it is convenient to instead define a complex impedance, $$Z = R + j X$$, where $$R$$ is a resistance and $$X$$ is a reactance. Here, $$j = \sqrt{-1}$$; electrical engineers use $$j$$ instead of $$i$$ so as not to confuse symbols with current. Writing the impedance in polar form, $$Z = Z_0\,\mathrm{e}^{j\phi}$$, where $$Z_0$$ is the magnitude of the impedance and $$\phi$$ is the phase angle. So, if we put a sinusoidal voltage with amplitude $$V_0$$ and frequency $$\omega$$ across a resistor, we have

\begin{align} V(t) = \mathrm{Re}[V_0\,\mathrm{e}^{j\omega t}] = R I(t), \end{align}

which gives

\begin{align} I(t) = \mathrm{Re}\left[\frac{1}{R}\,V_0\,\mathrm{e}^{j\omega t}\right]. \end{align}

The current is completely in phase with the voltage (which we already knew from Ohm’s law), meaning that resistors are completely (you guessed it) resistive. That is, for resistors, the impedance is $$Z = R$$.

Now, let’s put a sinusoidal voltage across a capacitor.

\begin{align} \frac{\mathrm{d}V}{\mathrm{d}t} = \frac{\mathrm{d}}{\mathrm{d}t}\,\mathrm{Re}[V_0\,\mathrm{e}^{j\omega t}] = \omega\,V_0\,\mathrm{Re}[j \mathrm{e}^{j\omega t}] = \frac{I(t)}{C}. \end{align}

Thus,

\begin{align} I(t) = \mathrm{Re}[j\omega\,V_0\,\mathrm{e}^{j\omega t}] = \mathrm{Re}\left[-\frac{1}{j/\omega C}\,V_0\,\mathrm{e}^{j\omega t}\right]. \end{align}

Here, the impedance is $$Z = -j/\omega C$$, which is purely imaginary, and therefore purely reactive. It is important to note that the impedance is frequency dependent. This is a key feature in filter design.

These relations are useful to remember; the impedance for a resistor is $$R$$, that for a capacitor is $$-j/\omega C$$, and that for an inductor (which we will not use) is $$j\omega L$$. We further note that Ohm’s law generalizes to impedances, where $$V = I Z$$.

## A generalized voltage divider¶

In the previous lesson, we learned about voltage dividers. Consider the same circuit as a voltage divider, but instead of resistance R₁ and R₂, we have generic impedances Z₁ and Z₂.

The expression for the voltage divider generalizes,

\begin{align} \frac{V_\mathrm{out}}{V_\mathrm{in}} = \frac{Z_2}{Z_1 + Z_2}. \end{align}

We will use this generalized voltage divider architecture to make filters.

## RC filters¶

Now that we have worked out that how voltage dividers work for general impedances, let us consider the case where $$Z_1$$ is purely reactive and $$Z_2$$ is purely resistive. This means that the component Z₁ in the above circuit diagram is a capacitor, say with capacitance $$C$$, and Z₂ is a resistor, say with resistance $$R$$. The complex representation of the output voltage is then

\begin{align} V_\mathrm{out} = \frac{R}{-j/\omega C + R}\, V_\mathrm{in}. \end{align}

The amplitude is found by taking the magnitude of the above expression, giving

\begin{align} \left|V_\mathrm{out}\right| = \frac{R}{\sqrt{1/\omega^2 C^2 + R^2}}\, \left|V_\mathrm{in}\right| = \frac{1}{\sqrt{1 + 1/\omega^2 R^2 C^2}}\, \left|V_\mathrm{in}\right|. \end{align}

Note that in taking the amplitude, we are ignoring the phase shift. The result is interesting because the magnitude of the output voltage is frequency dependent. For frequencies much smaller than $$1/RC$$, the second term in the denominator gets large, and the output voltage approaches zero. For high frequencies, the second term in the denominator is small, and $$\left|V_\mathrm{out}\right| \approx \left|V\mathrm{in}\right|$$. Thus the circuit where Z₁ is a capacitor and Z₂ is a resistor is a high-pass filter. High frequency changes in voltages get through, while low frequency changes in voltages are blocked.

## Thinking exercise 6: Passive low-pass RC filter¶

Show that when the situation is switched around, when Z₁ is a resistor with resistance $$R$$ and Z₂ is a capacitor with capacitance $$C$$, that the circuit works as a low-pass filter.

## Do-it-yourself exercise 8: DAC with PWM¶

In previous lessons, we have used the MCP4725 breakout board to perform digital-to-analog conversion. This enables us to have an analog voltage available. Now, you will try to get an analog voltage by filtering a pulse-width-modulated signal. You will compare these signals to a signal you get from the MCP4725.

Wire up this circuit:

Write an Arduino sketch that does the following.

• Uses I2C to send a sinusoidal voltage with amplitude 5 V and frequency of 1 Hz to the MCP4725 DAC. The converted voltage is read in on pin A0.

• Uses digital pin 3 to send a sinusoidally varying voltage with amplitude 5 V and frequency 1 Hz. Remember, PWM works by switching the voltage on and off, such that the average voltage matches what you want. In this case, the average voltage you are sending with analogWrite() is varying sinusoidally.

• Uses digital pin 6 to do the same.

• The PWM signals from pins 3 and 6 each go through a passive RC filter. The output voltages of these filters is read in on pins A1 and A2, respectively.

• You should send the readings from A0, A1, and A2 over serial to be plotted.

For the passive filters in the circuit, what frequencies would you expect to be filtered out? Note that the frequency of the pulse-width-modulated signal from digital pin 3 is 490 Hz and that from digital pin 6 is 980 Hz. Make plots of the signals from the MCP4725 DAC, pin 3 + RC filter, and pin 6 + RC filter. Do your plots agree with your assessment about filtered frequencies? Finally, what are some drawbacks of using PWM + an RC filter to perform DAC?

When we swap the resistor and capacitor from the high-pass filter, we get

\begin{align} V_\mathrm{out} = \frac{-j/\omega C}{R - j/\omega C}\, V_\mathrm{in} = -\frac{1}{1 + \omega R C}\, V_\mathrm{in}, \end{align}

which gives

\begin{align} \left|V_\mathrm{out}\right| = \frac{1}{\sqrt{1 + \omega^2 R^2 C^2}}\, \left|V_\mathrm{in}\right|. \end{align}

So, when the frequency $$\omega$$ is large compared to $$1/RC$$, the denominator is large, so that $$V_\mathrm{out} \approx 0$$. When $$\omega$$ is small compared to $$1/RC$$, $$\left|V_\mathrm{out}\right| \approx \left|V_\mathrm{in}\right|$$. Thus, low frequencies go through, but high frequencies are blocked.