Low Voltage AC Source (Part 3)

electronics AC filters

We already created a sine wave using a DAC. Today, we are going to try out another method: transforming a square wave into a sine wave.

From Square to Sine Wave

As we saw in the last part of this project, it is possible to generate a sine wave using a DAC. There is, however, more than one way to generate a sine wave. Today we are going to look at a different way and its advantages and disadvantages. We'll start off with generating a square wave, before we look at filters and how they can be used to transform a square wave into a sine wave.

Square wave to sine wave

Generating the Square Wave

In the last part of this project, we went through a lot of effort, before we could generate the sine wave with the desired frequency. Generating a square wave is a lot easier.

There a many ways to generate a square wave with the Arduino Uno. The first method that comes in mind is using PWM. There is however an issue with this: the Arduino language does not allow us to specify the PWM frequency. Only the duty cycle can be set. The second method that allows us to generate a square wave is using the tone procedure. This method allows us to specify the frequency. The only issue is that the tone procedure can only generate a square wave with a frequency higher than 31 Hz. For frequencies above this value using tone works perfectly fine. Another possible solutions is to just toggle a pin. This is something we can use to also support slow output frequencies. To do this, we are going to adjust the code from the last part of our project.

The adjusted code is much simpler. There is no need to precalculate anything, as we only have two possible values: HIGH and LOW. We only need to read the desired frequency and calculate the wait time between toggling the output pin state in the loop procedure. As we only have two values, we need to do this two times in each period. I used pin 9 as output pin for the square wave signal.

Here is how the adjusted code looks like:

#include <EEPROM.h>

unsigned long usPerStep;
unsigned long start_time;
bool state = LOW;

// Setup frequency
void setup() {
  // Read desired frequency
  Serial.begin(9600);
  Serial.print("Enter Frequency (Hz): ");

  // Wait 10s for input otherwise take stored value
  Serial.setTimeout(10000);
  float frequency = Serial.parseFloat();
  if(frequency == 0) EEPROM.get(0, frequency);
  else EEPROM.put(0, frequency);

  Serial.println(frequency);

  // Time per Step
  usPerStep = 1000000 / (frequency * 2);
  
  // Initially set start time
  pinMode(9, OUTPUT);
  start_time = micros();
}

void loop() {
  digitalWrite(9, state);
  state = !state;
  
  while(micros()-start_time < usPerStep);
  start_time += usPerStep;
}

The Theory

The Arduino program allows us to generate a square wave with a configurable frequency. The question that remains is, how we can transform it into a sine wave. To understand how this is going to work, we need a bit of theory. To be more specific, we need to take a short look at the topic of harmonic analysis.

Harmonic analysis is a mathematical discipline that is concerned with representing complex signals as a composition of harmonic oscillations with different frequencies, phase shift and amplitudes. It thus also allows us to decompose a signal into its frequency components which is also known under the term Fourier analysis. Why is this interesting for us? Well, a harmonic oscillation is an oscillation that can be described with a sine wave and a sine wave is what we want to generate. A square signal can be represented by multiple combined sine oscillations. More specifically it consists of the odd order harmonics of its base frequency.

A harmonic is an oscillation with a frequency that is a true multiple of the base frequency. In case of a 50 Hz signal this base frequency is the 1st harmonic. The second harmonic also called the first overtone is at 100 Hz, the third harmonic is at 150 Hz. As a square wave signal is composed of only odd order harmonics this means that the second harmonic has an amplitude of 0 V. For the odd order harmonics the amplitude decreases with every harmonic. For the 3rd order harmonic the amplitude is only a third of the 1st order harmonic's amplitude. For the 5th order harmonic its only a fifths and so on.

Square wave with its harmonics

If we combine the first five odd order harmonics, as shown in the image below, we get something that already looks very similar to a square wave. If we continue to add more harmonics we'll get closer and closer to a real square wave.

Composition of a square wave signal

