, , ,

What is PWM Pulse Width Modulation ? | Master Beginner-Friendly Guide 2025

Introduction to PWM (Pulse Width Modulation)

PWM Pulse Width Modulation : PWM stands for Pulse Width Modulation, a technique used to control the amount of power delivered to electrical devices. It is widely used in electronics, especially when dealing with motors, LEDs, and audio signals. PWM allows you to control the intensity, speed, or output of a device without changing the voltage — just by adjusting the signal’s duty cycle.In electronic engineering, PWM (Pulse Width Modulation) is a powerful technique used to control the power delivered to electronic devices. It is especially useful when you want to simulate analog output using digital signals.

PWM is commonly used in microcontrollers like Arduino, Raspberry Pi, and ESP32 to control devices such as LEDs, motors, and buzzers.

Why is PWM Important?

PWM is essential for many embedded systems and electrical engineering applications. Instead of sending a constant voltage, PWM sends a series of ON and OFF signals (called pulses). By varying how long the signal stays ON vs OFF, we can efficiently control the output without wasting energy as heat.

How PWM Works – The Basics

PWM works by switching a digital signal ON and OFF very quickly. The signal looks like a square wave, where the “HIGH” means ON and “LOW” means OFF.

Instead of giving full power all the time, you control how long the signal stays ON in each cycle. This gives the effect of partial power.

Key Concepts:

  • Duty Cycle: The percentage of time the signal is ON in one cycle.
    • 0% = always OFF
    • 50% = ON half the time
    • 100% = always ON
  • Frequency: How fast the PWM signal cycles between ON and OFF. It’s measured in Hertz (Hz). A higher frequency gives smoother control.

Real-Life Examples of PWM

  1. LED Dimming: PWM adjusts LED brightness by controlling how long it stays ON vs OFF.
  2. Motor Speed Control: PWM drives DC motors at variable speeds using the same voltage.
  3. Audio Signal Generation: PWM is used to generate and shape sound waves in digital audio systems.
  4. Heater and Fan Control: In HVAC systems, PWM provides precise thermal and airflow control.

PWM with Arduino (Beginner Code Example)

Here’s a simple Arduino example to dim an LED using PWM:

int ledPin = 9;  // PWM-capable pin

void setup() {
  pinMode(ledPin, OUTPUT);
}

void loop() {
  for (int brightness = 0; brightness <= 255; brightness++) {
    analogWrite(ledPin, brightness); // 0-255 PWM value
    delay(10);
  }

  for (int brightness = 255; brightness >= 0; brightness--) {
    analogWrite(ledPin, brightness);
    delay(10);
  }
}

This code increases and decreases the LED brightness smoothly using PWM.

A Pulse Width Modulation (PWM) signal is generated by rapidly switching a digital output pin ON and OFF at a fixed frequency, while varying the duration of the ON time (duty cycle) within each cycle. This switching creates a waveform that mimics an analog signal’s average power over time.

How PWM Signal is Generated – Step-by-Step

1. Select a PWM Frequency

  • Frequency defines how fast the signal repeats each cycle.
  • Common values: 490 Hz, 1 kHz, 20 kHz (used in motors, audio, LED dimming).

2. Set a Duty Cycle

  • This is the percentage of time the signal stays ON in one cycle.
  • For example:
    • 25% duty cycle: ON for 25% of the time, OFF for 75%
    • 75% duty cycle: ON for 75% of the time, OFF for 25%

3. Toggle the Digital Pin Accordingly

Use a timer or counter (hardware or software) to toggle the pin:

In Microcontrollers (like Arduino):

The hardware timers generate PWM by:

  • Counting clock pulses
  • Comparing the counter to a set value
  • Turning output HIGH until the match is reached
  • Resetting the counter to repeat the cycle

Software Example (Pseudocode):

loop every 1 ms {
  if (time < ON_time) {
    setPinHigh();
  } else {
    setPinLow();
  }
}

PWM Generation Methods

1. Using Microcontroller Timers (Hardware PWM)

  • Efficient
  • More accurate
  • Less CPU usage

2. Software PWM (Bit-Banging)

  • Controlled manually in code
  • Uses delay() or timers
  • More flexible, but consumes CPU time

PWM Signal Characteristics

ParameterDescription
FrequencySpeed of the PWM cycle (Hz)
Duty Cycle% of time signal stays HIGH
ResolutionBit-level control (8-bit = 256 steps)

Example: Arduino 50% PWM Signal on Pin 9

void setup() {
  pinMode(9, OUTPUT);
}

