Square wave generator help

Hello all,

I am trying to code a square wave generator that will allow me the ability to adjust the frequency and the pulse width.

Is it possible to achieve 20,000 cycles per second?

I have been using delays and analogReads to generate pulses but, I am only able to get about 1 cycle for every 2ms. If I am correct this only equates to 500 cycles per second. Is that right?

If the runs at 16Mhz, it should be able to generate a 20khz wave with ease, right?

Thank you for your help in this, I really appreciate it.

i can't answer directly maybe you find something interesting here http://interface.khm.de/index.php/lab/experiments/arduino-realtime-audio-processing/


I guess you mean analogWrite.

The default PWM frequency is 500Hz as you saw.

This can be changed quite easily for some values.

Search the forum to find out how to alter the PWM frequency.

Using the counter/timer your maximum frequency would be 8MHz if your system clock is 16MHz and you set the I/O clock source to be the system clock.

(* jcl *)

www: http://www.wiblocks.com twitter: http://twitter.com/wiblocks blog: http://luciani.org