Pulse width modulation (PWM) is a technique that controls the average power delivered to an electrical load by rapidly switching a digital DC signal on and off at a fixed frequency while varying the ratio of on-time to off-time. In a real circuit, PWM changes the effective voltage and power dissipation without wasting energy as heat, fundamentally altering how we drive motors, dim LEDs, and regulate power supplies compared to old-school linear methods.

Whether you are configuring an ESP32 to drive a brushless DC motor or sizing a MOSFET for a 12V LED strip, understanding the mathematical and hardware realities of PWM is what separates a working prototype from a melted breadboard. Below is the exact math, the hardware gotchas, and the data you need to design with it.

The Core Mechanics: Duty Cycle, Frequency, and the RMS Trap

The two defining parameters of any PWM signal are frequency (how many on/off cycles occur per second, measured in Hertz) and duty cycle (the percentage of one cycle that the signal remains HIGH).

When you apply PWM to a load, the load experiences the full supply voltage during the ON state, and zero voltage during the OFF state. Because the switching happens faster than the mechanical or thermal response time of most loads, the load reacts to the average energy delivered. However, calculating power requires a critical distinction between Average Voltage and RMS (Root Mean Square) Voltage.

⚠️ The Beginner's Power Calculation Trap
Beginners often calculate the average voltage ($V_{avg} = V_{peak} \times Duty Cycle$) and then use $P = V_{avg}^2 / R$ to find power. This is mathematically wrong for PWM. Power dissipation in a resistive load depends on the RMS voltage ($V_{rms} = V_{peak} \times \sqrt{Duty Cycle}$). Squaring the average voltage will drastically under-report the actual heat generated in your circuit.

PWM Data Table: 12V System into a 10Ω Resistive Load

Here is exactly what happens to voltage and power at a fixed 1kHz switching frequency across different duty cycles. Notice how power scales linearly with duty cycle, while average voltage does not dictate the true thermal load.

Duty Cycle On-Time (µs) @ 1kHz Average Voltage (V) RMS Voltage (V) True Power (W)
10% 100 µs 1.20 V 3.79 V 1.44 W
25% 250 µs 3.00 V 6.00 V 3.60 W
50% 500 µs 6.00 V 8.48 V 7.20 W
75% 750 µs 9.00 V 10.39 V 10.80 W
100% 1000 µs 12.00 V 12.00 V 14.40 W

Worked Numeric Example: Sizing a Heater Controller

Suppose you are driving a 5Ω resistive heating element from a 24V DC supply using a PWM signal at a 30% duty cycle. You need to know the true power dissipation to size your heat sink.

  • Average Voltage: $24V \times 0.30 = 7.2V$
  • RMS Voltage: $24V \times \sqrt{0.30} = 24V \times 0.5477 = 13.14V$
  • True Power: $V_{rms}^2 / R = (13.14^2) / 5\Omega = 172.6 / 5 = 34.5W$

The Trap in Action: If you had incorrectly used the average voltage (7.2V) to calculate power ($7.2^2 / 5$), you would have calculated 10.3W. You would have undersized your thermal management by a factor of three, likely resulting in a melted enclosure or failed MOSFET.

Where You Meet PWM in Practice (and What It Actually Changes)

PWM is the backbone of modern power electronics because it replaces linear voltage reduction. If you want to dim a 12V, 1A LED strip to 50% brightness using a linear resistor or an LM317 regulator, the component must drop 6V at 1A. That generates 6W of pure waste heat ($P = V_{drop} \times I$). PWM, by contrast, switches a MOSFET fully ON (where its $R_{DS(on)}$ is mere milliohms, generating almost zero heat) and fully OFF (where current is zero, generating zero heat). The efficiency of a well-designed PWM driver routinely exceeds 95%.

Here is where you will encounter it on the bench:

  • DC Motor Speed Control: Motors have high inductance, which naturally smooths the PWM pulses into a continuous current flow. Design rule: Use a frequency above 20kHz (e.g., 21kHz) to push the switching noise above the threshold of human hearing and eliminate audible motor whine.
  • LED Dimming: The human eye integrates light pulses just like it does a motor's inertia. Design rule: Keep frequencies above 1kHz to prevent visible flicker, and above 5kHz if the LED will be recorded by smartphone cameras or machine vision systems, which suffer from rolling-shutter banding at low PWM rates.
  • Switch-Mode Power Supplies (Buck/Boost Converters): PWM dictates the energy transfer to an inductor. The duty cycle directly sets the output voltage ratio in a continuous conduction mode (CCM) buck converter ($V_{out} = V_{in} \times Duty Cycle$).

