Home / Learn Embedded C / What Is PWM?

What Is PWM?

Pulse Width Modulation — simulating an analog-like output using a fast digital on/off signal.

Quick summaryPulse Width Modulation — simulating an analog-like output using a fast digital on/off signal.

How to think about this

PWM duty cycle examples25% duty cycleMostly LOW, briefly HIGH eachcycleAppears dim / slow / weak75% duty cycleMostly HIGH, briefly LOW eachcycleAppears bright / fast / strong
PWM duty cycle examples

Definition

PWM (Pulse Width Modulation) rapidly switches a digital output on and off at a fixed frequency, varying the fraction of time it spends HIGH (the "duty cycle") to simulate an analog-like average voltage.

Why you need this

Many things a microcontroller needs to control in a graded, analog-like way (LED brightness, motor speed, servo position) can be driven from simple digital output pins via PWM, without needing a true analog output.

How it works

A duty cycle of 100% looks fully on, 0% looks fully off, and 50% looks "half" on — the receiving device or human eye/ear perceives the time-average of the fast-switching signal rather than the individual pulses.

Where you'll see this

  • LED dimming
  • Motor speed control
  • Servo positioning

Try it

Simulating PWM duty cycle with the PWM_WRITE stubC: edit and run

PWM_WRITE in this sandbox takes a duty value out of 255, mirroring the common 8-bit PWM convention on many boards.

Try it: computing duty cycle percentage from a raw PWM value

Converting a 0-255 PWM value into a human-readable percentageC: edit and run

Most PWM peripherals accept a raw count out of some maximum (here 255), and converting to a percentage is just simple integer math.

Test what you just read

1. What does PWM "duty cycle" refer to?