Square wave to Sine wave but in DC range? - Page 1
Forum section: electronics / projects

author: Chriss
author_rank: Frequent Contributor
timestamp: March 07, 2017, 08:13:22 am
content: Hi!I'm searching for a solution how to convert the square wave which is coming from an Atmel uC to a sine wave, actually the sine wave should stay in the DC range. I don't need an AC sine wave.The problem for me is the frequency of the square wave could be changed, depend on the setup by user, so the frequency of the sine wave should also flow the square wave frequency.The frequency range should be from 10Hz maybe up to 10KHz.Any idea?Tanks.My best regards.

author: JPortici
author_rank: Super Contributor
timestamp: March 07, 2017, 08:25:41 am
content: anything that is over 0 Hz is technically AC.(tecnically not even that as DC may also be thought as a pulse at an extreeeemely slow frequency :)anyway. what are you trying to accomplish, exactly?sounds like a waveform generator.. yours is not the usual way to do it if you wand different waves!DCO perhaps?anyway, you are correct. To do it like you are thinking you need a filter that tracks the output, a voltage controlled filter. There are plenty of schematics! its control input could be the output of a PWM module of your uCthe duty cycle of the PWM is the cutoff frequency. But it needs to be either of an high order or cut off at some octaves below the square wave to filter out as much harmonics as possible...why don't you do it the other way around then? generate the sine wave in the analog domain with a VCF auto oscillating, square it with a comparator. control the cutoff with the MCU and measure the square frequency to keep the sinewave "in tune"or... simplier... DDS, you know?

author: Ian.M
author_rank: Super Contributor
timestamp: March 07, 2017, 08:39:15 am
content: That would be extremely difficult to do with analog circuits. A F=>V converter driving a low distortion VCO with a PLL keeping it locked would be one approach but I don't think you'd get a three decade frequency range without switching timing capacitors.The best approach would be if there is sourcecode available to allow the firmware to be modified to control a DDS chip to generate both the sine and square waves, with filtering for the sine wave.Otherwise, realistically, it either needs a fast DSP capable MCU that can run a DDS routine for the sinewave, and a software PLL to keep it locked to the squarewave, or if you've got FPGA development experience it could be implemented with a digital PLL and hardware DDS.Its not for the faint-hearted - if you haven't done some serious DSP projects before, you will struggle.

author: JPortici
author_rank: Super Contributor
timestamp: March 07, 2017, 08:53:52 am
content: I'd wait and hear what the end application first.. you can do reasonably good DDS for sinewave, at audio frequencies, on almost any modern 8 bit mcu

author: BrianHG
author_rank: Super Contributor
timestamp: March 07, 2017, 08:58:51 am
content: For an analog sine wave VCO with the range you want, take a look at some of the old function generator ICs like:ICL8038MAX038XR-2206Your other choice might be a voltage output DAC and feed it a sine waveform, or an MCU with internal software to do the same.

author: Siwastaja
author_rank: Super Contributor
timestamp: March 07, 2017, 09:38:48 am
content: If the noise/distortion requirements are not special, many modern $1 MCUs capable of producing fast PWM (i.e., internal timer running at around 100 MHz, or so) would be able to simply PWM the 20kHz sinewave from a lookup table, using a simple RC filter.Many modern sub-$1 MCUs also include fast DACs, such as even the chepest in STM32 series, offering 1 MSPS 12-bit DAC.

author: Codebird
author_rank: Regular Contributor
timestamp: March 07, 2017, 09:55:30 am
content: How siney-do you need? Square to sine is rather tricky, but square-to-triangle is comparatively easy.

author: Fungus
author_rank: Super Contributor
timestamp: March 07, 2017, 10:12:34 am
content: Quote from: Chriss on March 07, 2017, 08:13:22 amThere's lots of tricks but it depends on what it will be used for.If you want a really nice sine wave for audio work you can get a signal generator, eg.:http://www.ebay.com/itm/331866160373

