Square wave in different frequencies ?

I know its a subject that already came up, but still there is no one truth for it in google I would like to be able to output a square ( or PWM) on any digital pin , and set its frequency 0-20Khz .

Is there a library to do exactly that ? can it be done in background using timers ? will it affect millis() ?

Seems that there is no pure solution- one that simply do that, and not affecting the whole system.


tone(pin,20000); // min freq 31 hertz


Any one of the timer modules can automatically generate square waves in that frequency range, with no effect on the "whole system". See the processor data sheet, timer section, or this tutorial.