Back to Guides
Makers Guide / MCU Deep Dives

PWM Frequency Depth: Beyond Duty Cycle

PWM Waveform Comparison

Pulse Width Modulation (PWM) is the Swiss Army knife of electronics. Most makers understand the concept of Duty Cycle—the percentage of time a signal is "ON"—and how it regulates power. However, the Frequency of that signal (how many times it pulses per second) is often overlooked.

In this guide, we’ll dive deep into why frequency matters, how it interacts with different types of loads, and when you should consider tweaking it in your projects.

Does Frequency Change Average DC Voltage?

Short Answer: No.

The average DC voltage (V_avg) of a PWM signal is purely a function of the supply voltage and the Duty Cycle. Whether you pulse a 5V signal at 100Hz or 100kHz, if the duty cycle remains at 50%, the average voltage will stay at 2.5V.

V_avg = V_peak × (Time_ON / (Time_ON + Time_OFF))

If the average voltage doesn't change, why do we care about frequency? The answer lies in how the physical load responds to those pulses.

Effects on Different Loads

Loads have different "thermal" or "mechanical" inertia. The choice of frequency determines how well the load filters the pulses into a smooth effect.

Effect of PWM Frequency on LEDs and Motors

1. LEDs and Lighting

LEDs have zero inertia; they turn on and off almost instantly.

  • Low Frequency (< 100Hz): The human eye perceives "flicker." This can cause eye strain or look terrible on camera (strobe effect).
  • High Frequency (> 1kHz): The eye's persistence of vision blends the pulses perfectly. For professional lighting or cameras, frequencies as high as 20kHz+ are often used to avoid interference with shutter speeds.

2. DC Motors

Motors have mechanical and inductive inertia.

  • Low Frequency (50Hz - 500Hz): The motor may vibrate or produce a highly audible "hum" or "whine." You are literally hearing the motor coils vibrate at the PWM frequency.
  • Mid Frequency (1kHz - 15kHz): Effective control, but the audible noise can be annoying in laboratory or home environments.
  • Ultrasonic Frequency (> 20kHz): The switching noise moves above the human hearing range. The motor runs silently, but switching losses in your transistors (MOSFETs) increase.

3. Heaters (Resistive Loads)

Thermal loads have massive inertia. Since temperature changes over seconds, not milliseconds, a very low frequency (even 1Hz) is perfectly fine for a heater. In fact, high frequencies for heaters are avoided to reduce electromagnetic interference (EMI).

When Should You Tweak the Frequency?

You should adjust your PWM frequency when you encounter one of three problems:

Problem
Solution
Audible "Whining" noise from a motor or coil.
Increase frequency to >20kHz (Ultrasonic).
LED flicker on phone cameras or video.
Increase frequency to >5kHz.
MOSFET/Transistor getting too hot at high power.
DECREASE frequency to reduce switching losses.
Radio interference or sensitive sensor errors.
Change frequency or add a low-pass filter.

How to Control Frequency on Microcontrollers

Arduino (AVR)

On standard Arduinos, analogWrite() uses a fixed frequency (approx 490Hz or 980Hz). To change it, you must manipulate the hardware Timers and Prescalers directly.

// Example: Setting Timer 1 for 20kHz PWM on Pin 9
void setup() {
  pinMode(9, OUTPUT);
  TCCR1A = _BV(COM1A1) | _BV(WGM11);
  TCCR1B = _BV(WGM13) | _BV(CS10); // No prescaling
  ICR1 = 400; // This value determines frequency (16MHz / (2 * 20kHz) = 400)
}

void setPWM(int duty) {
  OCR1A = duty; // Set duty cycle (0 to 400)
}

ESP32

The ESP32 has a dedicated LEDC peripheral that makes frequency control incredibly easy. You can set exactly the frequency and resolution you want.

#include "driver/ledc.h"

void setup() {
  // Use a 20kHz frequency with 10-bit resolution
  ledcSetup(0, 20000, 10); 
  ledcAttachPin(18, 0);
}

void loop() {
  ledcWrite(0, 512); // 50% duty cycle (2^10 / 2)
}

Summary

Changing PWM frequency doesn't change the laws of physics regarding power (Average DC), but it fundamentally changes the quality of the output. For silent motors and flicker-free LEDs, push your frequency higher. If your power components are overheating, consider dropping it back down. Understanding this balance is what separates a hobbyist from a pro engineer.

🚀 Key Takeaway

Frequencies above 20,000 Hz are invisible to the ear but "expensive" for transistors due to heat. Frequencies below 100 Hz are "cheap" for hardware but "loud" to the eyes and ears.