PWM duty-cycle, frequency and period calculator

Enter numbers in decimal or scientific notation, such as 20000 or 2e4. Changing a unit reinterprets its number.

No valid result to draw.

Show the steps

Interactive calculation is unavailable until JavaScript loads. The formulas, worked examples and references below remain available.

Calculate PWM frequency, period, duty cycle, on-time and off-time, the average and RMS level, and timer counts for a clock.

How to use

Choose which two values you know: frequency and duty cycle, period and on-time, or frequency and on-time.

Enter them with their units. The duty-cycle slider moves the duty in 1% steps; type a value for finer steps.

Optionally enter the high level voltage to see the average and RMS level.

Optionally enter the timer clock and prescaler to see the counts per period, TOP and compare values.

Read the period or frequency, duty cycle, on-time and off-time. The waveform shows two periods and the average level.

Use Copy result for a labeled summary. Reset restores the starting values: 1 kHz, 25% and a 5 V high level.

Science

A PWM signal switches between a high and a low level at a fixed frequency. The period is the time for one full cycle, the reciprocal of the frequency. The duty cycle is the fraction of each period spent high.

When the low level is 0 V, the average of the waveform is the duty cycle times the high level. Motors, LEDs viewed by the eye and RC filters respond to that average. The RMS value, the high level times the square root of the duty cycle, sets heating in a resistive load.

A microcontroller timer makes PWM by counting clock ticks. In an edge-aligned up-counting timer one period takes TOP + 1 counts, so the frequency is the timer clock divided by the prescaler and by TOP + 1. The duty cycle can only change in steps of one count, so a short period leaves fewer duty steps. When the counts are not a whole number, the timer cannot make the frequency exactly, and the calculator says so before the nearest setting.

Limitations: this calculator assumes ideal edges and a 0 V low level. Center-aligned or phase-correct timers count up and down and run at half the frequency for the same TOP. Some timers, such as the ATmega328P in fast PWM mode, hold the output high for one more count than the compare value. Check your timer's reference manual. It does not map servo pulses to angles or design 555 timers. For converting frequency, period and wavelength units use the frequency, period and wavelength converter.

Formulas

Period: T = 1 / f. f is the frequency in hertz and T the period in seconds.

Duty cycle: D = ton / T. ton is the on-time; D is shown in percent.

Off-time: toff = T − ton = (1 − D) × T.

Average and RMS: Vavg = D × VH and Vrms = VH × √D, for a high level VH and a 0 V low level.

Timer counts: TOP + 1 = fclk / (P × f), rounded to a whole number, where fclk is the timer clock and P the prescaler. Compare value = D × (TOP + 1), rounded.

Resolution: Duty resolution = log2(TOP + 1) bits; one count is 100 / (TOP + 1) %.

Examples

1 kHz, 25%, 5 V high level: Period 1 ms, on-time 250 μs, off-time 750 μs; average 1.25 V, RMS 2.5 V. T = 1 / 1000 Hz = 1 ms; 25% of 1 ms is 250 μs. 0.25 × 5 V = 1.25 V; 5 V × √0.25 = 2.5 V.

Period 20 ms, on-time 1.5 ms: 50 Hz, 7.5%. A typical hobby-servo pulse: 1.5 / 20 = 0.075.

16 MHz clock, prescaler 1, 20 kHz, 25%: TOP 799, compare 200, 9.64 bits. 16,000,000 / 20,000 = 800 counts, so TOP = 799. 25% of 800 is 200. Each count is 0.125%.

16 MHz clock, prescaler 64, 490 Hz: No exact setting: +0.04%, TOP 509, actual 490.196 Hz. 16,000,000 / (64 × 490) = 510.2 counts, rounded to 510. The frequency is 0.04% high.

1 MHz clock, prescaler 1, 800 kHz: No timer setting: fewer than 2 counts per period. 1,000,000 / 800,000 = 1.25 counts. A PWM period needs at least 2 counts, so use a faster clock or a lower frequency.

References

Microchip: ATmega328P datasheet, fast PWM mode

OpenStax: Period and frequency in oscillations

Wikipedia: Pulse-width modulation, principle