What can we do with the knowledge? Well, to transform a square wave into a sine wave, we just need to remove the higher order harmonics and would end up with a perfect sine wave. This can be done using a low pass filter. A low pass filter suppresses signals above a certain frequency, while lower frequency signals can pass trough.

RC Low Pass Filters

For our purpose, we are going to use a simple RC low pass filter. An RC low pass filter is a pretty simple circuit. As shown in the circuit diagram below, it only consists of a capacitor (C) and a resistor (R). 1st order RC low pass filter

How does this circuit work?

In case of a DC voltage the capacitor charges up, until it reaches the input voltage and has no real effect afterwards. However, if there is a change in voltage the capacitor counteracts it. If e.g. a load requires more power for a short amount of time than the power supply can provide, the capacitor can provide it and prevent the supply voltage from dropping. It is very common for capacitors to be used as such a buffer.

For signals with a constantly changing voltage like our square wave things are a bit different. The capacitor is constantly charged and discharged and constantly counteracts the voltage changes. While charging and discharging, it seems like a current is flowing through capacitor. It doesn't, the electrons flow into the capacitor, while it is charging, and out of it later, when it is discharging.

Charging and discharging the capacitor is nothing that happens instantly, especially as the current is limited by the resistor. For this reason, it takes some time until the output voltage is the same as the input voltage. If the input voltage changes with a high frequency, the capacitor is not able to fully charge until it gets discharged again. So for high frequencies, the output voltage is less than the input voltage. The signal gets attenuated. For low frequency signals this is not the case. The capacitor has enough time to fully charge and discharge. For AC signals an RC low pass filter acts like a frequency dependent voltage divider.

If one wants to describe this effect in a more quantifiable, a more mathematical way, one can use a measure that is called impedance. Impedance is a measure for the opposition a circuit present towards the current flow. The impedance not only incorporates the resistance of the resistor, but also the seemingly existing one of the capacitor in case of AC signals. Its value is frequency dependent. The impedance allows us to do calculations similar to the ones we made for DC circuits and normal voltage dividers. However, the calculations become a bit more difficult as the impedance is represented by using complex numbers. If one wants to do calculations in AC circuits complex numbers are a very useful tool even if they are more complicated to work with.

For the purpose of our project, we don't need any of these complex calculations as long as we don't want to predict the output of our filter. For designing our simple filter circuit, we can work with a much simpler measure: the cutoff frequency.

The Cutoff Frequency

The cutoff frequency can be calculated using the following formula:
\(f_{c} = {1 \over {2 \pi R C}}\)

The cutoff frequency is defined as the frequency at which the power of the output signal is half the one of the input signal. What does this mean for the voltage? The electrical power can calculated with the following formula:
\(P = {V^2 \over R}\)

If we solve this formula for \(V\), we get the following:
\(V = \sqrt{P \cdot R}\)

From this formula, we can see, that for half the power the output voltage is \(\sqrt{1 \over 2} = {1 \over \sqrt 2}\) times the input voltage. If 1 V is RMS voltage of a signal which frequency is equal to the cutoff frequency, the output voltage is 0.71 V.

The factor between input and output voltage is also called voltage gain:
\(A_v = {V_{out} \over V_{in}}\)

Similarly, the power gain can be calculated for the power.

A gain higher than 1 means that the signal gets amplified, a gain below 1 means that it is attenuated. Our RC low pass filter is a passive filter, which means that there are no components in it that would amplify a signal. For this reason, the gain is never bigger than 1.

The gain is often represented as a logarithmic value. This is especially true for audio signals, from where we know the unit decibels (dB) which is e.g. used to describe the output power or loudness of a speaker. An increase of 10 dB is equivalent to ten times more power. Half the power is equivalent to a change of -3 dB.

For the power the gain in decibels can be calculated using the following formula:
\(a_p = 10 \log10({P_{out} \over P_{in}}) dB\)

We can also use the voltages to directly calculate the gain. In this case the formula is a bit different to take care of the fact, that the power increases quadratically with the voltage.
\(a_v = 20 \log10({V_{out} \over V_{in}}) dB\)

