PWM stands for Pulse Width Modulation, a technique that controls the average power delivered to a load by rapidly switching a digital signal on and off. In a real circuit or installation, PWM changes the effective thermal, luminous, or kinetic energy transferred to a component without altering the peak supply voltage or wasting energy as heat in a linear regulator. By keeping the voltage at its maximum rated level (like 5V or 12V) but chopping the time it is applied, microcontrollers can efficiently dim LEDs, spin motors at precise speeds, and position servos using only simple digital output pins.

The Core Mechanism: Duty Cycle, Frequency, and Real Numbers

To understand how PWM works, you only need to grasp two parameters: frequency and duty cycle. Frequency dictates how many on/off cycles occur per second (measured in Hertz), while the duty cycle is the percentage of time the signal spends in the 'HIGH' (on) state during a single cycle.

Think of it like a light switch. If you leave a switch on for half a second and off for half a second, the room is illuminated 50% of the time. If you do this 1,000 times a second, the human eye cannot track the flickering; it averages the light, perceiving it as half-brightness. That is the single analogy you need: PWM tricks the load (or the observer) into averaging a digital square wave into a pseudo-analog result.

A Worked Numeric Example: ESP32 Fan Control

Let us look at a practical bench scenario. You are using an ESP32 to drive a 12V DC cooling fan via a logic-level N-channel MOSFET (like the IRLZ44N). The ESP32 GPIO outputs a 3.3V logic signal to the MOSFET gate.

  • Frequency: We set the PWM frequency to 25,000 Hz (25 kHz). Why? Because frequencies below 20 kHz can cause the motor windings to vibrate at audible frequencies, creating an annoying high-pitched whine. 25 kHz pushes it above human hearing.
  • Duty Cycle: We set the duty cycle to 75%.
  • Timing: At 25 kHz, one full cycle takes exactly 40 microseconds (1 / 25,000). A 75% duty cycle means the gate is driven HIGH for 30 microseconds, then LOW for 10 microseconds.

The Result: The fan receives the full 12V from the power supply, but only 75% of the time. The effective average voltage across the motor windings is 9V (12V × 0.75). The fan spins at roughly 75% of its maximum speed, and the MOSFET dissipates almost zero heat because it is either fully on (low resistance) or fully off (zero current).

Key Metric: A 10-bit PWM resolution on an ESP32 yields 1024 discrete steps (0-1023). Writing a value of 512 to the LEDC driver yields exactly a 50.0% duty cycle, while an 8-bit Arduino Uno yields 256 steps (0-255), where 127 is approximately 49.8%.

Where You Meet This in Practice

You will encounter PWM in almost every embedded project that interacts with the physical world. Here are the three most common applications and the specific hardware considerations for each.

1. DC Motor Speed Control

When driving motors via an H-bridge (like the L298N or DRV8871), PWM controls speed. However, motors have static friction (stiction). A 10% duty cycle might provide enough average voltage to keep a motor spinning, but not enough initial torque to start it from a dead stop. In practice, you often need to 'kick' the motor with a 100% duty cycle pulse for 50 milliseconds before dropping down to your target 20% cruising speed.

Safety Warning: DC motors are inductive loads. When the PWM signal switches OFF, the collapsing magnetic field generates a high-voltage spike (inductive kickback). You must always wire a flyback diode (like a 1N4007) in reverse parallel across the motor terminals, or use an H-bridge driver with built-in clamp diodes, to prevent frying your microcontroller.

2. LED Dimming

Human perception of brightness is logarithmic, not linear. A 50% PWM duty cycle will look significantly brighter than 'half brightness' to the human eye. When building custom lighting with an Arduino or ESP32, you must apply a gamma correction curve in your code to map linear PWM steps to perceived linear brightness.

3. Hobby Servo Positioning

