Chapters

9 PWM Control: Frequency and Driver Decisions

actuators
pwm
control

9.1 Start With the Decision

A PWM rate that is quiet for one motor may waste power in another driver. Frequency, decay mode, and switching loss must be checked together.

9.2 Route Overview

This is part 2 of 2. Review PWM Control: Signals and Duty-Cycle Behaviour for the preceding evidence.

9.3 Learning Objectives

  • Choose PWM frequency from load and driver constraints.
  • Assess ripple, decay mode, and switching evidence.

9.4 Chapter Roadmap

  • Choosing PWM Frequency: Decision Factors
  • Checkpoint: Frequency Choice
  • Try It: Actuator Control Challenge
  • Quick Reference: PWM Formulas
  • For Kids: Meet the Actuator Crew!
  • Connection: PWM Control meets PID Feedback Loops
  • Deep Dive: Duty Cycle, Ripple Current, and Decay Mode
  • Checkpoint: Motor Driver Evidence
  • Summary
  • Key Takeaway
  • Knowledge Check
  • Knowledge Check - Interactive Quizzes
  • Quiz: PWM Control
  • Concept Relationships
  • See Also
  • Common Pitfalls
  • 1. Using analogWrite() on ESP32 Without LEDC Configuration
  • 2. Wrong PWM Frequency for Servo Control
  • 3. Keep Motor PWM Above 20 kHz
  • 4. PWM Voltage vs Driver Inputs
  • What’s Next
  • Label the Diagram
  • Code Challenge

9.5 Choosing PWM Frequency: Decision Factors

  1. Motor Max compares a lamp, fan, and heater responding at different switching paces.

    Start with how the load moves, sounds, or shines.

  2. Max measures smooth output while watching the driver temperature rise.

    Check smooth control and heat at that pace.

  3. Max keeps one load-specific setting after its response and heat checks pass.

    Keep the pace only if the real load passes.

CP-0043 decision strip: Choose frequency by following the load's physical response and switching device together: first avoid visible or audible modulation, then confirm current ripple and control range, and finally check driver heating.

Choose frequency by following the load’s physical response and switching device together: first avoid visible or audible modulation, then confirm current ripple and control range, and finally check driver heating. The table turns those checks into actuator-specific starting ranges rather than universal limits.

The PWM frequency is not arbitrary — it must match the actuator’s physical characteristics and the application’s requirements. Choosing wrong creates audible noise, poor control, or wasted energy.

ActuatorRecommended FrequencyWhy This RangeWhat Goes Wrong Outside It
LED200-1000 HzHuman eye flicker fusion threshold is ~70 Hz; 200+ Hz eliminates all visible flicker. Higher than 1 kHz wastes switching energy with no visual benefit.Below 70 Hz: visible flicker causes eye strain. Above 10 kHz: increased driver switching losses.
DC Motor5-25 kHzMotor inductance smooths current at these frequencies. Below 5 kHz the motor coils vibrate audibly. Above 25 kHz switching losses dominate.Below 1 kHz: loud audible whine. Above 50 kHz: MOSFET driver overheats from switching losses.
ServoExactly 50 HzServo decoder IC expects 20 ms period. Position is encoded as pulse width (1-2 ms) within this period.Any other frequency: servo misinterprets position or jitters. Most servos tolerate 40-60 Hz but 50 Hz is the standard.
Heating element0.5-2 HzThermal mass smooths temperature. Fast switching is unnecessary because the element’s temperature changes over seconds, not milliseconds.Above 10 Hz: relay contacts wear prematurely (if relay-controlled). No benefit — heaters cannot respond faster than their thermal time constant.
Solenoid valve100-500 HzMust be fast enough that the valve plunger does not physically open/close each cycle (it “floats” at average position).Below 50 Hz: valve chatters (audible clicking, mechanical wear). Above 1 kHz: increased eddy current losses in the solenoid core.

Motor MaxCheckpoint: Frequency Choice