void loop() {
  analogWrite(9, 128);  // 50% of 255 = 128 duty cycle
}

This code sends a square wave on pin 9 with a 50% duty cycle, effectively simulating half power output.

Important parameters associated with a PWM (Pulse Width Modulation)

1. Duty Cycle (%)

What It Is:

The percentage of one cycle in which the signal is HIGH (ON).

Formula:

Duty Cycle = (ON Time / Total Time) × 100%

Example:

  • 0% = Always OFF
  • 50% = ON half the time
  • 100% = Always ON

Importance:

Controls the average power delivered to a device.

2. Frequency (Hz)

What It Is:

The number of PWM cycles per second.

Formula:

Frequency = 1 / Period

Example:

  • 1 kHz = 1000 cycles per second

Importance:

  • Low frequency → Visible flicker (LEDs), noise (motors)
  • High frequency → Smoother response but harder on switching circuits

3. Period (T)

What It Is:

The total time for one complete PWM cycle.

Formula:

Period = 1 / Frequency

Importance:

Gives insight into how long each ON and OFF phase can last.

4. Resolution (Bits)

What It Is:

The number of discrete levels for duty cycle adjustment.

Example:

  • 8-bit = 256 levels (0–255)
  • 10-bit = 1024 levels (0–1023)

Importance:

Higher resolution = finer control over output (e.g., smoother dimming or speed control).

5. Pulse Width (ON Time)

What It Is:

The actual duration the signal stays HIGH in a single cycle.

Formula:

Pulse Width = Duty Cycle × Period

Importance:

Used to determine how much energy is transferred to the load during each cycle.

6. Rise Time / Fall Time

What It Is:

  • Rise Time: Time taken for signal to go from LOW to HIGH.
  • Fall Time: Time taken for signal to go from HIGH to LOW.

Importance:

Critical in high-speed or precision circuits, affects signal integrity and efficiency.

Summary Table

ParameterDescriptionUnits
Duty Cycle% of time signal is ON%
FrequencyNumber of PWM cycles per secondHertz (Hz)
PeriodTime for one complete PWM cycleSeconds (s)
ResolutionLevels of control over duty cycleBits
Pulse WidthActual ON duration per cycleSeconds (s)
Rise/Fall TimeTime to switch between statesSeconds (s)

Pulse Position Modulation vs. Pulse Width Modulation

FeaturePulse Width Modulation (PWM)Pulse Position Modulation (PPM)
DefinitionModulates the width (duration) of the pulseModulates the position (timing) of the pulse
How It WorksVaries how long the signal stays ON within a fixed periodKeeps the pulse width fixed but shifts its position in time
Signal TypePulse duration varies; position is fixedPulse position varies; duration is fixed
Duty CycleA key parameter (affects power output)Not applicable
Timing AccuracyLess sensitive to timing errorsHighly sensitive to synchronization and timing accuracy
Power ControlExcellent for power control in motors, LEDs, etc.Less effective for power control
Analog RepresentationAverage voltage corresponds to analog input levelPulse position corresponds to analog signal amplitude
Noise ImmunityBetter resistance to noise and interferenceMore prone to noise due to time shift sensitivity
Hardware ComplexityEasier to generate with timers (common in microcontrollers)More complex; needs precise timing circuits
Example Use CasesMotor speed control, LED dimming, audio, power managementRemote control systems, optical communication

In Simple Terms:

  • PWM (Pulse Width Modulation):
    ➤ “Change the length of the pulse, but send it at the same time every cycle.”
    ➤ Best for controlling power, like in fans, lights, and speakers.
  • PPM (Pulse Position Modulation):
    ➤ “Send the pulse at different times, but keep its width the same.”
    ➤ Best for data transmission, like in radio control or optical links.

Example Illustration:

PWM Example (50%, 75%, 25% duty cycles):

|‾‾‾‾‾|_____|     50%
|‾‾‾‾‾‾‾|__|     75%
|‾‾|______|     25%

PPM Example (Pulse moves in time):

|‾|___     Low signal
___|‾|_     Medium signal
______|‾|  High signal

Types of Pulse Width Modulation Techniques Explained

Pulse Width Modulation (PWM) is a powerful method used to control power delivery by switching signals ON and OFF rapidly. While PWM always controls the duty cycle, there are several ways to implement PWM based on what is varied: width, leading edge, or trailing edge.

1. Leading Edge Modulation (LEM) / Trail Constant PWM

Description:

  • The rising edge (start of the pulse) is fixed.
  • The falling edge (end of the pulse) is varied to change the pulse width.