Standard RC servos (like the SG90 or MG995) use a very specific, low-frequency PWM signal: exactly 50 Hz (one pulse every 20 milliseconds). The position of the servo arm is not determined by the duty cycle percentage, but by the absolute width of the HIGH pulse. A 1.0 ms pulse moves the servo to 0 degrees, 1.5 ms centers it at 90 degrees, and 2.0 ms moves it to 180 degrees.

The Great Confusion: PWM vs. True Analog (DAC)

What people most commonly confuse PWM with is a true analog voltage output, also known as a Digital-to-Analog Converter (DAC). PWM is strictly a digital square wave. If you measure a 50% duty cycle 5V PWM signal with a standard multimeter set to DC Volts, the meter's internal low-pass filter will average the signal and display roughly 2.5V. However, if you hook that same pin to an oscilloscope, you will see it violently snapping between 0.0V and 5.0V. It is not a steady 2.5V DC source. If you connect a PWM pin directly to an analog sensor input expecting a steady bias voltage, you will get erratic readings unless you first pass the signal through a hardware RC low-pass filter to smooth the square wave into a true DC voltage.

Hardware Timers vs. Software Bit-Banging

When programming microcontrollers, you can generate PWM signals using dedicated hardware peripherals or by manually toggling pins in software (bit-banging). For reliable operation, you should almost always use hardware PWM. The Arduino analogWrite() function and the Espressif LEDC API both utilize hardware timers under the hood.

Feature Hardware PWM (Timers) Software PWM (Bit-Banging)
CPU Overhead Zero (runs in background) High (blocks CPU or requires strict interrupts)
Frequency Stability Perfectly stable, no jitter Prone to jitter if interrupts fire
Pin Flexibility Limited to specific timer-routed pins Any GPIO pin can be used
Max Frequency High (up to 40+ MHz on ESP32) Low (usually < 10 kHz reliably)
Best Used For Motors, LEDs, Servos, Power supplies Slow-blinking indicators, simple buzzers

Frequently Asked Questions About PWM

What does PWM stand for in Arduino programming?

In Arduino programming, PWM stands for Pulse Width Modulation, but it is most frequently encountered via the analogWrite() function. This naming convention confuses many beginners who assume the function outputs a true analog voltage. On standard 8-bit AVR boards like the Arduino Uno or Nano, analogWrite(pin, value) accepts a value from 0 to 255. A value of 0 outputs a steady 0V (0% duty cycle), 255 outputs a steady 5V (100% duty cycle), and 127 outputs a 5V square wave that is HIGH roughly 49.8% of the time. It only works on pins marked with a tilde (~) on the board silkscreen, as those are physically wired to the microcontroller's internal hardware timers.

What PWM stands for when controlling high-power DC motors?

When scaling up to high-power DC motors (e.g., 24V scooter motors or 48V winches), PWM still stands for Pulse Width Modulation, but the implementation shifts from microcontroller pins to heavy-duty gate drivers. A 3.3V logic signal from an ESP32 cannot directly switch a 50A motor. Instead, the microcontroller sends the PWM signal to an opto-isolated gate driver board, which then rapidly switches high-current IGBTs or parallel MOSFETs. At high currents, the switching frequency is often lowered (e.g., to 1 kHz - 4 kHz) to reduce switching losses and heat generation in the power transistors, even if it introduces slight audible whine.

Is PWM the same as a true analog voltage output?

No. PWM is a digital switching technique, whereas a true analog output is generated by a Digital-to-Analog Converter (DAC). A DAC uses an internal resistor ladder or capacitor array to output a steady, continuous DC voltage (e.g., exactly 1.65V with no ripple). Some microcontrollers, like the original ESP32, include a built-in 8-bit DAC on specific pins (GPIO 25 and 26) via the dacWrite() function. However, DACs cannot supply high current to drive loads directly; they are strictly for providing reference voltages to op-amps or audio amplifiers. For driving power loads like heaters, motors, and high-power LEDs, PWM is vastly superior because the switching transistors operate at near 100% efficiency, whereas a linear analog output would burn off the excess voltage as waste heat.