PWM controls simulate variable analog voltage by rapidly switching a digital signal on and off, where the ratio of on-time to total cycle time (duty cycle) dictates the average power delivered to a load. In a real circuit, this changes how much energy reaches your component without wasting the excess as heat, which is exactly what happens if you use a linear resistor or voltage divider to drop voltage. Beginners commonly confuse PWM with true analog Digital-to-Analog Converter (DAC) output; while a DAC outputs a steady continuous voltage, PWM outputs a harsh square wave that relies on the load's physical inertia (like a motor's momentum or a human eye's persistence of vision) to smooth out the pulses.
The Math Behind the Magic: Duty Cycle and Frequency
To use PWM effectively, you must separate the two dials you are turning: duty cycle and frequency. Duty cycle is the percentage of time the signal is HIGH (usually 3.3V or 5V from your microcontroller) during a single period. Frequency is how many of those periods occur per second, measured in Hertz (Hz).
Let's look at a concrete bench scenario. You are driving a 12V, 0.5A (6W) DC cooling fan using an ESP32 and an IRLZ44N logic-level MOSFET. You set your PWM frequency to 25,000 Hz (25 kHz) and your duty cycle to 50%.
- The Signal: The microcontroller outputs 3.3V for 20 microseconds, then 0V for 20 microseconds, repeating 25,000 times a second.
- The Average Voltage: The fan's mechanical inertia averages these pulses. 12V × 0.50 = 6V average.
- The Power: Assuming a simplified resistive equivalent for the winding at that operating point, the average power delivered is roughly 3W.
The MOSFET dissipates almost zero heat because it is either fully on (low Rds_on of ~0.022 ohms) or fully off (zero current). A linear regulator dropping 12V to 6V at 0.5A would burn off the other 3W as heat, requiring a massive heatsink. This efficiency is why PWM controls dominate power electronics.
Where You Meet PWM Controls in Practice
You will encounter PWM across almost every embedded subsystem, but the required parameters change drastically depending on the physical load.
LED Dimming
Human eyes integrate light pulses above roughly 100Hz. However, if you use a 100Hz PWM signal to dim an LED and then record it with a smartphone camera, you will see severe rolling shutter flicker. For LED lighting, standard practice is 1 kHz to 5 kHz. This is fast enough to beat camera sensors but slow enough that standard MOSFETs switch cleanly without excessive gate-charge losses.
DC Motor Speed Control
Electric motors suffer from magnetostriction—the physical vibration of the laminations in the magnetic field. If you drive a motor with a 1 kHz PWM signal, it will emit a loud, irritating 1 kHz whine. To achieve silent operation, you must push the PWM frequency above the upper limit of human hearing, typically 20 kHz to 25 kHz.
Servo Positioning
Standard hobby servos (like the SG90 or MG996R) do not use duty cycle as a percentage of power; they use absolute pulse width to dictate shaft angle. The standard requires a strict 50 Hz frequency (a 20ms period). A 1ms HIGH pulse moves the servo to 0 degrees, 1.5ms to 90 degrees, and 2ms to 180 degrees.
Heating Elements (PID Control)
When controlling a 3D printer hotend or a reflow oven, the thermal mass is enormous. Switching a mechanical relay or Solid State Relay (SSR) at 20 kHz will destroy the switchgear instantly. Here, PWM controls are implemented in 'slow PWM' or time-proportioning mode, where the period is measured in seconds (e.g., 0.1 Hz to 1 Hz).
Frequency Tuning and Inductive Kickback
Choosing the wrong frequency is the most common cause of failed embedded prototypes. While high frequencies solve audible noise, they introduce switching losses and electromagnetic interference (EMI). Every time a MOSFET transitions from off to on, it passes through a linear region where voltage and current overlap, generating heat. At 50 kHz, these switching losses can overheat a poorly driven gate.
When your PWM signal switches OFF an inductive load (like a motor, relay, or solenoid), the collapsing magnetic field induces a massive reverse voltage spike. This spike will instantly punch through your MOSFET's drain-source junction or fry your microcontroller pin. You must always place a flyback diode (like a 1N4007 for slow switching or a 1N5819 Schottky for high-frequency PWM) in reverse parallel across the load to clamp this spike.
Furthermore, if you attempt to use PWM controls for audio output, you cannot wire a speaker directly to the pin. The speaker will reproduce the harsh square wave and its high-frequency harmonics. You must pass the PWM signal through an RC low-pass filter (e.g., a 1kΩ resistor and a 100nF capacitor) to smooth the square wave into an analog sine wave before it hits your amplifier.
Hardware Decision Tree: Native Pins vs. External ICs
Not all microcontroller pins are created equal. The Arduino Uno R3 (ATmega328P) uses analogWrite(), which is locked to 8-bit resolution (0-255 steps) and fixed frequencies of 490 Hz or 980 Hz. The ESP32 LEDC peripheral, however, offers up to 20-bit resolution and highly configurable frequencies.
Use this decision matrix to select your PWM hardware architecture:
| Application Scenario | Recommended Hardware | Why This Wins |
|---|---|---|
| Dimming 2-4 standard LEDs | ESP32 Native LEDC Pins | Up to 16 channels, 20-bit resolution allows imperceptible fading steps at low brightness. |
| Driving 8+ Hobby Servos | PCA9685 I2C Breakout | Offloads timing from the MCU. Hardware I2C buffer prevents servo jitter during heavy CPU tasks. |
| Bi-directional 12V DC Motor (up to 3.6A) | TI DRV8871 Driver | Integrates H-bridge, charge pump, and overcurrent protection. Native MCU pins cannot source this current. |
| High-Power AC Heating Element | Zero-Cross SSR + Slow PWM | Switches AC only at the zero-voltage crossing, eliminating massive EMI spikes and inrush currents. |
Frequently Asked Questions About PWM Implementation
Why does my LED flicker at low brightness on an Arduino Uno?
The Arduino Uno's 8-bit resolution only gives you 256 steps. At a 1% duty cycle, you are jumping from completely off to a visible pulse. The human eye is highly sensitive to low-light changes. To fix this, either switch to an ESP32 for higher bit-depth resolution, or implement software gamma correction in your code to map the linear PWM steps to a logarithmic brightness curve.
Can I parallel two microcontroller PWM pins to get more current?
No. Even if the pins are configured identically, nanosecond differences in switching times will cause one pin to source current into the other during the transition edges, leading to localized overheating and silicon failure. Always use a single pin to drive the gate of a logic-level MOSFET (like the IRLZ44N or IRLB8721), which can easily handle 30A+.
What happens if my PWM frequency is too high for my MOSFET?
The MOSFET gate acts like a small capacitor. If your microcontroller pin cannot source enough current to charge that capacitor quickly, the MOSFET spends too much time in the linear (partially on) region. This generates massive heat and will melt your component. If you need to switch at 100 kHz+, use a dedicated gate driver IC (like the TC4420) between your microcontroller and the MOSFET gate.
Final Verdict: Which PWM Setup Should You Build?
If you are driving low-current LEDs or small indicators, use your microcontroller's native pins. If you are controlling more than four servos, buy a PCA9685 I2C breakout board to eliminate CPU jitter. If you are driving a DC motor that draws more than 500mA, use a dedicated motor driver like the TI DRV8871.
However, for the default recommendation: for 90% of general maker projects in 2026, the ESP32-WROOM-32 native LEDC peripheral is the undisputed baseline. It offers enough resolution for smooth LED fading, enough frequency headroom for silent motor control, and requires no external I2C expanders for standard channel counts. Start your design there, and only add external ICs when you hit physical current limits or run out of pins.