Hardware Realities: Microcontrollers, MOSFETs, and Flyback Diodes

Generating a PWM signal in code is trivial, but driving a real-world load requires navigating hardware limitations. According to the Espressif ESP-IDF LEDC documentation, modern microcontrollers offer highly configurable PWM peripherals, but you must match the silicon to the load.

Microcontroller PWM Limits

The classic Arduino Uno (ATmega328P) uses hardware timers for its analogWrite() function. By default, most pins output at roughly 490 Hz, while pins 5 and 6 output at 980 Hz. This is fine for LEDs, but 490 Hz will cause severe, annoying whining in DC motors and audible buzzing in ceramic capacitors (due to piezoelectric effects).

The ESP32 solves this via the LED Control (LEDC) peripheral, allowing you to define custom frequencies and resolutions (e.g., 16kHz at 10-bit resolution). When designing for motors, always manually configure the timer frequency rather than relying on microcontroller defaults.

The MOSFET Gate Drive Mistake

Microcontrollers output 5V (Arduino) or 3.3V (ESP32/Raspberry Pi). A very common beginner mistake is using a standard N-channel MOSFET like the IRF520 to switch the load. The IRF520 requires a Gate-to-Source voltage ($V_{GS}$) of 10V to fully turn on and achieve its rated low resistance. At 3.3V, it barely conducts, acting like a massive resistor and overheating instantly.

The Fix: Always select a Logic-Level MOSFET for direct microcontroller driving. Look for part numbers starting with "IRL" (like the IRLZ44N) or check the datasheet to ensure the $R_{DS(on)}$ is fully specified at $V_{GS} = 4.5V$ or $2.5V$.

Inductive Kickback and the Mandatory Flyback Diode

If your PWM load is inductive (a motor, relay, or solenoid), the inductor resists changes in current ($V = L \times di/dt$). When the MOSFET switches OFF, the collapsing magnetic field generates a massive reverse voltage spike that will punch through the MOSFET's drain-source junction and destroy your microcontroller.

You must place a flyback diode in reverse bias across the inductive load. For low-frequency PWM, a standard 1N4007 rectifier diode works. For high-frequency PWM (above 5kHz), the 1N4007's slow reverse recovery time will cause it to overheat; use a Schottky diode like the 1N5819 or a fast-recovery UF4007 instead.

Frequently Asked Questions

Does PWM reduce the current drawn from the power supply?

Yes, it reduces the average current drawn from the supply, but it does not reduce the peak current. During the ON portion of the pulse, the load still draws full current dictated by Ohm's Law ($I = V_{supply} / R$). Your power supply and wiring must be sized to handle the peak current pulses, and you should use bulk decoupling capacitors near the switching MOSFET to supply these rapid current demands without sagging the main voltage rail.

What is the difference between PWM and PFM (Pulse Frequency Modulation)?

In PWM, the frequency is fixed and the pulse width (on-time) changes to regulate power. In PFM, the pulse width is usually fixed (or constant on-time), but the frequency varies based on load demand. PFM is heavily used in modern buck converters (like the TPS62160) at light loads because skipping pulses entirely reduces switching losses, yielding much higher efficiency at low current draws compared to fixed-frequency PWM.

Can I use PWM to control an AC mains load?

No. Standard PWM relies on DC switching. To control AC power (like dimming an incandescent bulb or controlling an AC motor), you must use Phase Angle Control (using TRIACs to chop the AC sine wave at zero-crossings) or Burst Fire Control (switching whole AC cycles on and off). Attempting to apply a DC PWM signal to a bridge rectifier for AC control results in severe harmonic distortion and poor power factor. For AC loads, look into zero-cross solid-state relays (SSRs).

References: For deeper reading on microcontroller timer configurations, refer to the Espressif LEDC API Documentation. For foundational analog circuit theory regarding RMS calculations in switched systems, see the SparkFun PWM Tutorial.