Generating 100-500khz PWM with Arduino Mega

I have to generate PWM upto 500khz with a variable duty cycle. Can I have a code for it :-[

You can write the code for it. 500 kHz is 1/32nd of the 16 MHz clock frequency so you will only get 32 levels of PWM at that rate. At 100 kHz you'll get 160 levels of PWM.

The PWM frequency is set by TOP and prescale. At those frequencies your prescale will be 1 and your TOP will be between 31 and 159. Read about "Fast PWM" in the processor datasheet.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html .

What is the application that requires a variable PWM frequency?

Thanks.. Tom... :slight_smile:

TomGeorge:
What is the application that requires a variable PWM frequency?

I am working on a project of 3 phase SPWM inverter. Its my first step with the frequency generator for the switching of MOSFET in boost converter. I could not find a code for it. Frquency of upto 250Khz would do the work.

moizurrehman4:
I could not find a code for it.

It's a bit like finding a pot of gold at the end of a rainbow.