1
\$\begingroup\$

I want to build a signal generator using a ATmega 328P on an Arduino. Which way would be better. Using one of the timers (8bit or 16bit) built in to generate a PWM and then low-pass filter it, or using a R2R ladder system?

I care more about harmonics than frequency, but I'm hoping for about 10kHz or above.

Thanks for your advice.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ If you care about signal clarity, go for a DAC, not PWM. \$\endgroup\$
    – Dzarda
    Sep 3, 2015 at 10:45
  • \$\begingroup\$ And use DDS software if you want high resolution. \$\endgroup\$ Sep 3, 2015 at 10:54

2 Answers 2

2
\$\begingroup\$

If you are worried about harmonics, you should definitely avoid PWM. Rectangular waves are jam-packed with harmonics that extend as far as pin slew rates permit. Although low-pass filtering will get rid of the higher order stuff easily, the lower order stuff will be harder to remove without some serious filtering. You don't specify how much harmonic distortion you could live with or what frequency range and waveshapes you are trying to generate (10kHz makes me think that you are trying to make an audio waveform generator), but you're fighting a losing battle that is going to end up with you needing external active components anyway - so why not add a DAC?

I'm also going to guess that you don't want to write any code, so a quick Google shows that Adafruit offer a MCP4725 breakout board with a prewritten library and tutorial. The MCP4725 is a 12-bit DAC. Even though its INL leaves something to be desired, you'll be a good order of magnitude better off than even your best efforts with the onboard PWM.

As Leon Heller says, if you really want to generate high quality signals (and over a much broader frequency range) you could go for a DDS solution. Analog Devices do some nice ones with onboard SRAM for arbitrary waveform storage, so you simply set the chip up, tell it to go and you're done - no need for a constant datastream. This is probably beyond your requirements at this stage, however. There are also a bunch of cheap Chinese boards based on the AD9850 all over eBay which might be of interest.

\$\endgroup\$
2
  • \$\begingroup\$ Thanks for answering. I should have given more info. I'm looking for a general purpose signal generator. I'm also trying to learn as much as I can so writing code isn't a problem at all. This also means I would steer into a more DIY solution than a breakout board but it's not out of the question. My main point was that I couldn't find clear info on which is better PWM vs R2R. I don't have years of experience with electronics so the 10kHz was pulled out of the air. It just seamed to be something the ATmega328 should be able to handle without much distorsion. Though higher freqs may be useful. \$\endgroup\$ Sep 3, 2015 at 12:01
  • 2
    \$\begingroup\$ If you're looking to learn, then I would try both the PWM technique (as it's quick to do and you will soon see why it's performance is poor) and the DAC technique. You will have lots of control over what's going on, and you will be doing more of the signal synthesis than you would be controlling an external DDS chip. From there you can move on to more sophisticated outputs if you so wish. This shows someone attempting pretty much the same thing that you are - worth looking at. \$\endgroup\$
    – stefandz
    Sep 3, 2015 at 12:08
2
\$\begingroup\$

It's hard to say definitively which one is better, as it depends on how you implement either technique. In general I would say PWM is the way to go, as R2R requires a lot of pins, well matched resistors, is rarely used for much more than 8-bits, and still needs filtering.

As mentioned, I'd go for a dedicated DAC if you want good results, but if it's for learning purposes going the timer/PWM module route with a decent filter plus opamp should give half decent results and you will pick a few things up in the process. There are plenty of tutorials on PWM signal generators out there to gt you started.

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.