You now know:

  • The greenhouse fan table uses 30%, 60%, 85%, and 100% duty points on a 12V, 0.5A fan.
  • The daily energy estimate is 56.4 Wh/day instead of 144 Wh/day, a 61% saving before the driver-loss caveat.
  • Frequency is chosen from symptoms: below 70 Hz can flicker an LED, below 1 kHz can whine a motor, and above 50 kHz can overheat the driver.

After this point, use the game and quick reference to practice selection. The deep dive then explains what the motor current and power stage are doing underneath the simple duty-cycle command.

9.6 Try It: Actuator Control Challenge

9.7 Quick Reference: PWM Formulas

Duty Cycle (%) = (ON time / Period) x 100
Duty Cycle (%) = (PWM value / Max value) x 100

Examples (8-bit PWM, 0-255):
- PWM = 0   = 0% duty   = 0V average (OFF)
- PWM = 64  = 25% duty  = 1.25V @ 5V supply
- PWM = 128 = 50% duty  = 2.5V @ 5V supply
- PWM = 191 = 75% duty  = 3.75V @ 5V supply
- PWM = 255 = 100% duty = 5V average (FULL ON)

Average Voltage = Supply Voltage x (PWM Value / Max Value)

“I have a superpower,” announced the microcontroller. “I can make things go fast or slow, bright or dim, even though I can only say ON or OFF!”

“That doesn’t make sense,” said Temperature Terry. “How can you make something half-bright if you can only turn it fully on or fully off?”

Max grinned. “Watch this!” He started flicking the LED on and off — really, really fast. “Lila, how do you feel?”

“I feel… kind of medium bright!” Lila said, glowing at about half her usual brightness. “But I can tell you’re flickering me!”

“That’s because I’m switching you on and off 5,000 times per second,” Max explained. “Your eyes and most actuators can’t see the individual flickers — they just see the average. If I keep you ON for 25% of the time, you glow at quarter brightness. 75% of the time? Three-quarter brightness!”

“So it’s like a really fast strobe light that tricks everyone into seeing smooth dimming?” asked the battery.

“Exactly! It’s called PWM — Pulse Width Modulation. And the best part is, I only use as much of Bella’s energy as needed. At 50% duty cycle, we use about half the power!”

“I like that,” said Bella, smiling. “More power saved means I last longer!”

Connection: PWM Control meets PID Feedback Loops

PWM sets the output to an actuator, but how do you decide what duty cycle to use? That is where PID (Proportional-Integral-Derivative) controllers come in. A PID controller continuously reads a sensor (e.g., temperature), compares it to the setpoint, and adjusts the PWM duty cycle to minimize the error. For example, a smart thermostat reads the room temperature, calculates the PID output, and adjusts the heater’s PWM duty cycle accordingly. PWM without feedback is open-loop control; PWM with PID is closed-loop control. See DC Motor Control for PID implementation examples with motors.

9.8 Deep Dive: Duty Cycle, Ripple Current, and Decay Mode

PWM is not a middle voltage. The output still slams between 0 V and the full supply; the actuator responds to the time average because it cannot react to every fast pulse. Duty cycle sets that average command, frequency decides whether the pulses are smooth enough, and resolution decides how many distinct command levels fit between off and full on.

Inspect Figure 9.1 before doing the arithmetic so pulse amplitude, period, high-time, and average response remain separate quantities. The switch still reaches the full supply; duty cycle changes how long that state lasts in each period.

At fixed 12 V and 20 kHz, 25%, 50% and 75% PWM duty cycles give ideal averages of 3, 6 and 9 V. More ON-time raises the average, not the pulse voltage or a steady intermediate supply.
Figure 9.1: PWM changes average actuator output by changing the high-time inside each period; with a 12 V supply, 25%, 50%, and 75% duty cycles ideally average to 3 V, 6 V, and 9 V.

Read Figure 9.1 from 25% through 50% to 75% duty. Pulse height remains 12 V while high-time and the marked ideal average rise; the actuator’s inductance and inertia smooth the physical response rather than turning the switch output into a literal intermediate supply voltage.

