PWM (Pulse-Width Modulation): a Hands-On Guide & Deep Dive
by owen000821 in Circuits > Arduino
36 Views, 1 Favorites, 0 Comments
PWM (Pulse-Width Modulation): a Hands-On Guide & Deep Dive

Pulse-Width Modulation (PWM) is one of the most fundamental techniques in power electronics, microcontroller control, motor drivers, LED dimming, and more. The beauty of PWM lies in its simplicity and efficiency: by rapidly switching a digital signal on and off, you can approximate variable analog control without wasting energy as heat.
In this tutorial, we’ll:
- Explain PWM fundamentals and key parameters
- Walk through how to generate PWM using a microcontroller
- Demonstrate how PWM controls devices like LEDs or motors
- Discuss pitfalls, practical considerations, and improvements
- Provide a sample project at the end
If you like, I can also generate code examples (Arduino, STM32, ESP32) or printable schematics.
Supplies
Tools & Components Needed
Basic Electronics Tools
- Solderless Breadboard – for quick circuit prototyping.
- Jumper Wires / Dupont Cables – for interconnections on the breadboard.
- Soldering Iron & Solder (optional) – for making permanent connections.
- Wire Stripper & Cutter – for preparing jumper wires.
- Multimeter (DMM) – to measure voltage, current, and duty cycle.
- Oscilloscope (recommended) – to visualize the PWM waveform and verify duty cycle and frequency.
- USB Cable / Programmer – for uploading code to your microcontroller board.
- Screwdriver Set / Pliers – for assembling or tightening connections.
Electronic Components
- Microcontroller Board
- Arduino Uno / Nano / ESP32 / STM32 (any with PWM pins).
- LEDs (Standard or RGB)
- For simple PWM brightness control or RGB color mixing experiments.
- Resistors
- 220 Ω – 1 kΩ for current limiting LEDs.
- 10 kΩ potentiometer (optional) for analog input to control duty cycle.
- NPN Transistor or MOSFET (logic-level)
- e.g., 2N2222, IRLZ44N, or AO3400 for driving motors or high-power LEDs.
- Flyback Diode
- 1N4007 or Schottky diode for inductive load protection (motors, relays).
- DC Motor / Fan
- For motor speed control demonstration.
- Power Supply
- 5 V / 12 V regulated adapter or a battery pack depending on load.
- RC Filter Components (optional)
- 1 kΩ resistor and 10 µF capacitor to convert PWM to analog voltage.
- RGB LED Module or Strip (optional)
- To demonstrate multiple PWM channels for color blending.
What Is PWM?
Pulse-Width Modulation (PWM) is a way of encoding a variable “average” voltage by switching a signal between a full-on (e.g. 5 V) and full-off (0 V) state at a fixed frequency, and changing the fraction of time (the duty cycle) the signal is “on.”
- High time (Ton): duration the signal is “on” (voltage high)
- Low time (Toff): duration the signal is “off” (voltage low)
- Period (T) = Ton + Toff
- Frequency (f) = 1 / T
- Duty cycle (D) = Ton / T (often expressed as a percentage)
For example:
- If Ton = 2 ms and Toff = 2 ms, T = 4 ms → f = 250 Hz, D = 50%
- If Ton = 1 ms and Toff = 3 ms, D = 25%, etc.
By changing D while holding f constant (and doing this fast relative to the load’s dynamics), the load
Key Parameters & Design Considerations
When designing or choosing a PWM system, the following parameters matter:
- Switching Frequency (f):
- Too low → the load may respond to individual pulses (visible flicker in LEDs, jerky motor motion).
- Too high → more switching losses, device limitations (MOSFET gate charge, driver overhead).
- Choose a frequency appropriate for your load. For LEDs, a few kHz is often enough. For motors, tens to hundreds of kHz may be used (depending on switching losses, inductance, etc.).
- Duty Cycle Range / Resolution:
- The granularity of control depends on how fine your duty cycle steps are (e.g. 8-bit → 256 levels, 10-bit → 1024 levels).
- In microcontrollers, timers often give you a resolution based on clock frequency and prescaler settings.
- Voltage & Current Limits:
- The output switch (e.g. transistor, MOSFET) must be rated for the maximum voltage and current you’ll drive.
- Use gate drivers, proper heat sinking, and consider transient protection (snubbers, freewheeling diodes for inductive loads).
- Filtering & Smoothing (Optional):
- In some cases, you may want to filter the PWM (e.g., with an RC low-pass filter) to get a “real” analog voltage.
- But filtering introduces delay, phase shift, and attenuation; only do it if needed.
- Dead Time (for H-bridges or push-pull):
- In bridged topologies, ensure you insert a short “dead time” when both switches are off to avoid shoot-through currents.
Generating PWM in a Microcontroller
Here’s a generic approach (e.g. Arduino / STM / ESP):
- Select Timer / PWM Peripheral: Most micros have 8-bit, 16-bit timers capable of PWM.
- Configure the Timer:
- Choose a prescaler to scale down your microcontroller clock.
- Choose a top value (the maximum count, e.g. 255 in 8-bit) to define the PWM period.
- Set compare (or compare match) registers to define duty cycle.
- Enable the Output Pin in PWM Mode: Assign the PWM-capable pin to the timer’s PWM output.
- Update Duty Cycle Dynamically: By writing new compare register values, you change Ton (i.e. duty cycle) while the timer continues operating.
- Optional: Interrupts / Callbacks: On overflow or compare match, you can execute code (e.g. update values, trigger events).
Practical Example: LED Brightness Control
Parts Required:
- Microcontroller board (Arduino Uno, ESP32, etc.)
- LED + current-limiting resistor
- Breadboard, wires
Circuit:
- Connect LED (with resistor) to a PWM pin and to ground.
Code Sketch (pseudo / Arduino-style):
This code smoothly dims up and down the LED. The analogWrite() abstracts setting the timer compare register.
When the duty is low, the LED appears dimmer because the “off” periods dominate; when high, it's brighter.
Example 2: Motor Speed Control
Because PWM switches rapidly, the inductance of motor windings acts to smooth the current. The average voltage (hence torque) can be controlled by duty cycle.
- Use a MOSFET or MOSFET H-bridge driver.
- Include a flyback diode or use driver with built-in freewheeling paths.
- Pick PWM frequency so that motor current ripple is acceptable (depends on inductance, load).
- Monitor thermal behavior (switching & conduction losses).
Sample Project: PWM-Driven RGB LED Mood Lamp
Objective: Use three PWM channels to control red, green, blue LED intensities and cycle through colors.
Hardware:
- Microcontroller with at least 3 PWM pins
- 3 high-power LEDs (R, G, B) + resistors or LED modules
- MOSFET drivers if needed (for high current)
Schematic Sketch:
Pseudo Workflow:
- Initialize PWM for 3 channels.
- Use a loop that smoothly increments / decrements duty values to mix colors (e.g. fade red → green → blue combinations).
- Optionally read in a potentiometer or sensor to adjust speed or brightness.
With this, you can build a mood lamp whose color transitions are controlled elegantly via PWM.
Summary & Further Exploration
- PWM is efficient, energy-conserving, and ubiquitous in modern electronics control.
- The key levers are frequency, duty cycle resolution, and switching device selection.
- Always consider real-world constraints: switching losses, EMI, heat, and load behavior.
- Once you’re comfortable, you can build more advanced systems: DC-DC converters (buck/boost), class-D audio amplifiers, motor control with current feedback, digital filtering of PWM, etc.