Using a logarithmic scale makes sense whenever a value does not change linearly and we want to be able to display very big and very small values together. Additionally, filters are often used in audio applications where the use of decibels is common anyway. The reason for this is that the human sound perception is not linear either. For humans ten times the output power (+ 10 dB) only seems twice as loud. For our use case this doesn't matter, but I'll still stick to the convention and express the gain in decibel.

The graphs below show the gain for an RC low pass filter with a cut-off frequency of roughly 72 Hz. The right one uses a logarithmic scale and the left one a linear scale. As you can see, the small voltage changes at higher frequencies are much more visible on the logarithmic scale. Gain for an RC low pass filter shown on a linear and a logarithmic scale

The RC Filter in Practice

Enough theory, let's build our first RC low pass filter. As discussed, we want to remove the higher order harmonics from our square wave. For this we need a resistor and capacitor combination that forms a filter with a cutoff frequency in between the base frequency and the first overtone. There is no way to build a filter that works for multiple different frequency at the same time. For a 1 Hz square wave we need a cutoff frequency between 1 Hz and 3 Hz. For 50 Hz, on the other hand, we need the cutoff frequency in between 50 Hz and 150 Hz. The component values need to be adjusted to the frequency of our square wave. This means we loose the ability to adjust the frequency without changing the circuit. A clear disadvantage compared to our previous solution with the DAC.

Let's continue anyway and build the circuit for a frequency of 50 Hz. In theory, there is an unlimited amount of resistor and capacitor combinations for building an RC low pass filter with a specific cutoff frequency. We are limited to the commonly available resistor and capacitance values, however. Additionally, the GPIO pins of the Arduino can only supply a limited amount of current. We need to make sure that we choose the resistor value high enough to not damage the Arduino with the current that flows while the capacitor charges up. As the Arduino operates at 5 V and the pins can supply up to 40 mA of current, a resistor value above 125 Ω should be fine. However, a more common value in Arduino kits is 220 Ω, as this resistor value is used to limit the current for LEDs. For this resistor value, a capacitor with a capacity of 10 uF allows us to achieve a cutoff frequency in the desired frequency range.

The filters cutoff frequency is:
\(f_c = {1 \over {2 \pi R C}} = {1 \over {2 \pi \cdot 220 Ω \cdot 10 uF}} \approx 72 Hz\)

This is also the filter matching the gain frequency response curves, I showed you before.

If we build the real circuit we also need to include the voltage divider and our 10 kΩ load across which we are going to measure the voltage with an oscilloscope. The full circuit is shown below. As you might have noticed, I connected the negative terminal of the electrolytic capacitors directly to ground and not to the output of our voltage divider. Electrolytic capacitors can be easily damaged if they are reverse biased. They are not the best choice for AC circuits. However, in our case we don't use a real AC supply. We use a voltage divider to move the ground level and by connecting the capacitor directly to ground and not to the voltage divider, we can ensure that the polarity is always correct. Circuit with a 1st order RC filter

Do the additional components have an influence on the RC low pass filter?
In our circuit we added additional components and don't have a pure RC filter circuit. For this reason, it's a valid question to ask whether this affects the behavior of our filter.

The short answer is, yes of course. The voltage divider, the load and the fact that we connected the capacitors directly to ground influence the output signal of our RC filter. It is more difficult to say how exactly they do influence the filter, however. Taking everything into account to calculate the filters output signal is a non-trivial task. Additionally, all components have tolerances and real capacitors do not only have capacitance, but also some inductance and additional internal resistance.

Everything you see in the diagrams on this page is only an approximation. In the diagrams I took care of the fact that we have a 10 kΩ load resistor in our circuit. I ignored the voltage divider and the fact that the capacitors are connected directly to ground, as well as any tolerances and limitations of real electronic components. In the end, however, the additional components only have small impact on the output signal - at least as long as the load has a high impedance. This is true for out 10 kΩ load resistor.

How does our filter perform in action? Well, as you can see in the image below, our output signal is not exactly something that one would call a sine wave.

