Generate Square Wave up to 100kHz


I was curious if anyone can confirm what I've learned about the resolution of square wave generation of arduinos.

I am interested in generating square waves up to 100kHz. A really simple way I have found to accomplish this uses the timer pin OCR2A to generate a square wave. The simplified formula for setting the timer is:

OCR2A = (clock frequency / 2) / output frequency

With a 16MHz clock, and using 100kHz for frequency: OCR2A = 80. But the function only accepts whole integers, so the next closest higher frequency is when OCR2A = 79 which is 101266 Hz.

That's a resolution of 1266 Hz! I was hoping to have a resolution of 250 Hz or less for my project.

Does anyone know if the arduino is capable of better resolution through perhaps another method?

The resolution limited by the period of a clock cycle (62.5nS @16MHz). You get an integer 160 of these in a 100Khz pulse.