2

I'm trying to convert the project here to an Arduino Nano.

I scaled the waveforms from 12 bit to 8 bit values. I replaced the analogReadResolution and analogWriteResolution calls with pinMode(10, OUTPUT) and pinMode(11, OUTPUT) and changed the two analogWrite calls to write to pins 10 and 11. Otherwise the program is unchanged.

My thinking (and this is where my poor electronics skills come into play) is that I just need a simple low pass filter to convert the PWM into the appropriate waveform (which, at startup, ought to be a sine wave on both pins). According to the article, the frequency generator tops out at 170Hz. I figure the PWM signal is going to be significantly higher than that, so I use R=3.3K and C = .1uF for my RC filter which ought to give me a -3dB at about 480Hz, but what I'm seeing on my scope is a mess. Not a stable signal at all.

This is the waveform I'm seeing on my scope:

OscopeImage

Can someone educate me?

Update: BTW, this is the initial waveform which ought to be a sine wave.

2

What you're proposing can work, but the PWM frequency needs to be much higher than the frequency you're trying to generate. If you look at the PCMAudio sample you'll see that it tunes the PCM frequency almost as high as it will go.

  • Can you elaborate on "much higher"? PWM on pins 5 and 6 is 980Hz. What sort of maximum frequency can I expect from the signal generator based on that? And how would I want to set up the low pass filter to clean up the signal? I've read that DDS article, but I have no inductors in my collection of components, so I was trying to make an RC filter instead. – Pete May 25 '15 at 17:03
  • At a PWM frequency of 980Hz I wouldn't expect anything above 5Hz with any sort of fidelity. – Ignacio Vazquez-Abrams May 25 '15 at 17:07
  • Oh, that's far too low for my needs. I was aiming for simplicity, but a better approach may be to use the digital outputs and create a DAC with a resistor ladder. Thanks for the education, I guess I'll have to start over. – Pete May 25 '15 at 17:12
  • You can tune the PWM as high as F_CPU/256, i.e. 62.5 kHz with a 16 MHz clock. – Edgar Bonet May 25 '15 at 17:17
3

The PWMs on pins 10 and 11 of the Arduino Nano run at 488 Hz. That's what you are seeing on the scope. The code you are using was meant for an Arduino Due, which has real digital-to-analog converters.

Edit: You can tune the PWM to run as high as F_CPU/256 by reconfiguring the timers 1 and 2 to “fast PWM” mode, instead of phase correct PWM, and setting their prescalers to 1 instead of 64. This should raise the PWM frequency to 62.5 kHz, given that the Nano has a 16 MHz clock.

The following code should do exactly this. You can put it somewhere inside your setup(). Warning: I have not tested this code. This will change the PWM frequency on pins 10 and 11, but also on pins 3 and 9, which share the same timers. It will probably break the tone() Arduino function.

// Set PWM frequency to 62.5 kHz on pins 3, 9, 10 and 11.
TCCR1A = 1<<WGM10;
TCCR1B = 1<<WGM12 | 1<<CS10;
TCCR2A = 1<<WGM21 | 1<<WGM20;
TCCR2B = 1<<CS20;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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