We detect you are using an unsupported browser. For the best experience, please visit the site using Chrome, Firefox, Safari, or Edge. X

What Is Pulse-Width Modulation?


A Pulse-Width Modulation (PWM) peripheral is used to generate a digital signal with a user-defined period and duty cycle (“ON” time) that can be used to encode a message or control the amount of power supplied to electrical devices. This type of signal can be used in a variety of applications such as motor control, LED lighting and power supplies.   

Depending on when the PWM signal is active high during each period, there are two common types of PWMs:

  • The widely used edge-aligned PWM offers simple configuration, fast response and higher resolution
  • The center-aligned PWM is extremely helpful in reducing the harmonics in the output of motor control, power supply and other applications
  • Some select devices can even generate variable-aligned PWM, which serves applications that require higher flexibility

Why Use PWM Peripherals?


Although they are designed slightly differently for each architecture, the PWM peripherals available in PIC® and AVR® microcontrollers (MCUs) provide your system with a hardware-driven PWM generator that does not occupy any CPU resources at runtime. They also have built-in hardware to accommodate PWM-related tasks such as dead-band delay and auto shutdown. To generate complex waveform outputs, the PWM peripheral can be used with other on-chip peripherals, such as the Complementary Waveform Generator (CWG), Configurable Custom Logic (CCL) or Configurable Logic Cell (CLC). PWM peripherals can be easily configured with MPLAB® Code Configurator (MCC) for PIC MCUs and Atmel START for AVR MCUs. 

How Are PWM Peripherals in PIC MCUs Used?


PWM Peripherals in PIC MCUs

PIC MCUs offer a variety of stand-alone PWM peripherals ranging from 10–16 bits that are designed to integrate and interface with different peripherals like timers. The standard PWM peripheral generates a PWM signal with a modifiable period and duty cycle duty. A variety of clock sources can be used to operate the PWM, and it can be used in low-power sleep modes when an external clock source is being used.

Capture/Compare PWM Module in PIC MCUs

The Capture/Compare PWM (CCP) module is an extremely versatile peripheral that offers multiple modes of operation. In PWM mode, it can operate as a PWM peripheral, and in capture mode it can measure PWM characteristics such as the edges of waveforms. In compare mode, it can also compare its internal timer values to user-defined values to trigger other peripherals or generate an interrupt.

16-bit PWM with Dual Output in PIC MCUs

Select  8-bit PIC MCUs offer a 16-bit PWM that uses an independent Timer source and can generate various output signals. This PWM peripheral is extremely useful for dynamic high-resolution lighting control and push-pull amplifiers.

r1-200214-PWM-Graphic-Blockdiagram

The PWM module has a compare feature and multiple outputs. The outputs are grouped in slices with each slice having two outputs where both share the same PWM period and operating mode. Different slices can utilize different operating modes. There are six operating modes: Left Aligned, Right Aligned, Center Aligned, Variable Aligned, Pulsed Compare, Toggled Compare. The left and right aligned modes can also be operated in Push-Pull mode.

How Are PWMs in AVR MCUs Used?


Integrated PWM Outputs in AVR Timer/Counters

The PWMs in AVR MCUs are integrated in the Timer/Counter peripherals as a function or mode. Visit the Timer Peripheral page to learn about the functions of each Timer/Counter peripheral in AVR MCUs.

Almost all Timer/Counter peripherals in AVR MCUs provide the function of generating edge-aligned PWMs. This can be referred to as fast PWM or single-slope PWM mode within the Timer/Counter peripheral.  Some Timer peripherals, like Timer/Counter Type A (TCA), have a split mode, which can be used to split a 16-bit PWM into two 8-bit PWMs in case your design needs more PWM outputs.

Some Timer/Counter peripherals, like Timer/Counter Type A or Timer/Counter0, 1, 2… on ATmega devices, can generate center-aligned PWMs. This can be referred to as phase correct PWM or dual-slope PWM mode within the Timer/Counter peripheral.

Getting Started with AVR (10) Updating PWM Duty Cycle Using a Millisecond Timer

In this video, we will learn:
- Setting up a millisecond timer to update the duty cycle
- Using the 8-bit timer-counter 0 as a millisecond timer.
- Incrementing the duty cycle every 2 mS, resetting the duty cycle to 0 when it equals the period.

Follow along with the entire ‘Getting Started with AVR’ series: http://bit.ly/GettingStartedwithAVR

Tutorials and Examples


8-bit Microcontrollers with Pulse Width Modulation


