variable filter for varying square wave frequency

Thread Starter

cktboy

Joined Apr 24, 2020
34
hello,
I have arduino generating square wave at varying frequency(potentiometer) but I want varying sine wave. I know that filtering a particular frequency square wave we can obtain sine wave but the problem here is how to make frequency adaptive filter for obtaining sine wave for varying square wave frequency?

thank you all
 

WBahn

Joined Mar 31, 2012
27,395
Do you have any DAC outputs?

What is the range of frequencies you are interested in (min and max)?

How clean does the sine wave need to be?
 

Thread Starter

cktboy

Joined Apr 24, 2020
34
no external DAC, the signal is square wave in CTC mode ranges from around 40KHz to 4MHz, it would be great i could get clean sine wave, i was hoping of making simple kind of function generator.
 

Ian0

Joined Aug 7, 2020
6,294
There is a neat circuit using a 4018, though a 4018 would be a bit slow, but you could generate the various phase-shifted squarewaves using the CTC.
@Audioguru again combined it with a switched capacitor filter to improve the sine wave.

Have you considered Walsh functions?
 

Attachments

Thread Starter

cktboy

Joined Apr 24, 2020
34
There is a neat circuit using a 4018, though a 4018 would be a bit slow, but you could generate the various phase-shifted squarewaves using the CTC.
@Audioguru again combined it with a switched capacitor filter to improve the sine wave.

Have you considered Walsh functions?
thank you, but i don't have the 4018 IC, any other solution?
 

DickCappels

Joined Aug 21, 2008
9,317
You answer @WBahn 's question in post #3. With those answers maybe you will receive suggestions that you can accept.

http://www.eprojectszone.com/how-to-generate-a-sine-wave-from-arduino-or-atmega-328/

A very clever method I remember seeing was in a Design Ideas or Ideas For Design article (these were in two different magazines) was to generate a high frequency clock signal to drive a switched capacitor filter, then divide down the clock to make a much lower square wave, which is passed through the filter. This method filters reduced the amplitude of harmonics thus turning the square wave into a sine wave. The beauty of this approach is that the ratio of clock-to-input frequencies was constant so the resulting sine wave did not change much as the frequency of the sine wave was changed.

To use the RC filter method, just make filter that attenuates most of the harmonics. As noted, this is a solution for a fixed frequency.

Another approach which should be easy with an Arduino:


1663154566377.png
A sine table is stored in memory and read out continuously in a timed loop. The value from the table is output to the R-2R DAC and if needed, buffered. You can get away with only entries for 90° of the sine wave, use use the table four different ways to make a full cycle.

Another method is to use Direct Digital Synthesis. This also uses an R-2R ladder network as the DAC.
1663155242291.png
(Note: Click on schematic above to see a larger version)

The article is here: http://www.radanpro.com/Radan2400/mikrokontroleri/Jesper's AVR pages - MiniDDS.htm

I copied the author's (Jesper) assembly code, using an ATMEGA48 (4K version of the ATMEGA328) and it worked fairly, eventually I was able to generate sine waves at arbitrary frequencies within the machine's capability.

There is Atmels's application note on the subject of generating 3 phase (almost) sine waves for the purpose of driving motors.
https://ww1.microchip.com/downloads/en/Appnotes/doc8010.pdf

If you can more information about what you want to accomplish and your restrictions (ex: I don't want to buy any parts), maybe on of the proposals identified in this thread can be adapted to your needs, but you have to tell us.
 

BobTPH

Joined Jun 5, 2013
5,762
Microcontrollers are amazing devices. good at solving many kinds of problems. Generating 4 MHz sine waves is not one of them.
 

Thread Starter

cktboy

Joined Apr 24, 2020
34
hello all,
it's not that i don't know that there exits other solution. i wanted to know whether there is solution for adjustable filter for my arduino case

thank you all
 

Ian0

Joined Aug 7, 2020
6,294
hello all,
it's not that i don't know that there exits other solution. i wanted to know whether there is solution for adjustable filter for my arduino case

thank you all
Switched capacitor filter, as I mentioned before, but they tend to need a clock at 50x the output frequency, so not at option for 4MHz.
Going directly from squarewave to sine wave will require some pretty steep filtering to get rid of the third harmonic, hence the various suggestions for alternative circuits preceding the filter to remove some of the lower harmonics.
 

AnalogKid

Joined Aug 1, 2013
10,125
A complicated way to get there:

Run the square wave through a voltage-to-frequency converter circuit.

Use the resulting output to control a multi-pole lowpass filter built using voltage-controlled transconductance amplifiers instead of traditional opamps.

Because the filter corner frequency will have to be so close to the input frequency, the output sine wave will be greatly attenuated relative to the input square wave amplitude.

Another approach is to use a (voltage controlled) gyrator circuit that forms a resonant trap at the third harmonic of the input frequency. This will do a better job of removing the dominant harmonic while prodicing a higher output voltage, but a worse job of removing the higher-order harmonics. If you are completely insane you could use both filter circuits in series, but that's a *lot* of parts that have to track.

A big problem for both circuits is the amplifier gain-bandwidth needed to function correctly at 4 MHz.

ak
 

bertus

Joined Apr 5, 2008
22,094
Hello,

Have a look at the LM13700.
It will not reach the wanted 4MHz, but only 50 kHz.
Figure 29 of the datasheet shows a filter.

Bertus
 

Attachments

MrChips

Joined Oct 2, 2009
27,122
40kHz to 4MHz adjustable filter is not going to be easy.
Why don't you start with a 4MHz sinewave VCO (voltage controlled oscillator) and use PLL (phase locked loop)?
 

ScottWang

Joined Aug 23, 2012
7,295
If you just using an LC filter can convert the square wave to a sine wave, and through an op-amp as a voltage shifter, you also need to adjust the output voltage level to the level you want, the range of frequency from 40Khz to 4Mhz is a little large, so you will need some different L and C, you also need to find the adjustable coils or capacitors.

An Arduino PIO can't output the frequency of a square wave too high, you can try to output one high and one low and measure its frequency.

Edit:
If my memory is correct, my Arduino PIO only can output 193Khz or 197Khz, it can't reach up to 200Khz, I just sent one high and one low.
 

Ian0

Joined Aug 7, 2020
6,294
The best suppression of the third harmonic that can be achieved with a 2nd order LC filter is 19dB, and that only happens well above the cutoff frequency of the filter, where the fundamental is also being considerably attenuated.
The third harmonic is 9.5dB below the fundamental, so the best 3rd harmonic figure you could hope for is -28.5dB, or 4% THD.
It‘s not going to work over a 100:1 frequency range without switching in different filters, otherwise there would be an amplitude variation of 79dB between mini mum and maximum frequency.
 

ScottWang

Joined Aug 23, 2012
7,295
For a DAC converter that it needs 256 levels to convert to a sinewave, so a 4Mhz sinewave will need 1.024Ghz, the frequency is pretty high.

Why do you want to use the Arduino to do the thing out of its ability?

If you just want the sinewave function then you can buy a sinewave function generator is better.
 
Top