Generating high frequency tones

Hi all,
I want to generate high frequency tones (20kHz and up). Looking at programs like the Arduino DDS signal generator (http://interface.khm.de/index.php/lab/experiments/arduino-dds-sinewave-generator/) though, it looks like it's limited to a 32kHz sampling rate, which means a 16kHz max output frequency. Why is this?

Think it is because it generates a sine wave , if you want a square wave you can easily go much higher:

void setup()
{
  pinMode(13, OUTPUT);
}

void loop()
{
    digitalWrite(13, HIGH);
    // add microdelay or delay here
    digitalWrite(13, LOW);
    // and here 
}

by using direct port manipulation and/or timers you can even go higher.

If you are talking about generating sinewaves then you need to look at how audio sampling works. To generate simple square waves, you can go up to 8 MHz as robtillaart says.

From this page:

The above image shows the general idea. We need to sample the sound we are trying to produce (eg. a sine wave) at at least twice the frequency we desire, see:

So to get 20 kHz we need 40 kHz samples (CDs BTW sample at 44.1 kHz).

That is, a sample every 25 mS. uS.

(edit) Corrected on AWOL's advice.

Now looking at the spacing of the dots vertically the samples need to be able to reproduce a range of values, or we are back to having a square wave. Say, 256 different values (as in the linked page you gave). That is in fact only "8-bit sampling". BTW the Wikipedia page says that "Eight-bit audio is generally not used due to prominent and inherent quantization noise". So we really should use more than 8 bits.

But sticking with 8 bits for now, to generate the 256 different voltage levels required, we need to use PWM (pulse width modulation) where a square wave is produced with a variable duty cycle.
See: Pulse-width modulation - Wikipedia

For example a duty cycle of 128 would be "half voltage" and duty cycle of 64 would be "quarter voltage" and so on (assuming this is out of 256).

Now for the internal timer to do this, it has to count up to 256. And the fastest it can do that is at the clock rate which is 62.5 nS.

62.5 nS * 256 = 16 uS.

So how many times can we count up to 16 uS in one second? 1/0.000016 = 62500. Therefore the absolute maximum sampling frequency, with a 16 MHz clock, and 8 bit samples, would be 62.5 kHz.

There is another factor ... for the individual samples to be "in phase" the authors of that sketch have used "phase correct" PWM mode. This mode centers the "on" cycle, rather than having it at one edge. But it takes twice as long to do phase-correct PWM mode, because the timer counts up, and then down again. So we have to divide the figure we got by 2.

62.5 kHz / 2 = 31.25 kHz.

So that's why the maximum sample rate is around 32 kHz. Can't argue with the laws of physics. :wink:

I am also a newby in using Arduino and I would like to ask you what is the voltage differece between HIGH and LOW in the above sequence? What if I would like to generate a signal that gows below 0, like +5v/-5v, instead of +5v/0v? What should I use instead of HIGH And LOW?

You can't, directly. The Arduino operates between 0V and +5V.

You would need some external circuitry, with a negative power supply.

"What if I would like to generate a signal that gows below 0, like +5v/-5v, instead of +5v/0v? "

You could take the output signal and pass it thru a capacitor. The cap prevents DC levels from passing thru. So a 0/5V signal would in effect become a +/-2.5V signal.
If you wanted +/-5V then you need to amplify that signal, or buffer (or amplify) the signal with a part that is powered from +/-5V.

So to get 20 kHz we need 40 kHz samples (CDs BTW sample at 44.1 kHz).

That is, a sample every 25 mS.

Ahem!

AWOL:

So to get 20 kHz we need 40 kHz samples (CDs BTW sample at 44.1 kHz).

That is, a sample every 25 mS.

Ahem!

Er, every 25 uS.

Yes, mS, uS, whatever it takes :wink:

You know, looking at this diagram that I got from Wikipedia, I'm starting to doubt that the red line has anything to do with the pulses shown by the blue line. What do you think?

I think it's a specific example, rather then a generic PWM signal. The text associated with the image says

An example of PWM in an AC motor drive: the phase-to-phase voltage (blue) is modulated as a series of pulses that results in a sine-like flux density waveform (red) in the magnetic circuit of the motor. The smoothness of the resultant waveform can be controlled by the width and number of modulated impulses (per given cycle)

I guess it depends on what 'AC motor drive' means.

Between the 5 and 10 mS mark the blue line is negative, but the red line is positive. How does that work? Unless it is out of phase because of the smoothing capacitor.

You are misinterpreting. The Redline is changing in level by the width of the blue pulse.
Positive pulses, the level goes up.
Negative pulses, the level goes down.
The amount the level is changing is determined by width of the pulse.