Take a 12 V ventilation fan controlled by an ESP32 through a MOSFET. At 20 kHz, one PWM period is 1 / 20000 = 50 microseconds. A 35% command keeps the switch on for 0.35 x 50 = 17.5 microseconds and off for 32.5 microseconds. The ideal average motor terminal voltage is 12 x 0.35 = 4.2 V. On an 8-bit PWM channel the requested register value is round(0.35 x 255) = 89; on a 10-bit channel it is round(0.35 x 1023) = 358. The motor still receives 12 V pulses, not a smooth 4.2 V source, but its winding inductance and rotor inertia make the shaft respond mostly to average torque.

The same arithmetic explains why PWM is efficient. If a small fan draws 0.5 A at full speed, full electrical input is about 12 V x 0.5 A = 6 W. A rough 35% operating point averages 0.35 x 0.5 = 0.175 A and 12 x 0.175 = 2.1 W, before driver losses and non-linear motor behavior. A linear regulator or series resistor would burn the unused energy as heat, while a MOSFET PWM driver spends most of its time either fully on with low resistance or fully off with almost no current.

ParameterWhat it controlsGetting it wrong
Duty cycleAverage level: brightness, speed, torqueChanges the command, not the switching rate
FrequencyHow fast the output switchesToo low causes LED flicker or motor whine; too high raises switching losses
ResolutionNumber of duty-cycle codesToo coarse makes fades and low-speed control step visibly

Pick PWM settings from the actuator backward. An LED dimmer should avoid visible flicker, so 500 Hz to a few kHz is usually fine. A brushed DC motor in a quiet room is often set near 20 kHz so the switching tone is above normal hearing. A hobby servo is different: its electronics expect a control pulse inside a 20 ms frame, so the frame frequency stays near 50 Hz and the useful command is the pulse width, commonly about 1.0 ms to 2.0 ms. At 50 Hz, a 1.5 ms center pulse is only 1.5 / 20 = 7.5% duty, but changing that duty changes the decoded angle rather than average motor voltage directly.

Resolution is the practical limit on how fine the command can be. With 8-bit PWM there are 256 codes, so one step is 1 / 255 = 0.392%. On a 12 V motor that is about 12 x 0.00392 = 47 mV of ideal average-voltage change per step. With 12-bit PWM there are 4096 codes, so one step is 1 / 4095 = 0.0244%, or about 2.9 mV on the same supply. Higher resolution helps slow LED fades and low-speed motor tuning, but timer hardware usually trades frequency against resolution.

On a motor, the off-time is not empty. The winding is an inductor, and an inductor resists sudden current changes. During each on-pulse current ramps up; during the off-time it keeps flowing through a freewheeling path. In slow decay, the H-bridge shorts the motor terminals so current recirculates and decays gently, which improves low-speed smoothness. In fast decay, the driver applies reverse voltage or returns energy toward the supply, so current drops quickly; this tracks commands better but increases ripple. Many drivers use mixed decay.

A quick ripple estimate shows why frequency matters. Approximate a small motor winding as R = 6 ohm and L = 1 mH on a 12 V supply. If the shaft is already spinning so back-EMF is 4 V, the on-pulse applies roughly 12 - 4 = 8 V across the winding. The current slope is di/dt = V/L = 8 / 0.001 = 8000 A/s. At 20 kHz and 35% duty, on-time is 17.5 microseconds, so the current rises by about 8000 x 17.5e-6 = 0.14 A during the on-pulse. At 2 kHz with the same duty, on-time is 175 microseconds and the rise is about 1.4 A. That larger ripple can produce audible vibration, rough torque, and extra heating even though the average duty cycle is unchanged.

