By Robolab Technologies In Robotics 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.