Diagram:

|‾‾‾|_____
|‾‾‾‾‾|___
|‾‾|______

Use Case:

  • Common in motor control and power supplies where starting edge alignment is needed.

2. Trailing Edge Modulation (TEM) / Lead Constant PWM

Description:

  • The falling edge (end of the pulse) is fixed.
  • The rising edge is moved to change the width.

Diagram:

__|‾‾‾|
_|‾‾‾‾‾|
___|‾‾|

Use Case:

  • Often used in digital circuits, especially when a clean pulse end is required for synchronization.

3. Center-Aligned PWM (Symmetric PWM)

Description:

  • The pulse is centered in the middle of the period.
  • Both rising and falling edges are varied symmetrically to maintain the center.

Diagram:

__|‾‾|__
_|‾‾‾‾‾|_
|‾‾‾‾‾‾‾|

Use Case:

  • Ideal for motor drives and inverters to minimize harmonic distortion.

4. Phase-Correct PWM

Description:

  • Counter counts up and then down (rather than resetting to 0), producing a symmetrical waveform.

Benefit:

  • Eliminates phase shift and reduces noise in sensitive applications.

Use Case:

  • Found in applications like servo motor control and DC motor speed control.

Summary Table

PWM TypeFixed EdgeVariable EdgeBest Used In
Leading Edge ModulationRising (Start)Falling (End)Power electronics, SMPS
Trailing Edge ModulationFalling (End)Rising (Start)Digital and timing-sensitive apps
Center-Aligned PWMCentered PulseBoth EdgesAC motor drives, inverters
Phase-Correct PWMSymmetric CountTime-balancedServo motors, precision control

1. PWM Using Arduino (ATmega328p)

The Arduino UNO uses an 8-bit timer by default for PWM. It typically supports Fast PWM and Phase Correct PWM.

Basic Fast PWM (e.g., Trailing Edge Modulation)

// Generates PWM on pin 9 with 50% duty cycle
void setup() {
  pinMode(9, OUTPUT);
  analogWrite(9, 127); // 127 = ~50% of 255
}

void loop() {
  // Nothing needed here for static PWM
}

Phase Correct PWM (Using Timer1 Directly)

This gives symmetrical PWM for precision applications.

void setup() {
  pinMode(9, OUTPUT); // OC1A pin

  // Configure Timer1 for Phase Correct PWM
  TCCR1A = (1 << COM1A1) | (1 << WGM11);
  TCCR1B = (1 << WGM13) | (1 << CS11); // Prescaler = 8
  ICR1 = 39999;       // TOP value (for 50 Hz PWM)
  OCR1A = 2999;       // Duty cycle = 7.5% (centered)
}

void loop() {
  // You can adjust OCR1A dynamically for servo control
}

2. PWM Using STM32 (HAL-based, Center-Aligned & Edge PWM)

STM32 microcontrollers offer flexible PWM control using TIMx timers.

Center-Aligned PWM (HAL/STM32CubeIDE)

Set the timer in Center-Aligned Mode 1:

// Pseudo-code for STM32 HAL (CubeMX generated setup assumed)
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, pulseValue);

Ensure in CubeMX or manually:

  • Timer mode = Center-Aligned Mode 1
  • PWM output on a GPIO pin (e.g., PA8 for TIM1_CH1)
  • Pulse width = (DutyCycle% * TimerPeriod) / 100

Edge-Aligned PWM (Default)

// Basic PWM Start
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);

// Set Duty Cycle (50%)
__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1, htim3.Init.Period / 2);

Summary: PWM Types by Code

PWM TypeArduino SupportSTM32 Support
Leading EdgeanalogWrite()Timer Edge Mode
Trailing EdgeCustom Timer ConfigDefault Mode
Center-AlignedTimer1 Mode NeededCenter-Aligned Mode
Phase CorrectTimer1 OCR1Available in TIMx

Advantages of Using PWM

  • Efficient Power Control: Less heat and energy waste.
  • Digital Control: Easy to implement using microcontrollers.
  • High Precision: Fine-grained control over devices.
  • Low Cost: No need for complex analog circuits.

Common Devices that Use PWM

  • Servo Motors
  • DC Motors
  • LED drivers
  • Power supplies
  • Embedded systems (like Arduino, ESP32, STM32)

Conclusion

PWM (Pulse Width Modulation) is a powerful and energy-efficient technique to control electrical devices using digital signals. From dimming lights to controlling motors, PWM is everywhere in electronics. If you’re starting your journey in embedded systems or electronics, mastering PWM will open the doors to building exciting and dynamic projects.

