There comes a time in every electronic designer’s life when, whether they know it or not, they need an analog filter in their design. If you’re coming from a digital background, where everything is nice and numeric, the harsh reality of continuous voltages can be a bit of a shock. But if you’re taking input from, or sending output to the big analog world out there, it pays to at least think about the frequency-domain properties of the signal, and maybe even do something about them.
Designing an analog filter to fit your needs can be a bit of a daunting task: there are many factors that you’re going to need to consider, and they all interact. It’s easy to get lost. We’re going to simplify this as much as possible by instead focusing on a few common applications and building up the simplest possible filters that work well for them.
Today, we’re going to consider the lowpass filter, and specifically a Sallen-Key filter with Butterworth characteristics and a second-order rolloff. Sound like word salad? We’ll fix that up right away, because this is probably the single most important filter to have in your analog toolbox for two very common use cases: pulse-width modulated (PWM) output and analog-to-digital conversion (ADC) input.
The Problem(s)
PWM output and ADC sampling on input have two things in common, there is a low-frequency signal that you’re interested in but it’s mixed together with some high-frequency content that you don’t want. In the PWM case, the high-frequency content comes from the underlying square wave whose duty cycle is being modulated — all you want is the average value over a few cycles.
A lot of the time you can ignore this high frequency output noise. Blinking an LED very quickly is no problem, and your eyes respond so slowly that they effectively do the lowpass filtering for you. When driving a motor with PWM at a high enough frequency, the motor windings form inductors that smooth out the signal for you. For audio out to speakers, or motor driving with a slower PWM frequency, or for creating your own DAC from the PWM, you’ll want to filter out the highs.
In sampling with an ADC, the frequency issue is more subtle: you want to pre-filter out all of the undesired frequency content that’s higher than half of your sampling frequency, because otherwise it folds into lower frequencies and messes up your data. The mathematical reason is the Nyquist-Shannon sampling theorem, but for intuition, there’s the wagon-wheel effect. When you’re sampling infrequently, “higher than half of your sampling frequency” is nearly everything, so there’s a lot to filter out.
The solution? Well, there are two. The first is to sample at more that twice the nuisance frequency and take averages in firmware. But when you’re already sampling really fast, or if the nuisance frequency is very high, that’s not an option. The analog solution is to get rid of the nuisance signals directly by filtering them away, and sampling just faster than twice the frequency of the desired signal. This is called lowpass sampling, and if you’re periodically reading an ADC without a filter up front, you’re lowpass sampling without the lowpass filter, and if there’s any strong higher-frequency signal mixed in, your resulting data is messed up. So you need a lowpass filter.
The other big difference between the PWM and ADC cases is that we want an audio waveform out of the PWM, which means a higher frequency AC signal, but in the ADC case we’re interested in the DC component of a low-frequency signal. Frequency range and AC/DC voltage offset will matter for the practical implementation of the filter, so it’s good to see both cases.
Filter Design Primer
There are at least five choices you have to make in designing a filter, and they’re all interdependent. When you add in other common pitfalls of analog design, like amplifier instability, range, slew rate, bandwidth, and distortion, parts tolerances, and real-world concerns like input and output impedance, it’s enough to drive you crazy. Here’s a whirlwind tour.
- Type: What frequencies do you want? Which do you want to get rid of? This determines whether you want a lowpass or highpass or bandpass filter, for instance. Today, we’re going for a lowpass.
- Topology: This is the actual circuit design. Here, we’re looking at a simple Sallen-Key filter topology because it’s stable and makes the lightest demands on the op-amp that we’re using. Even so, we’ll see that op-amp choice can matter.
- Order/Rolloff: The rate at which the high frequencies are rolled off depends on the order: the number of reactive components (here capacitors) in the design. You can make steeper filters if you need to by cascading single stages, and we’ll do that later. For now, we’ll be satisfied with a second-order filter that reduces the amplitude of the signal by a factor of four for every doubling of the frequency (a -12 dB per octave rolloff).
- Response: Some filter shapes over-emphasize certain frequencies and exhibit more overshoot or ringing in the time domain in trade for a steeper cutoff of the undesired frequencies. You can read more about that on your own time. Here, we’re going with a Butterworth response because it’s a nice midpoint between doing little damage to the signal and getting quickly to the eventual rolloff rate.
- Cutoff Frequency: The boundary between the frequencies that we want to keep (in a lowpass, the lows) and those we want to remove (the highs) is another important parameter that’s set by component choice.
A very good, if a bit advanced, resource on filter design is Chapter Eight of Analog Devices’ Linear Circuit Design Handbook. If you’re not feeling the math, you can skip the section on s transforms without losing out much. The remaining 140+ pages are pure gold, and should help get you through any analog filter design. We’re taking the expressway here, but if you want to enjoy the scenic route, go check it out. Filter Design in Thirty Seconds (PDF) is a quickie guide, and very much in the spirit of this series. It’s a great refresher once you’ve been through everything once or twice, but it naturally leaves out a lot of the good stuff.
Sallen-Key Lowpass: A Workhorse
A generic filter design starts with at least the five choices mentioned above. I’ve chosen four of them for you, which leaves one dimension, and it’s entirely independent of the others. A second-order, Butterworth-response Sallen-Key filter is the Pareto-principle solution, giving you 80% of the benefit for 20% of the effort. Some configuration of Sallen-Key filter will work for most specifications, and we’ll handle complications like multiple stage filters and other circuit topologies for special cases later on. For now, here’s the basic circuit diagram.
The beauty of this circuit is that the two resistors can be the same, and they usually will be, but that they don’t need to be particularly tightly matched, so that component tolerances aren’t an issue. The two capacitors do want to be in roughly the correct ratio because this determines the response type, but again these aren’t critical and a simple way to get a factor-of-two capacitance is simply to use two of the same capacitors in parallel. Finally, the op-amp is used as a simple buffer or amplifier stage, rather than being implicated in frequency-determining gymnastics. As mentioned above, this places the minimum demands on the high-frequency gain of the op-amp.
Enough praise of the Sallen-Key topology, let’s build something practical.
PWM Output Filtering
Imagine that you want to produce an audio-rate waveform by PWM. Maybe it’s for sample playback, maybe it’s simple beeping or booping. The solution that you’ve perhaps seen before is to simply throw a resistor and capacitor on the output. This works, but has two drawbacks: the rolloff is only 3 dB per octave so there’s less attenuation of the higher PWM frequency, and it’s a passive filter which increases the output impedance across the board, making it quieter even for the low-frequency components that we want to keep. You could cascade two R-C lowpass filters to steepen the rolloff, but they interact and the circuit gets messy. It’s better to toss an op-amp in the mix and solve both problems at once.
The product of C
(here, the value of C2
) and R
determines the cutoff frequency, like so: . You have a lot of freedom in choosing the values. A good rule of thumb is to keep R
in the range of 500 Ω to 200 kΩ. I usually ballpark a C
value for 10 kΩ and then pick a capacitor that works and fine-tune the cutoff frequency by tweaking the resistors until it’s right. For experimentation, two 50 kΩ pots or a stereo pot would be ideal.
Here, we’re aiming at an audio PWM, so ideally we’d set the cutoff of the filter around 20 kHz, at the top of human hearing. In an ideal world, our PWM frequency would be so much higher than 20 kHz that we wouldn’t have to compromise. In the real world, where we might be running PWM frequencies as low as 30-40 kHz, a cutoff of around 10 kHz is maybe a better goal. Plugging in 10 kΩ gives 1.13 nF. It’s almost too perfect! We’ll pick a 1 nF C and tweak the 10 kΩ if we need to fine-tune.
The ratio of the two capacitor values determines the response type of the filter. Again, we’ve settled on Butterworth as a good compromise, and because it makes the math easy. If you want other responses, your best bet is to dig up a filter design tool online to do the math for you. Ratios higher than two reach the cutoff rate faster, but exhibit frequency ripple. Ratios less than one roll off more slowly at first but have better impulse-response properties, or equivalently better phase linearity. Butterworth is the sweet spot for us.
Because this is an audio application, we don’t care about the absolute voltage, just the wiggles. An input capacitor decouples the voltage level of the microcontroller’s PWM and the pair of 100 kΩ bias the signal to the middle of the op-amp’s voltage range. An output capacitor decouples again, so the signal is ready to plug into whatever amplifier you’ve got. The values of these capacitors don’t matter much, but they have to be a lot bigger than C
. I used 1 µF here.
Op-Amp Selection
One of the reasons to choose a Sallen-Key filter circuit is that it’s easy on the op-amp. As drawn here, the op-amp is simply buffering the signal — it doesn’t have to manipulate any voltages in feedback to control anything. This means that if your op-amp has a frequency range that goes as high as the cutoff frequency, you’re good to go. I used garden-variety TL074s for this demo, and almost anything will work fine. I say almost because I initially tried an LM324 (which is the quad version of the popular LM358), and it choked on the square wave input.
In particular, the LM324 and LM358 have bad crossover distortion, so when the PWM input switches rapidly from on to off, the op-amp fails to track it. This shows up as nasty little spikes in the output. Some crossover distortion will be evident with any op-amp, but it’s especially horrible with the LM324. I generally love the LM324 because it’s cheap and works with input voltages as low as ground. We’ll use it below for slow ADC sample filtering, but its poor crossover performance makes it inappropriate for PWM filtering.
Anti-Aliasing, Pre-Sampling Filter
Let’s follow on with the example of a light sensor in the presence of flickering interior lighting. Imagine that we want to track per-second changes in light and smooth out the noise. As long as this is going into the input of a microcontroller’s ADC, we might as well scale the output to fit while we’re at it. Unlike in the PWM case, we care about the DC offset here — indeed, it’s what we’re trying to measure — so we’ll be directly coupling the filter to the output of the light sensor.
It may be a little bit odd to think of this as an anti-aliasing filter because of the low frequency, but the principles are the same — this is a lowpass sampling application. If we’re interested in detecting one-second changes, we can filter with a cutoff at 1 Hz and sample twice that fast, two times per second. The same idea works for sampling higher frequencies too, but the demands on the op-amp and the ADC get more pronounced. It’s better to start off easy. Here’s the circuit I have in mind.
The photoresistor and 2.2 kΩ resistor form a voltage divider that’s dependent on the amount of light hitting it. (Pro-tip: If you want to pick an optimal resistor for any voltage-divider-like setup, multiply the maximum and minimum resistances that you’re interested in, and take the square root. The result, the harmonic mean, is the lower resistor value that you want.)
I wanted a cutoff frequency around one second for this filter, so I initially ran the math with a 10 kΩ resistor, and got 10 µF — too large for my liking. Scaling the cap down by ten and the resistor up by ten is just about right. Because we’re interested in the DC values, simply drop the input capacitor and bias resistors and the output blocking cap.
From these scope shots you can see the desired action of the lowpass filter. In the left pane, the noise from the fluorescent light, which sits around 330 mV peak-to-peak, is filtered out nicely down to the twenty-millivolt level. In the right pane, you can see the response as I wave my hand slowly over the sensor, simulating a range of lighting levels. (I have four fingers!) The value tracks the actual light level reasonably quickly, although you can see some delay between the unfiltered and filtered values. This is just what happens when you filter!
Op-Amp Selection, Again
As opposed to the PWM application, the TL074 fails us here while the LM324 shines. The TL074, among many other op-amps, is designed for use with a negative voltage rail, which means that it can’t handle input signals that get within a couple volts of the lowest voltage. Here, where we’re using ground as the negative voltage reference, this causes the amplifier to miss out on input signals below about 1.5 V as I wiggle my hand up and down while covering up the sensor. The LM358 picks up these changes.
Picking op-amps for any application is full of nuances. I picked two of the most standard and representative types for this demo. The TL074 is decently fast for an old part, which makes it good for audio, but it’s designed for dual-supply use so it doesn’t cope with low voltages reliably. In audio (AC) applications, you usually only use the center portion of the voltage range anyway, so that’s OK. The LM324/LM358 is slower, but uses less power and reaches down to the negative rail on input, which makes it good for low-frequency, battery-powered, DC signal conditioning. When you need both rail-to-rail operation and high speed, and then you tack on low noise and low power consumption, you’ll find yourself reading datasheets and making tradeoffs, or spending money.
Matching to an ADC
The last detail to prepare a messy analog signal for digitalization is to scale the output voltage up or down to match the voltage range of the ADC. As it stands, the wire between the output and the inverting input on the op-amp holds the gain at 1. It’s a simple matter of adding two resistors in feedback to amplify the resulting signal, or adding a voltage divider following the filter to scale the signal down. Naturally, you only want to be doing one of these two.
What’s Next?
If you’ve followed along this far, you’ve got a general purpose lowpass filter that will handle almost all of your lowpass filtering needs: it’s got a decent rolloff, is capable of working with DC- or AC-coupled signals, and it’s got adjustable gain so that you can squeeze the maximum precision out of your ADC. You can even tweak the response type by changing the ratio of the capacitors if you want, but that’s getting fancy.
Next up will be three more examples of the Sallen-Key filter: a highpass design just swaps the resistors and capacitors, and a very useful bandpass filter can be made by cascading a highpass with a lowpass. Higher-order (steeper rolloff) filters can also be made by cascading these stages.
(Beautiful background image in the banner from [Windytan]’s phosphor-effect script.)
Filed under: Engineering, Hackaday Columns, how-to
No comments:
Post a Comment