author: w2aew
author_rank: Super Contributor
timestamp: March 07, 2017, 04:14:22 pm
content: A common technique used in old analog function generators was a two step process. Convert the square wave into a triangle/sawtooth waveform using an integrator (easy with an op amp), and follow that with a PieceWise Linear wave shaping circuit, often using a multiple diode shaping circuit, where diodes are used to progressively shape the triangle into a near sine wave. Here are some sites to go check out:http://www-personal.engin.umd.umich.edu/~fmeral/ELECTRONICS%20II/03%25c3-Diodes/02a-Diode%20Waveshape%20Illus.pdfhttp://www.timstinchcombe.co.uk/index.php?pge=trisinhttp://www.ti.com/lit/an/snoa665c/snoa665c.pdfhttp://www.electronics.dit.ie/staff/ypanarin/Lecture%20Notes/DT021-4/5SignalShaping.pdf

author: Zero999
author_rank: Super Contributor
timestamp: March 07, 2017, 07:21:51 pm
content: Quote from: Chriss on March 07, 2017, 08:13:22 amWhat does that mean? Do you mean you want the sine wave biased at half the supply voltage, never going below zero?As you're using a micro controller anyway, why not simply generate a sine using PWM or some sort of DAC?If that's not possible, say you can't change the code on the existing MCU, then add another one to work out the frequency of the square wave and output the appropriate sinewave via PWM or a DAC.

author: Ian.M
author_rank: Super Contributor
timestamp: March 07, 2017, 07:56:23 pm
content: Quote from: w2aew on March 07, 2017, 04:14:22 pmThe quality of a piecewise linear sine shaper output is critically dependent on the input amplitude.Unfortunately, the effective gain of the square to triangle integrator is inversely proportional to frequency. The traditional way to handle that was to gang the adjustment of the integrator components with that of the squarewave oscillator, so the integrator gain tracked with the oscillator frequency, but that's no good for an external signal.One is left with the problem of designing an AGC circuit that can handle a 1000:1 range of input levels while maintaining near constant output. Even if you do forward correction with a F to V circuit and an analog multiplier that's still tricky, and you'll probably need a PID servo loop to trim the gain.Some improvement can be gained at the expense of considerable complexity by switching integrator capacitors by frequency decade (or even simply hi/lo), with some hysterisis so it doesn't jitter.Any deviation from an exact 50% duty cycle will cause the integrator output to 'walk' until it rails and distorts, so you'll also need another PID servo loop to inject a centering bias.Both PID loops will need tuning to handle the maximum possible rate of change of input frequency.That's an Eurocard protoboard or two of OPAMPs and precision passives, with a considerable price tag. Its not surprising we are pushing the O.P towards digital methods.

author: sentry7
author_rank: Regular Contributor
timestamp: March 07, 2017, 08:06:09 pm
content: Quote from: Ian.M on March 07, 2017, 07:56:23 pmIf we're talking about an ordinary op amp integrator, this walk can be stopped with a large discharge resistor in the feedback loop. That doesn't stop the frequency dependent gain, but at least it doesn't clip.

author: bson
author_rank: Unknown
timestamp: March 07, 2017, 08:13:57 pm
content: You could also find a simple DDS synthesizer and clock it with a multiple of your signal.

author: Ian.M
author_rank: Super Contributor
timestamp: March 07, 2017, 08:17:07 pm
content: Quote from: sentry7 on March 07, 2017, 08:06:09 pmYes, you can stop the walk that way, but it leaves a residual DC offset, which will distort the sine shaper output.

author: Christe4nM
author_rank: Unknown
timestamp: March 07, 2017, 10:38:23 pm
content: Unless we know more about the exact application it's hard to suggest solutions that fit for the OP. To me the first post reads as "I need a sine wave generator, of which the output frequency is the same as that of an incoming square wave."If so: you need a frequency detector for a square wave input. (Quite easily done in a microcontroller I think); you need a sine wave generator (for these low frequencies also very doable with a microcontroller either with PWM output or a DAC). And finally a way to get the output frequency matched to the input frequency. (Which again seems to me very doable in that same microcontroller)Unless I'm overlooking something when I say it's pretty doable with a microcontroller?However, this is all assuming I understand the OP correctly.So please tell us a bit more about what you want to achieve. What kind of requirements do you have on the sine wave? (like amplitude, distortion etc.) Where does that square wave come from (a microcontroller yes, but what kind of circuit is it part of) do you have any control over that microcontroller?Would it be possible to use the microcontroller generating the square wave to also generate the sine wave? If so, do you still need that square wave, or was it's only purpose as reference frequency for the sine? Etc...

