What is Pulse-width modulation (PWM)?
A method that adjusts average voltage or power by varying the high time of an output while keeping the period constant. Used to dim LEDs and control motor speed.
Field: Embedded Systems & Firmware
Read the beginner explainer: What Is a Microcontroller? Tiny Computers in Everyday Things
Japanese name: PWM(パルス幅変調)
Duty cycle and average value
Pulse-width modulation (PWM) keeps the switching period T fixed and varies the high time t_on. The duty cyclesets the average output, which is D times the pulse amplitude: a 5 V signal at 40% duty averages 2 V. The information is carried entirely in the timing, so the output stage only ever sits at two levels. A load that responds slowly compared with the period, such as a motor, a heater, the human eye looking at an LED, or an RC or LC filter, sees mainly this average value.
How a microcontroller timer generates PWM
A hardware timer counts clock pulses from zero up to a period value and then restarts. The output is set at the start of each period and cleared when the counter matches a compare register. With a period of 1000 counts and a compare value of 250, the output is high 25% of the time. Resolution is set by the number of counts per period, so for a fixed timer clock, higher PWM frequency means coarser duty steps. Once configured, the timer runs without CPU involvement. Analog PWM circuits do the same job by comparing a control voltage with a triangle or sawtooth wave.
Why switching is efficient
The switching transistor is either fully on, with little voltage across it, or fully off, with almost no current, so it dissipates far less power than a transistor held at an intermediate voltage in a linear circuit. Losses come mainly from on-resistance and the brief transitions between states. The frequency is chosen for the load: high enough that LED flicker is not visible and motor current is smoothed by winding inductance, and often above the audible range to avoid motor whine, but not so high that switching losses dominate.
PWM as a DAC and in power electronics
Passing PWM through a low-pass filter gives a crude digital-to-analog converter without dedicated DAC hardware; setting the filter cutoff well below the PWM frequency reduces ripple, at the cost of slower response. The same principle runs switch-mode power supplies, where an ideal buck converter outputs D times its input voltage, and inverters, which switch power devices at several kHz to tens of kHz while varying pulse widths to synthesize AC. A three-phase motor inverter uses six switches, two per leg.
Try it: questions about Pulse-width modulation (PWM)
- AThe duty cycle of the pulses (the fraction of each period that is on)
- BThe color of the pulses
- CThe memory capacity of the microcontroller
Show answer and explanation
Answer: A. The duty cycle of the pulses (the fraction of each period that is on)
Averaging a PWM signal with a low-pass filter gives an output of roughly high-level voltage × duty cycle when the low level is 0 V—about 2 V for 40% of 5 V. Resolution is set by the timer's count range, and placing the PWM frequency well above the filter cutoff reduces ripple. Since it produces analog voltages without a dedicated DAC, it is used for LED dimming, motor speed control and simple audio output, but response speed and accuracy trade off against ripple.
- AWhile the output is saturated, increase the proportional gain to get out of saturation sooner
- BIf the integral term is calculated in floating point no overflow occurs, so no special measure is needed
- CWhile the output is saturated, stop integrating if the error would deepen the saturation, or feed the difference from the limited value back into the integrator, to hold down the integral
Show answer and explanation
Answer: C. While the output is saturated, stop integrating if the error would deepen the saturation, or feed the difference from the limited value back into the integrator, to hold down the integral
The first method is conditional integration (clamping): while the output is saturated and the error would deepen the saturation, the integral is not updated. The second is back-calculation: the difference between the value calculated before limiting and the value actually output is multiplied by a coefficient and fed back into the integrator to pull the integral back. In the velocity form of PID control, which calculates the change in the manipulated variable, simply limiting the output makes windup unlikely. When calculating in integer fixed point, overflow of the integral variable itself must also be guarded against, for example with saturating arithmetic.
FAQ
How do you calculate PWM duty cycle?
Divide the high time by the period: duty cycle = t_on / T. For example, 250 counts high out of a 1000-count period is 25%, and the average output is 25% of the pulse amplitude.
Does PWM change the frequency or the voltage?
Neither, in its basic form: frequency and pulse amplitude stay constant and only the pulse width changes. The average voltage or power delivered to the load changes because the fraction of on time changes.
Why is PWM more efficient than a linear regulator for driving a motor?
A linear regulator drops the excess voltage across a transistor that carries full current, wasting it as heat. With PWM the transistor is either fully on or fully off, so its voltage-current product, and thus its loss, stays small.
Books on Embedded Systems & Firmware Ad
Learn hands-on: parts and kits
Building real circuits is a good way to check what you learned in the questions.
As an Amazon Associate I earn from qualifying purchases.
Comparisons that include this term
This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.
Back to the glossary