3

I want to build a circuit that can generate sine waves at frequencies controlled by a computer or a microcontroller. I would like to go as low as 0.01 Hz (down to 0.05 Hz would probably be acceptable) and as high as 100 kHz; I need at least 10 frequencies per decade, which is a resolution of roughly 12 mHz at the bottom of the scale (or 60 mHz if the bottom is 0.05 Hz). The output voltage doesn't need to be high - I'm going to run it through some op-amps afterwards anyways.

What is the standard way of doing this? Is DDS the way to go (using either a sine lookup table on a uC or an IC like the AD9850)? Is there a simple analog solution that I'm missing?

I would love to stick to through-hole parts if I can.

Fizz
  • 14,355
  • 2
  • 43
  • 97
Greg d'Eon
  • 3,807
  • 4
  • 24
  • 50
  • 2
    My knee-jerk reaction would be to use a DDS for this. By the way, what's your desired resolution in frequency? – Nick Alexeev Jan 05 '15 at 19:47
  • Yeah for the low frequency end you can forget analog. There is always a transient/ turn-on with an analog oscillator. It will last at least a few cycles. For ~$400, Rigol makes a nice sig. gen. that does milli-Hertz to 20 MHz. – George Herold Jan 05 '15 at 19:53
  • 2
    TI's [AN-263 Sine Wave Generation Techniques](http://www.ti.com/lit/an/snoa665c/snoa665c.pdf) has a nice "menu" on p.4 from which you can pick your favorite depending on the freq. range. For yours there are several designs possible. Distortion and stability are also among the factors to consider when picking a solution (but you haven't touched that). – Fizz Jan 05 '15 at 19:55
  • @NickAlexeev: I added some information on the resolution. I need at least 10 points per decade, so the second lowest point is ~12 mHz higher than the lowest. – Greg d'Eon Jan 05 '15 at 19:55
  • Alas, your requirement to have a 10M:1 sweepable range (this is how I interpret "controlled by computer") kinda excludes most single-chip analog classics like the XR-2206, ICL8038 and so forth (most of which have been obsoleted by their manufacturers, but can still be found as surplus)... unless you can live with multiple adjument pots as shown for example at https://www.youtube.com/watch?v=KPtsgFw5Fno You could presumably build the micro-controlled equivalents of those pots, but it's not as easy as sending bits to a DAC. On the other hand, these oldies are available in DIP. – Fizz Jan 05 '15 at 21:17
  • If I understand "sweepable range" correctly, I think that's what I need. I'm picturing a microcontroller sending a couple bits and a sine wave appearing until the uC tells it to change frequencies. Manually controlled pots aren't useful here and digitally controlled pots sound like an excellent source of frustration. – Greg d'Eon Jan 05 '15 at 21:28
  • Thank you for all the effort @Fluff! I'll take a close look at these options as soon as I get a chance. – Greg d'Eon Jan 05 '15 at 22:55

4 Answers4

5

You could easily do a highly flexible DDS on an STM32F4 Discovery board for ~$15 using an internal DAC, DMA, a timer and a sine lookup table. People have blogged examples of similar things if you look for them.

