PWM control is a technique that simulates a variable analog voltage by rapidly switching a digital signal on and off and adjusting the ratio of on-time to the total cycle time. In a real circuit, PWM does not actually change the supply voltage; instead, it changes the average power delivered to the load. By snapping a digital pin between 0V and 3.3V (or 5V) thousands of times per second, a microcontroller can dim an LED, slow down a DC motor, or position a servo without wasting energy as heat.

The Core Mechanism: Duty Cycle vs. Frequency

To master Pulse Width Modulation, you must separate its two independent parameters: duty cycle and frequency.

  • Duty Cycle: The percentage of time the signal is HIGH (on) during a single cycle. A 25% duty cycle means the signal is on for a quarter of the time and off for three-quarters.
  • Frequency: How many complete on/off cycles occur in one second, measured in Hertz (Hz). A 1kHz frequency means 1,000 full cycles per second.
The Water Valve Analogy: Imagine a water valve that only has two physical states: fully open or fully closed. To get half the water flow, you do not force the valve halfway open—which creates turbulence and wastes pressure as heat. Instead, you snap it fully open for one second, then fully closed for one second, repeating endlessly. The pipe delivers exactly half the water, but the valve itself never operates in an inefficient, partially-restricted state.

This is exactly how PWM works with electricity. A MOSFET acting as a switch is either fully conducting (minimal resistance, minimal heat) or fully blocking (zero current, zero heat). It never lingers in the linear region where it would act like a resistor and burn up.

Worked Example: Driving a 12V Fan with an ESP32

Let us look at a concrete bench scenario. You want to run a 12V, 0.5A PC cooling fan at roughly 60% speed using an ESP32 DevKit v1 (which outputs 3.3V logic). Because the ESP32 cannot output 12V or handle 0.5A on its GPIO pins, we use an IRLB8721 logic-level N-channel MOSFET.

The Setup and Math

  • Target Speed: 60%
  • Duty Cycle: 60%
  • Frequency: 25,000 Hz (25 kHz) — chosen specifically to be above the upper limit of human hearing to prevent the fan motor coils from emitting an audible high-pitched whine.
  • Average Voltage: The fan 'sees' an average of 7.2V (12V × 0.60).
  • Average Current: Approximately 0.3A (0.5A × 0.60).

The Heat Comparison (PWM vs. Linear)

When the IRLB8721 MOSFET is fully ON, its Rds(on) (drain-to-source resistance) is roughly 0.008 ohms at a 3.3V gate drive. The power dissipated as heat during the ON state is calculated as P = I² × R.

At 0.5A peak current: 0.5² × 0.008 = 0.002 Watts. The MOSFET stays completely cool to the touch.

If you had instead used a linear voltage regulator (like an LM317) to drop 12V down to a steady 7.2V analog DC voltage to achieve the same fan speed, the regulator would have to burn off the difference as heat: (12V - 7.2V) × 0.5A = 2.4 Watts. That linear regulator would require a massive heatsink and still run hot. This thermal efficiency is why PWM control dominates modern power electronics.

Where You Meet PWM Control in Practice

You will encounter PWM in almost every embedded system you build, but the implementation changes based on the load:

  • LED Dimming: Human eyes have persistence of vision. If you PWM an LED at anything above 100Hz, your eye integrates the flashes and perceives a steady, dimmer light. (Typical frequency: 1kHz - 5kHz).
  • DC Motor Speed: Motors have mechanical inertia and electrical inductance. The current smooths out through the motor windings, resulting in proportional torque and speed. (Typical frequency: 10kHz - 25kHz).
  • Servo Positioning: Hobby servos do not use duty cycle to control power; they use the exact width of the HIGH pulse (usually between 1ms and 2ms) to dictate the physical angle of the output shaft. (Strict frequency: 50Hz).
  • Switch-Mode Power Supplies (SMPS): Buck and boost converters use high-frequency PWM (often 100kHz to 2MHz) to charge and discharge inductors, stepping voltages up or down with 90%+ efficiency.
