I wonder if I can customize the PWM frequency of Arduino. By searching the Internet, I can only find some posts teaching you to divide the clock speed by some power of 2. However, let's say I want the PWM frequency to be some arbitrary number like 130kHz, 250kHz, 380kHz, etc. Is that possible for Arduino? Or, I can only achieve that by using a more advanced microcontroller (for example, from Texas Instruments)?
It's also possible to use tone() from the Arduino library if the person who asked this question does not care about controlling the duty cycle of the PWM waveform.
Thank you for your reply. Is your code also able to generate much higher frequency, between 100k Hz to 500k Hz? Or, is there a frequency limit (upper bound or lower bound)?
The function is from the Arduino Reference page itself so I presume that it would work for any Arduino compatible board. I can confirm that the function works for the Arduino Uno and Arduino Mega 2560.
If you do not mind being restricted to a PWM duty cycle of 50%, the tone function will be sufficient for your purpose.
I think you are assuming that he is using fast PWM with TOP = 255. It is possible to use fast PWM with TOP = OCRXA where OCRXA can take any value from 0 to 255 for timer 0 and timer 2.
You are correct that you need TOP = 255 for 8 bit duty cycle resolution. Looking at the table below, if PWM modes 10, 11, 14 or 15 are used, the TOP value can be set to less than 255 for a frequency up to 8 MHz (if duty cycle resolution is not important).