Toolvito

Works on this device

Pwm Calculator

PWM (pulse-width modulation) switches an output on and off quickly. The share of time it is on, the duty cycle, sets the average power or voltage. This calculator works with the timer that makes the PWM, the voltage it gives, and the RC filter that smooths it.

Enter your values

For the PWM output of a microcontroller timer: the frequency, duty cycle and resolution from its settings.

Your values

The clock that feeds the timer, for example the CPU or peripheral bus clock. Check the clock tree in your data sheet.

Type 1 for no prescaler.

The value at which the counter turns round, for example 255 for an 8-bit timer that runs through all its values.

The value that sets the duty cycle: a compare, capture-compare or duty register, depending on the maker.

Most timers that restart at a register value count TOP + 1 ticks. Phase-correct and centre-aligned modes count up and down.

Timers differ here by one tick. The PWM chapter of your data sheet says which way yours works.

Your values are worked out in this browser. They are not sent to a server.

Your result will show here

Fill in the values, then choose Calculate.

How to use this calculator

  1. Choose what you want to work out: the PWM from timer values, the timer values for a PWM, the resolution, the average voltage, or the ripple of an RC filter.
  2. For timer modes, enter the clock that feeds the timer and choose how its counter counts. Counting up and starting again is often called fast PWM; up and down is phase-correct or centre-aligned PWM.
  3. For counting up, choose when the output is high. Timers differ here by one tick; the PWM chapter of your data sheet says which way yours works.
  4. Choose Calculate. The timer settings search shows every prescaler in a table.

The formulas

  • Fast PWM (counting up): f = clock ÷ (prescaler × (TOP + 1)); duty cycle = (compare + 1) ÷ (TOP + 1) or compare ÷ (TOP + 1), depending on the timer.
  • Phase-correct PWM (counting up and down): f = clock ÷ (prescaler × 2 × TOP); duty cycle = compare ÷ TOP.
  • Resolution = log2(TOP + 1) bits. At a fixed clock, a higher PWM frequency gives a lower resolution.
  • Average voltage = low + D × (high − low), with D the duty cycle as a share. RMS = √(D × high² + (1 − D) × low²).
  • RC filter ripple (steady state): V × (1 − a)(1 − b) ÷ (1 − a × b), with a = e^(−t_high ÷ RC) and b = e^(−t_low ÷ RC). For RC much longer than the period it is close to V × D × (1 − D) × T ÷ (R × C).
  • Cut-off frequency of the filter = 1 ÷ (2π × R × C).

Worked examples

Arduino Uno style. Clock 16 MHz, prescaler 64, TOP = 255, compare 191, output high up to and including the compare value: f = 16,000,000 ÷ (64 × 256) = 976.5625 Hz, and the output is high for 192 of 256 ticks, a 75% duty cycle. Counting up and down instead gives 16,000,000 ÷ (64 × 2 × 255) = 490.2 Hz.

20 kHz PWM. At 16 MHz with no prescaler, TOP = 16,000,000 ÷ 20,000 − 1 = 799, so there are 800 steps: a resolution of log2(800) = 9.64 bits. For a 10-bit resolution the highest frequency is 16,000,000 ÷ 1024 = 15.625 kHz.

Filter. A 5 V, 1 kHz PWM at 50% into R = 10 kΩ and C = 1 µF: R × C = 10 ms. The output averages 2.5 V with a ripple of 0.125 V peak to peak. The cut-off is 15.9 Hz, and after a change the output needs about 46 ms to settle within 1%.

Limits you should know

  • Register names and the exact PWM modes differ between makers. The data sheet of your microcontroller decides; no prescaler list is built in.
  • The average and the filter assume ideal levels and instant edges, and an unloaded filter. A load on the filter capacitor lowers the output.
  • The filter mode covers a single RC stage. For an LC filter, see the Low Pass Filter Calculator.
  • For the duty cycle of any signal from its times, see the Signal Duty Cycle Calculator. For PWM made with a 555 timer, see the 555 Timer Calculator.

Related tools