The power stage must also survive the switching. A MOSFET that drops only 80 mV at 1 A dissipates about 0.08 W while fully on, but if slow gate drive leaves it in the linear region for 1 microsecond on each edge, 20 kHz PWM creates 40000 switching edges per second and edge losses can become comparable to conduction loss. This is why motor boards include gate resistors, flyback paths, bulk capacitors near the driver, and thermal ratings. The waveform is digital at the pin, but the motor current, heat, and electromagnetic noise are analog consequences.

Motor MaxCheckpoint: Motor Driver Evidence

You now know:

  • At 20 kHz and 35% duty, the fan example has a 50 microsecond period, 17.5 microseconds on-time, and an ideal 4.2 V average from 12 V.
  • The same 35% command is about 89 counts on 8-bit PWM and 358 counts on 10-bit PWM.
  • Ripple depends on winding inductance and frequency: the chapter’s 1 mH example rises about 0.14 A at 20 kHz but about 1.4 A at 2 kHz.
  • Driver evidence includes decay mode, freewheeling path, gate drive, bulk capacitance, and thermal rating, not only the duty number.

9.9 Summary

PWM turns a digital pin into proportional actuator control by switching quickly and changing duty cycle. The correct design depends on matching frequency and resolution to the load: servos need timed pulses, DC motors need current smoothing and quiet switching, and LEDs need flicker-free brightness control.

Key Takeaway

PWM is the universal technique for controlling actuator output (speed, brightness, position) using digital microcontroller pins. The key parameters are duty cycle (controls power output), frequency (must match the actuator type — 50 Hz for servos, 1-20 kHz for motors, 500+ Hz for LEDs), and resolution (determines how many discrete control levels are available). The average voltage delivered to an actuator equals the supply voltage multiplied by the duty cycle percentage.

9.10 Knowledge Check

9.11 Knowledge Check - Interactive Quizzes

9.12 Concept Relationships

ConceptRelates ToConnection Type
Duty CycleDC MotorsControls motor speed via average voltage
PWM FrequencyServo MotorsServos require exactly 50 Hz for position control
Resolution (8/10/12-bit)ESP32 CapabilitiesHigher resolution = finer speed control
Average PowerElectronicsPower dissipation = V_avg × I

9.13 See Also

Common Pitfalls

The Arduino analogWrite() function does not work on ESP32. The ESP32 uses the LEDC (LED Control) peripheral which requires channel setup (ledcSetup(channel, freq, resolution)) and pin attachment (ledcAttachPin(pin, channel)) before ledcWrite() will function. Attempting analogWrite() on ESP32 silently produces no PWM output.

Standard RC servos require 50 Hz PWM (20 ms period). Using higher frequencies (e.g., 1 kHz from analogWrite()) causes the servo to jitter, overheat, or receive incorrect position commands. Always configure the PWM channel to exactly 50 Hz when using PWM to control servo motors — or better, use the Arduino Servo.h library which handles frequency automatically.

DC motor coils vibrate mechanically at the PWM switching frequency. PWM at 1 kHz or 5 kHz produces clearly audible tones from the motor. Set motor PWM frequency above 20 kHz (beyond human hearing range) to eliminate audible noise. This requires direct timer configuration on Arduino; the ESP32 LEDC peripheral supports frequencies up to 40 MHz.

ESP32 GPIO outputs 3.3 V HIGH; many H-bridge driver ICs (L298N) have input thresholds designed for 5 V logic and may not reliably switch at 3.3 V. Verify the driver IC’s input HIGH voltage threshold against your microcontroller’s output voltage; some drivers require a logic level shifter or accept 3.3 V inputs directly (TB6612FNG, DRV8833).

9.14 What’s Next

If you want to…Read this
Learn about servo motor control which uses specific PWM pulse widthsServo Motors
Understand DC motor control with PWM and H-bridgesDC Motors
Explore stepper motor control and step sequencesStepper Motors
Learn about relay and solenoid control with digital outputsRelays and Solenoids

9.15 Continue Your Route

This final part closes the route from Choosing PWM Frequency: Decision Factors through Code Challenge. Return to PWM Control: Signals and Duty-Cycle Behaviour or continue from the actuators module index.