Become a subscriber (Free)

Join 29,000 other subscribers to receive subscriber sale discounts and other free resources.
Don't worry -- youre-mail address is totally secure. I promise to use it only to send you MicroZine.

PIC PWM : Pulse Width Modulation

PIC PWM for the PIC Microcontroller. This page shows you how to setup registers in PIC microcotnrollers to control the PWM module.

Pulse width modulation or PWM is simply the creation of a digital signal that is turned on and off at a repeated rate (specific period) and with a varying mark to space ratio. The most well known use for PWM is to control motors (usually with an H-Bridge driving circuit) but you can also use them to create an analogue signal or control the brightness of LEDs or lamps.

In effect the driven element acts a smoothing circuit evening out the pulse information so that what you are really doing is changing the average power delivered to a load. In the case of a motor the inductance of the motor smooths out the digital signal; for a lamp the same is true. For an analogue output you will need a smoothing circuit (RC - resistor capacitor - circuit with center frequency well below the frequency of the PWM signal). For an LED there's no smoothing but you need the frequency to be above 50Hz so your eye can not detect flicker.

For an RC smoothing circuit attached to a PWM output you can create an analogue output signal. if the center frequency of the RC pair is well below the frequency of the PWM signal than you have a useful analogue output.

Note: Using PWM to generate analogue is not perfect and there will always be some noise fed through due to the PWM frequency but it is low cost (no components) and convenient (microcontrollers have built in PWM modules).

This is interesting because usually you would need a DAC to generate an analogue output and you can even use a PWM signal to re-create an audio output such as reading a WAV file from an SD card. This is super convenient for simple audio output. In fact you can also get away with so called class D amplifier which relies on the smoothing aspect of the inductance in the loudspeaker to re-create audio using only one transistor (or FET)!

The advantages of a DAC are that it will provide higher quality output with no noise component so you would want to use a DAC in a high quality instrument or audio system.

As well as saving the costs of extra chips and interfaces the Pulse Width Modulation signal will not drift over time since it is generated from the time base of the processor i.e. a quartz crystal. Using analogue circuits to generate accurate signals that don't drift is a difficult task so PWM is very effective and cheap.

It works by changing the average voltage level and this is done by generating a constant frequency signal but one where the pulse width is changed (or modulated).

For a moment if you think of the digital signal when it is at its extremes i.e. normal - it generates the maximum of 5V when the output is high and the minimum of 0V when the output is low. If you want to generate a 2.5V signal then you need to make the signal on for half of the time and off for the rest and then take the average.

PIC PWM digital output and average output

Examples of PWM from the PIC Micro

PWM pulse width modulation signal

In the diagram the digital signal (solid line) is at a constant frequency while the pulse width is changed (modulated). The dotted line represents the average signal (if the digital signal is converted to an average). The duty cycle represents the amount of time that the signal is high compared to the amount of time that the signal is low.

PIC PWM Duty Cycle

So the top signal is high for 10% of the period so the average is low, the middle signal is high for 50% of the period so the average is half and the bottom signal is high for 90% of the period so the average is high. For fully off you use 0% and fully on you use 100%.

So the duty cycle is independent of the frequency of the PWM signal and you'll always see the same type of waveform for a specific duty cycle.

PWM Frequency

The frequency of the PWM signal is important depending on the device you are driving. If the aim is to create a dc signal then you would want the frequency high (kHz) so a low pass filter could remove the frequency component. How high depends on how much frequency component is allowed at output and depends on how it is used i.e. what error can be tolerated.

Averaging the PIC PWM output

To convert the PWM signal to a usable analogue signal you need to average it and you can do this by using a resistor capacitor filter (low pass). The higher the PWM frequency the less that frequency will come through the filter so you can design out the PWM frequency from the analogue output.

Note: In some cases a filter is not needed as the filtering is done by the device you are controlling e.g. a motor (is inductive anyway). Or in the case of an RGB ledyour eye averages out the signal (persistence of vision)!

Noise immunity of the PIC PWM signal

Since the PWM signal is fully digital the only way noise can affect it is if the noise is strong enough to change a digital 1 to a digital 0 and vice versa. This immunity is much higher than a purely analogue signal that will be affected by any noise.

For this reason changing an analogue signal into a digital one can improve either the signal transmission distance or its immunity to spurious noise.

For example you could encode an audio signal into PWM, send it over longer cables than a pure analogue signal could travel, and then remove the PWM frequency at the receiver.

Projects using PWM techniques :

Note: that the RGB led link above uses a software PWM method as it needed three PWM outputs and the 16F877 only has 2.

Controlling the PIC PWM output

The PIC microcontrollers are very easy to use with PWM as they have built in PWM generators - all you do is set up the relevant control registers

// Timer 2 PIC PWM
PR2=199; // 4 MHz clock -> 5kHz PWM frequency
T2CON = (1<<TMR2ON);

// Initialize Control PIC PWM
CCPR1L = 30; // Initial Duty
CCP1CON = 0x0f; // PWM mode set and 5,4 duty = 0

...and then control the duty cycle:

CCPR1L = j;

Note that the above control only controls the upper 8 bits of the 10 bit PWM module (the other two bits are in a CCP1CON).

Note the above code is part of the C programming course.

P.S. One PIC PWM 'gotcha' is that the duty cycle for the PIC microcontroller is not the generic duty cycle measured as a percentage rather it is the number of basic oscillator cycles for the output to remain high. So its related only to the master clock not to the period of the PWM signal!

New! Comments

Have your say about what you just read! Leave me a comment in the box below.

Privacy Policy | Contact | About Me

Site Map | Terms of Use

Visit our Facebook Page:

   Click Here

  1. How to use the ADXL345 for movement sensing and more.

    With the ADXL345 acellerometer you can detect up to 16g! You can also find out how to use it for tap detection and more.

    Read more

  2. How the HMC5883L 3-axis digital magentomter works

    HMC5883L - How make a digital compass, Find out the differences between the HMC5883L and the QMC5883L and whether they are compatible.

    Read more

  3. ESP8266 Arduino IDE setup.

    Easily use an ESP8266 with the Arduino IDE and program your first sketch into the ESP8266

    Read more

  4. How to use the MCP4725, a versatile and tiny (SOT-23-6) analogue output device

    The MCP4725 chip is a 12 bit DAC with memory that outputs voltage that you can use for many dfferent purposes. Find out what they are in this page.

    Read more

  5. PCF8591: An 8 bit ADC you can easily connect using I2C

    PCF8591: A four input ADC with single DAC. How good is this 8 bit ADC, and should you use it in your next project?

    Read more

  6. How to use an arduno Nano as an ISP (In System Programmer)

    Arduino Nano ISP: How to program an ATmega328P using an Arduino Nano as the ISP programmmer. One common problem: Programming a sketch into the chip without a reset control - solved here.

    Read more

Readers Comments

"I wanted to thank
you so so so much
for all the information
you have provided in
your site it's


- Ranish Pottath

"This site really is
the best and my favorite.
I find here many useful
projects and tips."

- Milan


"Awesome site,
very, very easy and nice
to navigate!"

- Matt

Learn Microcontrollers

"Interested in

Sign up for The
Free 7 day guide:


"I am a newbie to PIC
and I wanted to say
 how great your
site has been for me."

- Dave


"Your site is a great
and perfect work.

- Suresh


"I couldn't find the correct
words to define
yourweb site.

Very useful, uncovered,
honest and clear.

Thanks so much for
your time and works.

- Anon

Back to Top