Skip to content
MCUs

PWM Duty Cycle Calculator

Calculate PWM frequency, period, duty cycle percentage, and on/off times. Works for Arduino analogWrite (8-bit), ESP32 LEDC (up to 16-bit), and servo pulse width calculations.

PWM Frequency & Duty Cycle

PWM (Pulse Width Modulation) rapidly switches an output on and off. The ratio of on-time to total period is the duty cycle. Higher frequency reduces flicker (LEDs) or ripple (power supplies). Higher duty cycle means brighter LED or higher motor speed. Calculate frequencies, periods, and PWM register values for Arduino, ESP32, and STM32.

OnOffOne Period (T = 1/f)

Duty Cycle = (On-time / Period) × 100%

LED: 1 kHz–10 kHz · Motor: 5 kHz–20 kHz

0% = always off · 50% = half power · 100% = always on

Arduino: 8-bit · ESP32: up to 16-bit

Period

1000.00 μs

On-time
500.00 μs
Off-time
500.00 μs
PWM value
128 / 255

PWM basics

Pulse-width modulation switches a pin fully on and off very quickly. The duty cycle is the fraction of each period spent on:

  • Duty cycle = Ton ÷ T × 100 %
  • Frequency f = 1 ÷ T
  • Average voltage = Vhigh × duty cycle

At 25 % duty a 5 V pin averages 1.25 V. LEDs look dimmer and motors run slower, because they respond to the average.

Arduino analogWrite

analogWrite(pin, value) takes 0–255, so duty = value ÷ 255. On an Uno, pins 5 and 6 run at about 980 Hz and pins 3, 9, 10 and 11 at about 490 Hz. analogWrite(9, 64) gives 25 % duty.

ESP32 LEDC: resolution vs frequency

The ESP32 generates PWM from an 80 MHz clock, so frequency and resolution trade off: max frequency ≈ 80 MHz ÷ 2^bits.

ResolutionDuty stepsMaximum frequency
8-bit256312 kHz
10-bit102478 kHz
12-bit409619.5 kHz
13-bit81929.8 kHz
16-bit65 5361.2 kHz

Choosing a frequency

LoadSuitable frequencyWhy
LED dimming500 Hz–5 kHzAbove ~200 Hz no visible flicker; higher avoids camera banding
DC motor (driver like L298N, TB6612)1–20 kHzAbove ~16 kHz the whine becomes inaudible
Hobby servo50 Hz, 1–2 ms pulseServos read pulse width, not duty
Fan (4-pin PC fan)25 kHzIntel fan specification
PWM to analog via RC filterAs high as resolution allowsSmaller ripple for the same filter

analogWrite values for common duty cycles

Duty cycleArduino analogWrite (8-bit)ESP32 LEDC 10-bitAverage of 5 VAverage of 3.3 V
10 %261020.5 V0.33 V
25 %642561.25 V0.83 V
50 %1285122.5 V1.65 V
75 %1917673.75 V2.48 V
100 %25510235 V3.3 V

Brightness isn't linear

Our eyes respond roughly logarithmically, so an LED at 50 % duty looks much brighter than "half". For smooth fades, map the control value through a gamma curve: duty = 255 × (level/255)^2.2. A 10 % visual brightness needs only about 1 % duty.

Driving real loads

A GPIO pin can only drive about 20 mA. To PWM a motor, fan or LED strip, switch it with a logic-level MOSFET (IRLZ44N, AO3400) or a motor driver (TB6612FNG, L298N, DRV8833), and add a flyback diode across DC motors. Above roughly 20 kHz most MOSFET gate drivers on breakout boards start to run warm, so 1–20 kHz is the usual range for motors.

Formula, inputs and worked examples

Formula and method

Duty cycle (%) = Ton / Tperiod × 100; frequency = 1/Tperiod; Toff = Tperiod − Ton.

Worked examples

Dim an LED to 30 % on Arduino

analogWrite(9, 77) gives 77 ÷ 255 = 30 % duty at about 490 Hz: 0.61 ms on and 1.43 ms off per 2.04 ms period.

Quiet motor on ESP32

20 kHz with 10-bit resolution is within the 78 kHz limit and gives 1024 speed steps without audible whine.

Servo pulse

A 1.5 ms pulse every 20 ms (50 Hz) is a 7.5 % duty cycle and centres a standard servo.

Inputs

  • PWM frequency — Unit: Hz
  • Period or ON time — Unit: s
  • Duty cycle — Unit: percent

What the result means

  • Duty cycle — Unit: percent
  • Period and ON/OFF time — Unit: s

Practical tips and common mistakes

  • Use a logic-level MOSFET or driver for anything above ~20 mA; never drive motors directly from a pin.
  • Add a flyback diode across motor terminals when switching with a MOSFET.
  • On ESP32, lower the resolution if a high frequency is rejected.

Assumptions and limitations

  • PWM duty cycle controls average power only under suitable load and switching assumptions; it is not always equivalent to a lower DC voltage.
  • Timer resolution and frequency are hardware-dependent; servo pulses have different timing requirements from LED dimming.

Frequently asked questions

How do I calculate PWM duty cycle?

Divide the on-time by the period and multiply by 100. A pulse that is high for 2 ms in a 10 ms period has a 20 % duty cycle.

What PWM frequency does Arduino analogWrite use?

About 490 Hz on most pins and about 980 Hz on pins 5 and 6 of the Uno and Nano. The frequency can be changed by reconfiguring the timer prescalers.

What is the average voltage of a PWM signal?

The high voltage times the duty cycle. A 3.3 V ESP32 pin at 60 % duty averages 1.98 V, which you can measure with a multimeter or after an RC low-pass filter.

Why does my motor whine with PWM?

The PWM frequency is in the audible range (below ~16 kHz). Raise it to 18–25 kHz if your motor driver supports it.

Learn more

More MCUs