Filtered square wave

What happened? One way to interpret the result is to say that we see the capacitor charging and discharging. This is exactly what happens and this is how it looks like. However, what we are really interested in are the square wave's harmonics that we wanted to remove and why we didn't get a sine wave. What frequency components are left in our filtered signal? The graph below shows us, that the high order harmonics have been attenuated a lot, but are still part of our signal. They are what causes our output signal to look the way it does.

Output signal of the 1st order RC filter and its frequency components

If we want to look at how our output signal is composed of this individual frequency components, we need more than just their amplitudes. The image below shows the first five harmonics and the resulting waveform if they are composed together. It's easy to see that we would get the signal we saw on the oscilloscope, if we would account for all harmonics. There is however another thing worthy of note: the individual signals are shifted compared to each other. This so-called phase shift is caused by the capacitor in our RC filter and is also frequency dependent. Due to this phase shift our signal is not symmetrical anymore.

Composition of the output signal of the 1st order RC filter form its frequency components

All these details don't really matter. What matters is that in theory we can still get a sine wave. Our filter is just not good enough. It doesn't attenuate the higher order harmonics strong enough.

Higher Order Filters

Filters that provide more attenuation than a simple RC low pass filter are called higher order filters. A very simple way to build such a higher order filter is to chain multiple RC low pass filters together. It's a very simple way, but certainly not the best. Each filter stage we add also affects the other ones and our the first order harmonic that we are trying to extract becomes more and more attenuated too. If we wanted to do this right, we would need an amplifier between each step, which would prevent the filter stages from affecting each other and prevent the signal from getting attenuated too much.

For the sake of simplicity we stick to the simple version of a 3rd order RC low pass filter and just chain three RC filters together like shown in the image below. 3rd order low pass filter

It's way harder to calculate the output signal of this filter than doing it for the 1st order filter, as it is required to take into account how the filter stages affect each other. For the sake of simplicity we won't calculate new component values or determine the new cut-off frequency. Let's just build the circuit with three identical filter stages as shown in the picture below and see what happens.

Circuit for the 3rd order RC low pass filter

The Result

The picture below shows the output of our 3rd order RC low pass filter. It's not yet perfect, but it is almost a sine wave. Filtered square wave using a 3rd order RC filter

Is this solution worth the effort? Well, the output signal has an amplitude of less than 1 V. That doesn't look good compared to the full 2.5 V we could achieve using the DAC. Similar to the DAC, this circuit won't allow us to provide much power for the AC circuit. We definitely need an additional amplification step to not only increase the voltage, but also the current we can provide to the AC part of our circuit.

Does this method have any advantages? Yes, it does. The first and properly most important one is that it also works for high frequencies where using the DAC is not an option. Additionally, we don't necessarily need a microcontroller for it to work. A much simpler oscillator circuit could be used instead. A square wave signal is also much easier to handle. We could feed the Arduino's output signal into a MOSFET amplifier stage and then adjust the filter for higher output currents. We could further improve the performance of this solution, if we let go of the idea to generate a perfect sine wave and just accept some distortions. It's not that these solutions is useless, it just doesn't fit the purpose of our project. What really makes this solution a no-go for us, is the fact that we loosed the ability to configure the output frequency which was one of our goals.

Could we improve this solution by adjusting the component values? Yes, we can - at least to some extent. However, the first and the third order harmonic are too close to eachother to be able to use simple filters without attenuating the whole signal. Filters are much easier to use, if the frequencies we need to filter out are more distinct from the signal we are trying to keep. This is the case in the next method I'll present to you. But if you want to, you can try to optimize the component values for this solution using the tool below. It calculates the approximate output signal of the filter. It assumes a 10 kΩ load. For simplicity, it ignores the existence of the voltage divider we used to offset the ground level.

Tool: Approximated Output Signal of an RC Filter

Resistance (Ω)
Capacitance (µF)
Resistance (Ω)
Capacitance (µF)
Resistance (Ω)
Capacitance (µF)

Previous Post Next Post