Commonly asked PWM (Pulse Width Modulation) interview questions

Basic PWM Interview Questions

  1. What is PWM and why is it used?
  2. Explain the term ‘duty cycle’ in PWM.
  3. What are the applications of PWM in embedded systems?
  4. How does PWM simulate analog output using digital signals?
  5. What is the typical frequency range used in PWM?
  6. What is the difference between duty cycle and frequency?
  7. How does changing the duty cycle affect motor speed or LED brightness?
  8. Which microcontroller pins are used for PWM output in Arduino?
  9. What is the default PWM frequency on Arduino’s analogWrite()?
  10. Can PWM signal be used to control both AC and DC devices?

Intermediate PWM Interview Questions

  1. What are the different PWM modes in microcontrollers (e.g., Fast PWM, Phase Correct PWM)?
  2. Explain how center-aligned PWM differs from edge-aligned PWM.
  3. How is PWM implemented using timers?
  4. What is the impact of frequency on the performance of a PWM system?
  5. How do you generate a PWM signal using a timer peripheral in STM32?
  6. What are the drawbacks of using PWM?
  7. Explain how you would generate a 1 kHz PWM signal with 75% duty cycle.
  8. What are dead-time and its importance in PWM-based motor control?
  9. What is jitter in PWM signals, and how does it affect performance?
  10. Describe how a low-pass filter converts a PWM signal into analog voltage.

Advanced PWM Interview Questions

  1. How would you implement software PWM without using a hardware timer?
  2. Explain the difference between Pulse Width Modulation (PWM) and Pulse Position Modulation (PPM).
  3. How is PWM used in Class-D audio amplifiers?
  4. What are harmonics in PWM, and how can they be reduced?
  5. How would you synchronize multiple PWM channels in real-time control systems?
  6. Describe how PWM is used in buck/boost converters.
  7. What techniques are used to improve PWM resolution in low-frequency systems?
  8. How is complementary PWM used in full-bridge motor driver circuits?
  9. Explain phase-shifted PWM and its applications.
  10. How does the resolution (bit-depth) of PWM affect output quality?

Frequently Asked Questions (FAQs) on PWM (Pulse Width Modulation)

What is PWM (Pulse Width Modulation)?

PWM (Pulse Width Modulation) is a technique used to control the average power delivered to electronic devices by rapidly switching the signal between ON and OFF states. The duty cycle determines how long the signal stays ON during one cycle.

What is the duty cycle in PWM?

The duty cycle is the percentage of one period in which the signal is ON. For example:

  • 0% duty cycle = always OFF
  • 100% duty cycle = always ON
  • 50% duty cycle = ON for half the time

Where is PWM used?

PWM is commonly used in:

  • Motor speed control
  • LED dimming
  • Audio signal generation
  • Power delivery systems
  • Servo control (robotics, RC models)

How is PWM different from analog voltage?

PWM uses digital signals (ON/OFF pulses) to simulate analog output by changing the duty cycle. The average voltage over time can mimic analog behavior without using a true analog signal.

What are the types of PWM techniques?

The major types of PWM include:

  • Leading Edge Modulation (Fixed rising edge)
  • Trailing Edge Modulation (Fixed falling edge)
  • Center-Aligned PWM (Pulse is centered)
  • Phase-Correct PWM (Symmetrical counting)

What’s the difference between PWM and PPM?

  • PWM (Pulse Width Modulation) varies the pulse width to encode data or control power.
  • PPM (Pulse Position Modulation) varies the position of a constant-width pulse within a time frame.

How do I generate PWM in Arduino?

Use the built-in analogWrite(pin, value) function. It takes a value between 0–255, where:

  • 0 = 0% duty cycle (always OFF)
  • 255 = 100% duty cycle (always ON)

Example:

analogWrite(9, 127); // ~50% duty cycle on pin 9

How do I control motor speed using PWM?

PWM controls the motor’s speed by adjusting the average voltage supplied to the motor. A higher duty cycle = more speed; a lower duty cycle = slower speed.

Does PWM waste energy like linear voltage control?

No. PWM is more energy-efficient because the switching components (like transistors) are either fully ON or fully OFF, which reduces power loss.

Can PWM be used for audio signals?

Yes. PWM can represent analog waveforms such as audio tones. After low-pass filtering, the PWM signal can be used to produce audible sound.

You can also Visit other tutorials of Embedded Prep 

Special thanks to @mr-raj for contributing to this article on Embedded Pre

Leave a Reply

Your email address will not be published. Required fields are marked *