A PWM controller is a microcontroller hardware peripheral or dedicated integrated circuit that generates a digital square wave with a precisely adjustable duty cycle to simulate analog voltage levels or control power delivery. In a real circuit, it changes the average power delivered to a load by rapidly switching the full supply voltage on and off, which eliminates the massive heat losses you would otherwise get from linear voltage dropping (like using a power resistor or linear regulator). Makers frequently confuse a true hardware PWM controller with software "bit-banging" (toggling a GPIO pin manually in a loop), or they mix up PWM frequency (how fast the wave switches) with duty cycle (the percentage of time the signal stays HIGH).
How a Hardware PWM Controller Actually Works
Inside a microcontroller like the ESP32 or ATmega328P, a hardware PWM controller relies on a timer/counter peripheral tied directly to the system clock. The clock ticks up a hardware register. When the register hits a predefined "compare" value, the physical GPIO pin is forced LOW. When it hits the "period" (or overflow) value, the register resets to zero and the pin goes HIGH. Because this happens entirely in silicon, the main CPU is free to run WiFi stacks, read sensors, or execute complex logic without interrupting the waveform.
According to the Espressif LEDC API documentation, the ESP32 utilizes a dedicated LED Control (LEDC) peripheral specifically designed to offload this timing from the main dual-core processors, ensuring zero jitter even under heavy RTOS task loads.
Worked Example: Sizing PWM for a 12V Brushless Fan
Let’s look at a concrete bench scenario. You want to control a standard 12V, 0.5A PC cooling fan using an ESP32-WROOM-32 (which outputs 3.3V logic) and an IRLZ44N logic-level MOSFET.
- Frequency Selection: PC fans typically require a 20kHz to 25kHz PWM signal. If you drop below 20kHz, the internal motor coils will vibrate at an audible frequency, creating an annoying high-pitched whine. We will select 25,000 Hz.
- Resolution Selection: The ESP32 LEDC peripheral allows custom bit resolutions. Let’s use an 8-bit resolution, giving us 256 discrete steps (0 to 255).
- Target Speed: We want the fan running at roughly 40% speed.
The Math:
40% of our 8-bit maximum (255) is 102.
The average voltage the fan "sees" is 12V × 0.40 = 4.8V.
Because the MOSFET switches fully ON (12V) and fully OFF (0V) rather than acting as a variable resistor, the MOSFET dissipates almost zero heat, and the fan's internal commutation circuitry handles the 4.8V average smoothly.
Where You Meet PWM Controllers in Practice
You will encounter hardware PWM controllers across almost every domain of embedded electronics:
- Motor Speed Control: Driving DC brushed motors via H-bridges (like the L298N or DRV8833), or sending throttle signals to Brushless DC (BLDC) Electronic Speed Controllers (ESCs).
- LED Dimming: Switching high-current buck converters that feature a dedicated "PWM DIM" pin, or directly driving MOSFETs for high-power LED strips. This maintains the LED's color temperature, unlike analog voltage reduction.
- Servo Positioning: Standard RC servos expect a very specific 50Hz PWM signal where the absolute width of the HIGH pulse (usually 1ms to 2ms) dictates the shaft angle.
- Digital-to-Analog Conversion (DAC): By passing a high-frequency PWM signal through a simple RC low-pass filter (a resistor and a capacitor), you can smooth the square wave into a true analog DC voltage, effectively building a makeshift DAC.
Decision Tree: Which PWM Implementation Should You Use?
Not all PWM is created equal. Use this decision matrix to select the right architecture for your project.
| If your project requires... | Then choose this implementation... | Concrete Part / API Pick |
|---|---|---|
| High frequency (>10kHz) AND your MCU is running heavy tasks (WiFi, RTOS, interrupts) | Native Hardware Peripheral | ESP32 ledcSetup() or Arduino Timer1 |
| Many channels (8 to 16+) of 50Hz servos, and your MCU lacks enough hardware timers | External I2C PWM IC | Adafruit PCA9685 16-Channel breakout |
| High-voltage / High-current AC or DC switching with built-in protection and feedback | Dedicated External PWM Controller IC | Texas Instruments TL494 or UC3842 |
| Very low frequency (<5Hz) indicator blinking with zero concern for timing jitter | Software Bit-Banging | Standard digitalWrite() with delay() |
Common PWM Configuration Mistakes on the Bench
When a PWM circuit fails or behaves erratically, it is rarely a flaw in the theory. It is usually one of these three bench-level mistakes:
- Using the Wrong MOSFET for 3.3V Logic: A classic mistake is pairing a 3.3V ESP32 with an IRF520 MOSFET. The IRF520 has a gate threshold voltage (Vgs) of around 4.0V. The 3.3V PWM signal will barely crack the gate open, causing the MOSFET to operate in its linear (high-resistance) region, overheat, and potentially fail. Always use a logic-level MOSFET like the IRLZ44N or IRLB8721, which fully saturate at 3.3V.
- Audible Whine in Inductive Loads: If your solenoid, relay, or fan is screaming, your frequency is too low. The human ear typically hears up to 20kHz. Push your hardware PWM frequency to at least 22kHz–25kHz to push the switching noise into the ultrasonic range.
- Software Jitter from Bit-Banging: If you try to generate PWM using
delayMicroseconds()in your main loop, any other code execution (like reading an I2C sensor or printing to the Serial monitor) will pause the loop, stretching your LOW or HIGH times and destroying your duty cycle. This causes motors to stutter and LEDs to flicker.
Frequently Asked Questions
Can PWM damage a DC motor?
No, PWM itself will not damage a standard brushed DC motor. However, if the frequency is set too low (e.g., 50Hz), the motor will rapidly accelerate and decelerate, causing mechanical vibration, excess heat in the windings, and audible cogging. Stick to >1kHz for brushed motors.
What is the difference between PWM and PFM?
PWM (Pulse Width Modulation) keeps the frequency constant and changes the width of the pulse to regulate power. PFM (Pulse Frequency Modulation) keeps the pulse width constant but changes the frequency. PFM is often used in ultra-low-power DC-DC buck converters to maintain high efficiency at very light loads, whereas PWM is preferred for steady, predictable motor and LED control.
Do I need a flyback diode for PWM?
Yes, if you are switching an inductive load (like a relay coil, solenoid, or brushed motor) with a MOSFET. When the PWM signal goes LOW and the MOSFET cuts power, the collapsing magnetic field in the inductor generates a massive reverse voltage spike that will instantly punch through and destroy your MOSFET. A standard 1N4007 or Schottky diode wired in reverse parallel across the load clamps this spike safely. For more on protecting driver circuits, refer to the Arduino Analog Output documentation regarding inductive kickback.






