About Me

Pilani, Rajasthan, India
I am an engineering student currently pursuing an undergraduate degree in Electronics & Instrumentation from BITS Pilani, Pilani campus. My hobbies are reading novels- fiction and non-fiction alike, playing and watching football, dabbling with new software and going through blogs. I love reading Electronics For You. It has helped me a lot in my college life. And sometimes, people around me.

Hope you find this blog useful. Thank you.

Tuesday, August 30, 2011

Introduction to Pulse Width Modulation

Hi all. It’s been a while since the last post. But, it has been an eventful period all the same with placements going on in BITS Pilani.
Anyway, in this post, I shall be giving a basic introduction to a concept called Pulse Width Modulation, its need and applications and how it can be implemented on an ATmega32/16.

Pulse Width Modulation (PWM) – Basics

In layman language, PWM is a technique in which you vary the ratio of HIGH time to LOW time of a pulse. One of the applications is simulation of an analogue signal using digital TTL signals. What if you want to supply 2.5 Volts to a component? You can use a voltage divider circuit using R1=R2 and then supply the output across any one of these resistors to your component of interest. Simple enough? But what you are overlooking is the fact that this circuit is highly inefficient as there is always going to be power dissipation across the resistors. Also, this configuration is highly susceptible to errors because a slight change in resistance owing to wear and tear or long use is bound to change your input voltage.
 In PWM, you are actually fooling your component by switching between 5 Volts and 0 Volts so fast that it is not able to distinguish between the 5 Volts and 0 Volts and ends up seeing the average of these two signals i.e. 2.5 Volts. A corollary you can draw from this is that by simply adjusting the ratio of these two signals in a given time period, you can supply a variety of ‘analogue-like’ signals. For instance, in a PWM signal with a time period of 1 second, if I am supplying 5 Volts for 0.7 seconds and 0 Volts for the rest, what analogue voltage would my component see? That’s right – 3.5 Volts!

Why do I need to know PWM technique?

The world is analogue. Yes, there are desktops and laptops and mobiles and tablets and what not? All of these are digital in nature. But suppose you want to adjust the speed of your DC motor or you want a system in which the strength of the LED glow is proportional to some system parameter or you want to build a simple all-digital temperature control system? There are many applications in which PWM is helpful for the simple reason – It is energy-efficient as well as accurate, easy to implement and being digital in nature, it is not easily affected by noise.

Basically, in any digital system where you are varying a system parameter over a wide range, there is a high probability that PWM is involved; such is the popularity of this technique.

PWM using ATmega32/16

In any microcontroller, PWM is implemented by using the timers/counters available. The timers/counters have a PWM mode in which the user is supposed to let the controller know the desired time period and also the value of the counter at which the output has to go HIGH/LOW depending on the type of PWM.
For instance, suppose I want a PWM signal of 2.5 Volts. What I will do is I will tell the counter that I want a prescaler of 64 (say) on my clock of 1MHz (say). Also, I will tell it that it should set the output low whenever the counter reaches the value 127 (referred to as 'OCR' value). So, on its way to 255 (referred to here as the 'TOP'), whenever the counter encounters the value 127, the output goes HIGH/LOW. The following diagram will throw more light on the working of PWM in microcontrollers.

Now we will see how to implement this using AVR Studio. Please have your datasheet with you.J 

PWM implementation in AVR Studio
 There are various types of PWM:

1)      Fast PWM – In this type of PWM, in a single time period, a PWM signal will have first the HIGH signal and then the LOW signal.
2)      Phase-correct PWM – In this type of PWM, in a single time period, a PWM signal will be symmetric about the centre i.e. if plotted against time, the signal to the left and right of the centre of the signal will be mirror images.

3)      Phase and frequency correct PWM – In this type of PWM, you can alter the frequency of your signal. In the other types, your frequency remains fixed – only the duty cycle can be altered.

In this post, I will be covering only Fast PWM as it is the simplest to understand for beginners. Other modes will be covered in a future post. Watch out J

So let’s get started!

Registers and Terms involved in Fast PWM implementation

First and foremost, in any kind of PWM implementation, you will come across the following terms:
a)      MAX – This is the maximum value upto which the counter can count. For an 8-bit counter, MAX = 0XFF (255).
b)      BOTTOM – This is the value from where the counter starts counting at the start of every period. For an 8-bit counter, BOTTOM = 0 (0X00).
c)      TOP – This is the value with which the count is compared to decide when to switch the output to HIGH/LOW. (Please note, in the diagram above, TOP and MAX were used interchangeably. This is not always the case. Henceforth, we won't use the two terms interchangeably.)
d)     Duty cycle – This is the ratio of the on-time in a given period to the length of the entire time-period of the PWM signal.

We will be working with the 8-bit timer/counter to keep things simple enough. The registers involved in Fast PWM implementation are the same as the ones I mentioned in the post on Timers. You can refer to the description of the registers in that post for a better understanding. A brief recapitulation follows:

1)      TCCR0 – Manipulating WGM bits in this register help select the desired PWM mode and manipulating the CS bits help select the desired prescaler for the clock generation. Please refer to the datasheet for the required manipulations.
2)      TCNT0 – This 8-bit register stores the current value of the counter.
3)      OCR0 – This register stores the TOP value.
4)      TIFR – This register has a bit called OCF0 which goes HIGH whenever TCNT0 = OCR0 i.e. whenever the output switches from HIGH to LOW or vice versa (depending on the configuration selected). You need to write a ‘1’ to this bit to clear it. (Peculiar right?)

Following is the code for the generation of a 40% duty cycle Fast PWM signal with a clock frequency of 1 MHz prescaled by 64. The code is fully commented, but please make sure you have the datasheet with you to understand the various bit manipulations.

#include <avr/io.h>

int main(void)
        TCCR0 |= (1<<WGM01)|(1<<WGM00);//selecting Fast PWM mode
TCCR0 |= (1<<COM01)|(0<<COM00); // selecting non-//inverting mode of PWM operation
// In non-inverting mode of operation, the HIGH portion of the //signal comes first during a given time period
TCCR0 |= (1<<CS01)|(1<<CS00); // selecting a pre-scaler //of 64
OCR0 = 101; // this is the TOP value; for 40% duty cycle, //TOP = (0.4*256)-1
// This code will generate a square wave having 40% duty cycle on the pin OC0 i.e. PB3 

Some more information

One thing that would have become very clear is that the frequency of our PWM signal is different from the clock signal. There is a formula to evaluate your signal frequency. The formula (for an 8-bit counter) is given by:

256 is replaced by 65536 for a 16-bit counter.

To summarize, in Fast PWM mode:

1)      We can change the PWM frequency (also called modulation frequency) by changing the system clock settings and the prescaler value.
2)      We can change the PWM duty cycle by altering the TOP value which is stored in the register OCR0.


This post would not have been possible without the following treasures of information.


The other types of PWM and their implementation will be covered in a later post.
Hope this was helpful J