author: BrianHG
author_rank: Super Contributor
timestamp: March 08, 2017, 01:35:10 am
content: Quote from: Ian.M on March 07, 2017, 07:56:23 pmFunny, the ICs I mentioned have all that built in... The first 2 will also give the full desired frequency range in 1 sweep.

author: Ian.M
author_rank: Super Contributor
timestamp: March 08, 2017, 02:08:51 am
content: Quote from: BrianHG on March 08, 2017, 01:35:10 amQuote from: BrianHG on March 07, 2017, 08:58:51 amUnfortunately they are all obsolete so you would have find someone reputable who's still go a few N.O.S., and none of them can directly generate triangle and sine waves from an external squarewave. The MAX038 is the closest as it has a phase detector and its VCO is designed to support phase locking it to an external signal, but you are still going to need to steer its base frequency close to the frequency of the waveform its to be locked to. That drops the size and complexity maybe to half a Eurocard.

author: BrianHG
author_rank: Super Contributor
timestamp: March 08, 2017, 02:39:04 am
content: I guess then I would use a cheap 64Mhz pic with dac. Use the timer input to time the source square wave & playback from either a huge sine table, or computing the sine sending it to the dac output pin running at high Khz. It will be something like an under 3$ solution, but, you will need to write some C code to get it running.

author: Ian.M
author_rank: Super Contributor
timestamp: March 08, 2017, 03:05:57 am
content: You dont really need a built-in DAC - just an 8 bit port and an external R/2R ladder into a low pass filter and buffer.A 64MHz 8 bit PIC only gets you 16 MIPS, which isn't a lot if you need to reproduce a 10KHz sine wave. 1600 instructions per cycle, and how many samples do you need to get an acceptable waveshape without having to design a 'brickwall' filter? I would expect anythong under 32 is going to be crappy, which drops it to 50 instructions per sample - just possible if you code in assembler. Bear in mind you have the period measurement and sample rate adjustment to keep it locked to the input waveform running psuedo-concurrently.A small dsPIC would be as better choice. dsPIC33FJ06GS001looks like a good candidate. in 18 pin package, 40 MIPS from internal oscillator, with input capture and output compare on a 16 bit timer to sync to the squarewave and generate the sampling clock, and a 10 bit buffered DAC to generate the sine wave.

author: David Hess
author_rank: Super Contributor
timestamp: March 08, 2017, 04:10:40 pm
content: Quote from: Ian.M on March 07, 2017, 07:56:23 pmIt is not quite this bad because it is not done that way. Instead the function generator is phase locked to the reference by adjusting the integrator's input current. Phase locking over a 1:1000 range is completely feasible this way. The output waveform after triangle to sine conversion is then only dependent on the performance of the function generator itself which handles the switching and amplitude compensation. I still would not recommend this method though because there are better ways. Note that at least in high performance designs, the integrator used is *not* the standard operational amplifier integrator which has bandwidth limitations; instead a diode bridge based current switch is used to drive an integration capacitor. I wonder how far this design could be pushed with modern parts.Software DDS is the obvious solution and up to 10kHz, I think this is feasible with careful programming. I have done this before with older lower performance microcontroller but not to produce a sine wave output would would mean using a precomputed lookup table but that should not be a problem.The alternative I would consider if a square wave output of 50 or 100 times the desired frequency is available is to drive an external switched capacitor filter which is clocked at 50 or 100 times the frequency of interest. The microcontroller drives the switched capacitor filter at 50 or 100 times the desired frequency and an external divide-by-50 or divide-by-100 drives the switched capacitor input from its clock. The antialiasing filter on the output will need to be switched to divide the frequency range up.

author: Ian.M
author_rank: Super Contributor
timestamp: March 08, 2017, 07:52:17 pm
content: The switched capacitor filter idea is worth looking at - its clock doesn't need to be an accurate 50x or 100x the squarewave, it just has to track it well enough to reliably filter the third harmonic. F=>V circuit controlling a VCO would do it, maybe with some automatic hi/lo range switching to reduce that 1000:1 frequency range to 32:1. Its not a great option compared to programming a suitable MCU, but for a one-off if you don't already have the toolchain for something suitable, it could make sense.