What People Commonly Confuse It With: Beginners often confuse PWM with a true Digital-to-Analog Converter (DAC). A DAC outputs a continuous, steady, variable voltage (e.g., a true 1.65V DC line). PWM outputs a harsh 0V-to-3.3V square wave that only averages out to 1.65V over time. If you measure a 50% PWM signal with a standard multimeter, it will read 1.65V DC. If you hook it up to an oscilloscope, you will see the raw square wave.

Decision Tree: Picking Your PWM Frequency and Driver

Selecting the right frequency and hardware prevents audible noise, flickering, and blown microcontroller pins. Use this decision matrix to lock in your design parameters.

If your load is... Set Frequency to... Use this Microcontroller Peripheral Required Driver Hardware
Standard LED Strip (12V/24V) 1 kHz to 5 kHz ESP32 LEDC / Arduino analogWrite Logic-level MOSFET (IRLB8721)
DC Motor / PC Fan 20 kHz to 25 kHz ESP32 LEDC / MCPWM MOSFET + Flyback Diode (1N5819)
Hobby Servo (SG90 / MG996R) Exactly 50 Hz ESP32 LEDC (Servo library) Direct GPIO (if <20mA) or 5V BEC
High-Power Heater / Peltier 1 Hz to 10 Hz (Slow PWM) Software timer / Ticker Solid State Relay (SSR) or Contactor

For authoritative details on configuring the ESP32's dedicated LED Control (LEDC) peripheral, which handles PWM generation in hardware without burdening the main CPU, refer to the official Espressif LEDC API documentation. For standard Arduino AVR boards, the Arduino analogWrite reference details the fixed 490Hz and 980Hz hardware timers.

Common Hardware Mistakes and How to Avoid Them

Writing the code for PWM is trivial; wiring the hardware is where most makers burn out their boards. Watch out for these specific failure modes:

1. Forgetting the Gate Resistor

A MOSFET's gate acts like a tiny capacitor. When the ESP32 GPIO pin snaps HIGH, it dumps current into this capacitor to charge it. Without a resistor, this inrush current can exceed the ESP32's absolute maximum GPIO rating (typically 40mA, though 20mA is recommended for continuous reliability) and degrade the silicon over time. The Fix: Always place a 100Ω to 330Ω resistor in series between the GPIO pin and the MOSFET gate.

2. Using the Wrong MOSFET for 3.3V Logic

The infamous IRF520 MOSFET module is sold in every beginner kit, but it is not a logic-level MOSFET. It requires 10V on the gate to fully turn on and pass high current. If you drive it with an ESP32's 3.3V pin, it only partially opens, enters its high-resistance linear region, and will melt under heavy loads. The Fix: Always check the datasheet for the Rds(on) specification at Vgs = 2.5V or 4.5V. The IRLB8721 or IRLZ44N are excellent logic-level choices for 3.3V and 5V microcontrollers.

3. Skipping the Flyback Diode on Inductive Loads

Motors, fans, and solenoids are inductors. When the MOSFET snaps OFF, the collapsing magnetic field in the motor generates a massive reverse voltage spike (often hundreds of volts) that will instantly punch through your MOSFET's drain-source junction and fry it. The Fix: Wire a Schottky diode (like a 1N5819) in reverse bias across the motor terminals (cathode to positive, anode to negative) to safely recirculate the flyback current.

4. The Ground Loop Omission

If you are powering the ESP32 from your laptop's USB and powering the 12V fan from a separate bench power supply, the PWM signal will not work. The microcontroller's 3.3V HIGH signal is referenced to its own ground, not the bench supply's ground. The Fix: You must connect the GND pin of the ESP32 to the GND terminal of the external 12V power supply to establish a common reference voltage.

The Default Recommendation: If you are starting a new project and are unsure which parameters to select, default to 5kHz for LEDs and 20kHz for motors. Use an IRLB8721 MOSFET with a 220Ω gate resistor and a 10kΩ pull-down resistor from gate to ground to ensure the load stays off while the microcontroller is booting up. This combination is bulletproof for 95% of hobbyist and prototyping applications up to 15 Amps.