PWM Calculator

For a counter-based PWM on a given clock, find the PWM frequency, the number of duty-cycle steps, the resolution in bits and percent, and the smallest pulse. See the frequency-versus-resolution trade-off across counter widths, and get a ready-to-use parameterised Verilog module.

Results

PWM frequency
390.6 kHz
PWM period
2.56 us
Duty steps
256 (0 to 255)
Duty resolution
0.3906% per step
Smallest pulse
10 ns (one clock)

Frequency vs resolution @ 100 MHz

BitsStepsResPWM freqNotes
4166.25%6.25 MHzsmooth
5323.12%3.125 MHzsmooth
6641.56%1.562 MHzsmooth
71280.781%781.2 kHzsmooth
82560.391%390.6 kHzsmooth
95120.195%195.3 kHzsmooth
1010240.0977%97.66 kHzsmooth
1120480.0488%48.83 kHzsmooth
1240960.0244%24.41 kHzsmooth
1381920.0122%12.21 kHzsmooth
14163840.0061%6.104 kHzsmooth
15327680.00305%3.052 kHzsmooth
16655360.00153%1.526 kHzsmooth

Notes

Verilog PWM module
// Counter-based PWM, 8-bit duty. out is high while cnt < duty.
// Generated by libfpga.com/tools/pwm-calculator
module pwm #(parameter W = 8) (
    input  wire         clk,
    input  wire [W-1:0] duty,   // 0 = always off, 2^W-1 = (almost) always on
    output wire         out
);
    reg [W-1:0] cnt = 0;
    always @(posedge clk) cnt <= cnt + 1'b1;
    assign out = (cnt < duty);
endmodule

This exact result is bookmarkable, the URL contains all your inputs. Need it in a script? Append &format=json (API docs).

About this tool

A counter-based PWM ramps a counter 0 to 2^N-1 and drives the output high while the counter is below the duty value. One full ramp is one PWM period, so the PWM frequency is clock / 2^N: every bit of duty resolution you add halves the PWM frequency. That's the fundamental trade-off, fine dimming steps or a high, flicker-free frequency, pick two and you can't have the third without a faster clock. This finds the numbers and generates the module; the PWM recipe explains the idea.

Related tools