View All Parametrics
Product Stand alone PWM Capture/Compare/PWM (CCP) PWM Max Resolution (bits) Program Memory Size (KB) Data EEPROM (bytes)
ATMEGA1608 9 0 0 16 256
ATMEGA1609 11 0 0 16 256
ATmega164PA 6 1 0 16 512
ATmega168PB 6 1 0 16 512
ATmega169PA 4 1 0 16 512
ATmega16A 4 1 0 16 512
ATmega16U2 4 1 0 16 512
ATmega16U4 8 2 0 16 512
ATmega2561 8 2 0 256 4096
ATMEGA3208 9 0 0 32 256
ATMEGA3209 11 0 0 32 256
ATmega324PA 6 1 0 32 1024
ATmega324PB 10 3 0 32 1024
ATmega328P 6 1 0 32 1024
ATmega328PB 10 3 0 32 1024
ATmega32A 4 1 0 32 1024
ATmega32U2 4 1 0 32 1024
ATmega32U4 8 2 0 32 1024
ATMEGA4808 9 0 0 48 256
ATMEGA4809 11 0 0 48 256
ATmega48A 6 1 0 4 256
ATmega48PA 6 1 0 4 256
ATmega48PB 6 1 0 4 256
ATmega644PA 6 1 0 64 2048
ATmega64A 7 2 0 64 2048
ATMEGA808 9 0 0 8 256
ATMEGA809 11 0 0 8 256
ATtiny102 2 2 0 1 0
ATtiny104 2 2 0 1 0
ATtiny13A 2 0 0 1 64
ATTINY1614 4 0 0 16 256
ATTINY1616 4 0 0 16 256
ATTINY1617 4 0 0 16 256
ATTINY1624 3 0 0 16 256
ATTINY1626 3 0 0 16 256
ATTINY1627 3 0 0 16 256
ATTINY3216 4 0 0 32 256
ATTINY3217 4 0 0 32 256
ATTINY417 8 3 0 4 128
ATtiny44A 4 1 0 4 256
ATtiny48 2 1 0 4 64
ATTINY814 6 0 8 8 128
ATTINY816 6 3 0 8 128
ATTINY817 6 3 0 8 128
PIC12F1571 3 0 16 1.75 0
PIC12F1572 3 0 16 3.5 0
PIC16F15313 4 2 10 3.5 0
PIC16F15323 4 2 10 3.5 0
PIC16F15324 4 2 10 7 0
PIC16F15325 4 2 10 14 0
PIC16F15344 4 2 10 7 0
PIC16F15345 4 2 10 14 0
PIC16F15354 4 2 10 7 0
PIC16F15355 4 2 10 14 0
PIC16F15356 4 2 10 28 0
PIC16F15375 4 2 10 14 0
PIC16F15376 4 2 10 28 0
PIC16F15385 4 2 10 14 0
PIC16F15386 4 2 10 28 0
PIC16F18313 2 2 10 3.5 256
PIC16F18323 2 2 10 3.5 256
PIC16F18324 2 4 10 7 256
PIC16F18325 2 4 10 14 256
PIC16F18326 2 4 10 28 256
PIC16F18344 2 4 10 7 256
PIC16F18345 2 4 10 14 256
PIC16F18854 2 5 10 7 256
PIC16F18855 2 5 10 14 256
PIC16F18856 2 5 10 28 256
PIC16F18857 2 5 10 56 256
PIC16F18875 2 5 10 14 256
PIC16F18876 2 5 10 28 256
PIC16F18877 2 5 10 56 256
PIC18F04Q40 3 3 16 16 512
PIC18F04Q41 3 3 16 16 512
PIC18F05Q40 3 3 16 32 512
PIC18F05Q41 3 3 16 32 512
PIC18F06Q40 3 3 16 64 512
PIC18F06Q41 3 3 16 64 512
PIC18F14Q40 3 3 16 16 512
PIC18F14Q41 3 3 16 16 512
PIC18F15Q40 3 3 16 32 512
PIC18F15Q41 3 3 16 32 512
PIC18F16Q40 3 3 16 64 512
PIC18F16Q41 3 3 16 64 512
PIC18F25Q43 3 3 16 32 1024
PIC18F26Q43 3 3 16 64 1024
PIC18F27Q43 3 3 16 128 1024
PIC18F45Q43 3 3 16 32 1024
PIC18F46Q43 3 3 16 64 1024
PIC18F47Q10 2 2 10 128 1024
PIC18F47Q43 3 3 16 128 1024
PIC18F55Q43 3 3 16 32 1024
PIC18F56Q43 3 3 16 64 1024
PIC18F57Q43 3 3 16 128 1024

Documentation


Application Notes

Title Download
AN564 - Using the PWM Download
Getting started with TCB Download
TCD Tech Brief Download
Getting Started with TCA Download
TB3137 - High-Precision 16-Bit PWM Technical Brief Download