In order to get the required frequency (the STM32F4xx DAC's can only do 300ksps at full swing, which equates to ~1V/\$\mu\$s) you will need to constrain the range of the DAC.

For a sinusoid, maximum slew rate (in V/s) is \$2 \pi \times f \times A\$. Inserting the values from the OP's requirements and assuming Vref of the DAC is 3.3V (which if I recall correctly is the case for an STM32F4Disco), a 3.3Vpp sine wave at 100kHz, max slew rate is \$2 \pi \times 10^5 \times 3.3 / 2 = 1036726\$V/s, or \$\approx1.04\$V/\$\mu\$s.

In order to comply with this constraint, the sine wave's amplitude has to be limited, from \$3.3\$V to \$3.3 \times 1 / 1.04 = 3.18\$V.

This now brings in the apparent issue of the 300ksps limit on the STM32F4 DAC. That limit is a furphy. I don't know what the actual physical upper limit is, but I suspect it's the APB1 bus speed. I do know (because I've done it) that you can write at least 2Msps to the DAC and as long as you respect the 1V/us slew rate it will behave predictably. So, you can do 16 samples per cycle for a 100kHz sine wave, a 1.6Msps update rate, as long as you limit the sine wave amplitude to 3.18V instead of 3.3V.

Or, forget the DAC, and use I2S instead. The STM32F411 datasheet says

"Up to 5 SPI/I2Ss (up to 50 Mbit/s, SPI or I2S audio protocol), SPI2 and SPI3 with muxed full-duplex I2S to achieve audio class accuracy via internal audio PLL or external clock."

At some point you're probably going to have to deal with digital pots because the reality is that if you want a smooth, continuous output signal, particularly at low speed, then you're going to have to run your DDS through a filter, and that filter is going to have to be adjustable - you can't just put a 100kHz LPF on it and expect it to be useful at sub-1Hz frequencies.

johny why
  • 166
  • 8
markt
  • 4,936
  • 2
  • 15
  • 13
  • Great information. You're right about the low pass filter - I'll have to experiment and see what works for the sub-1 Hz range. – Greg d'Eon Jan 05 '15 at 23:27
  • Hmmm, does Rigol and other inexpensive kit really try and LPF the milli-Hz stuff? At 10 bit what sort of TC do I need for 1 Hz? (~1ms?) And doesn't the filter also make a phase shift? – George Herold Jan 06 '15 at 00:09
  • @GeorgeHerold I have no information about how (or if) Rigol deals with filtering. TC? – markt Jan 06 '15 at 00:16
  • Oh sorry, of course you don't. Does anyone filter milli-Hz DDS sine waves? TC = time constant. (R*C) (I'll look at the rigol tomorrow.) – George Herold Jan 06 '15 at 00:21
  • I thought you meant time constant, but better to be sure... short answer is a bloody long one! Re phase shift, yes of course, but the fundamental is a pure tone (except as you change frequencies) so the phase shift shouldn't matter. It depends on the application though. – markt Jan 06 '15 at 00:35
  • Well, I can't say I dislike this solution, because you're turning it into a software problem. A DDS also consists of a sine lookup & DAC but basically has the table sweeping "software" hardcoded in its ASIC. If you already a compatible uC, a DDS on a PCB/shield is only around $5... but on the other hand if already have a uC with built-in DAC, the cost is $0 (because the software is free.) – Fizz Jan 06 '15 at 00:42
  • After thinking about this a bit more, there seems to be a serious problem with the DAC's speed. With 300KSPS you only get to output at most 3 different voltages per period at 100KHz. And I don't see how you can shape a reasonably looking sine wave with that low a sampling rate. At best it will be a triangle shape. I don't see how that can be corrected by subsequent analog stages unless you implement a triangle-to-sine converter... which basically renders moot the use of a DAC. – Fizz Jan 06 '15 at 02:12
  • @RespawnedFluff The DAC can only run at 300ksps _at full swing_, so for Vref=3.3V it has a max slew rate of ~1V/us. A 3.3Vpp sine wave @ 100kHz has a slew rate of ~1.04V/us, so by restricting it to a 3Vpp range it can meet the 1V/us limit. I actually mentioned this (in less detail) in my post; I'll edit it to expand the point. – markt Jan 06 '15 at 06:29
  • 2
    Regarding filtering, it may be possible to use a fixed frequency lpf tuned for the high frequencies, and dither the lower frequency case at high frequency. That is, if output value is supposed to be, say, 4.3 lsb, output 4454454445 to dac. This increases resolution as well. – Nicolas D Jan 06 '15 at 08:36
  • @NicolasD That's a nice use of sigma-delta, great idea. – markt Jan 06 '15 at 09:42
  • By the way, ST [has an app note #AN3126](http://www.st.com/web/en/resource/technical/document/application_note/CD00259245.pdf) on doing exacly this (waveform generation, including sine wave) using the STM32. – Fizz Jan 10 '15 at 13:31
2

Here's my (RF's) rough summary of the DDS solution, which was initially suggested by Nick Alexeev. I'm marking this as community wiki, so feel free to improve with details etc.

Using AD's calculator, in order to get the desired balance between the 12mHz frequency resolution (which I'll just round to 10mHz) and a resnoably smooth curve at 100KHz output, you need about 10 to 40Mhz DDS clock, with 20Mhz looking like a good middle ground. At lower than 10Mhz clock, the output of the DDS at 100KHz starts to look quite noisy. At higher than 40Mhz clocks, the resolution gets too coarse; at 40Mhz clock you get a little less than 0.01Hz change for every tuning bit, in the low range of course; the tuning word being a clock divider, it produces a non-linear frequency curve.

As for practical solutions, one can find DDS chips already mounted on Arduino-compatible shields (for around $5 it seems); the catch however is that they may come with a fixed (and possibly unsuitably high) clock, but that can be improved upon with some board rework.

Also worth mentioning is that if one is using a microcontroller (uC) that has a built-in and sufficiently speedy DAC, then adding a DDS (which is basically an ASIC sweeping a sine lookup table and sending that indexed value to a DAC) is rather pointless since the sine lookup can be implemented in the uC software. For more details on this integrated-DAC approach see markt's answer.

Fizz
  • 14,355
  • 2
  • 43
  • 97
2

I've used a design for a non-digital oscillator that produced decent sines down to below 0.1Hz and to above 100kHz. Theoretically it was an LC parallel tuned circuit in the feedback path of an amplifier. The L and the C were actually precision gyrator circuits (which is why they were able to be tuned to such ridiculously low resonant frequencies). You'd never build one with real inductors and if you did it would be non-tunable.

Both "C" and "L" were kept tracking by using a precision dual-gang pot and, if I remember correctly, a frequency range of over 50:1 could be got from the pot before changing fixed capacitors in the gyrators.

The pots of course would be better served by digipots these days as these can "track" much better than regular pots. I'm sure with care, it could work down to 0.01Hz.

Inductor gyrator: -

enter image description here

There are higher quality gyrators (better frequency response) and here's another link to a document about a different sort of gyrator. The beauty of the single op-amp gyrator in this application is that the CR network (as normally intended to be parasitic) can be used effectively as the tuning section making the L and C in parallel via small series resistors.

pipe
  • 13,748
  • 5
  • 42
  • 72
Andy aka
  • 434,556
  • 28
  • 351
  • 777
  • I've never heard of gyrators before. This is really cool stuff, but it sounds a lot more difficult to get right than a DDS solution. – Greg d'Eon Jan 06 '15 at 13:24
  • 1
    Take a look at the additions and the link. – Andy aka Jan 06 '15 at 13:38
  • That link is extremely helpful. I'm still a bit scared of trying to do this in the analog world, but this looks like useful info. – Greg d'Eon Jan 06 '15 at 13:46
  • Sure, this is one of the several analog solutions that would work... but this is going to be just as difficult to interface to a computer as a XR-2206 etc., which Kynit rejected off the bat in the comments below his question. Are there DAC-controlled gyrators that would make this easy for his application? – Fizz Jan 06 '15 at 14:21
  • @RespawnedFluff digipots would allow control as said in my answer. – Andy aka Jan 06 '15 at 14:37
  • I think digipots would work here. I just think I'd be happier sticking with timers and interrupts than something I'm unfamiliar with. This looks like a great solution that can work as well as DDS for someone with a bit more filter design experience than me. – Greg d'Eon Jan 06 '15 at 14:45
  • @Kynit your skills and experience are an important factor of course – Andy aka Jan 06 '15 at 14:57
  • I guess Kynit changed his mind about digipots. 17 hours ago he wrote "digitally controlled pots sound like an excellent source of frustration". – Fizz Jan 06 '15 at 15:06
  • For the cost of about ~5.5 cheapest digipots (at 40 US cents each in 3000+ orders) carried by DigiKey, you can buy the cheapest DDS (about $2.20 in 1500+ quantity, max 16Mhz) but which would work here, so the analogue solution the digipots would be controlling would need to be outstanding to be worthwhile. (The unit prices for DYI/1-piece orders are higher for both of course... and sure there are price variations between distributors, but this is a good benchmark, I think.) So after looking at the relative cost side, I see why the old analog function-gen ICs have been swept from the market. – Fizz Jan 06 '15 at 15:56
0

One approach would be to clock a 28bit adder/accumulator with a 4 Mhz clock. A uController driving the low order 20bits with a digital word and the top eight bits feed addresses to an eprom for a 256 entry sine look up table. The data bits from the eprom summed into an R2R ladder would capacitively couple to your op amp gain stage. Drive a high value from the micro into the adder and the high order bits cycle faster, for a higher frequency sine wave. A low value takes a lot more clock cycle to step make a count occur in the top eight bits driving the eprom. With a little extra work in the micro software; you can make the eprom table smaller by just storing angles from 0degress through 90. Just read the table forwards for the first 90 degrees (quadrant 1) then backwards for quadrant 2 forward again for quadrant three etc.

steverino
  • 545
  • 3
  • 14
  • I'm guessing the "selling point" of this solution is avoiding to buy/use a DAC IC and instead implementing it as that R/2R ladder? – Fizz Jan 06 '15 at 01:05
  • @RespawnedFluff No sales pitch. I just thought of this approach because of the through hole parts comment. – steverino Jan 06 '15 at 01:15
  • Well, DACs in DIP package are still available for sale from multiple manufacturers... – Fizz Jan 06 '15 at 01:38
  • So I'm using a timer onboard the uC to increment a counter off the uC to figure out which voltage to output? Why not keep it all onboard? And why a ladder instead of a DAC? – Greg d'Eon Jan 06 '15 at 03:04