A digital device like a microcontroller can easily work with inputs and outputs
that has only two state, on and off. So you can easily use it to control a LED’s
state i.e. on or off. In the same way you can use it to control any electrical
device on/off by using proper drivers (transistor,triac, relays etc). But sometimes
you need more than just "on" & "off " control over the
device. Like if you wanna control the brightness of a LED (or any lamp) or the
speed of DC motor then digital (on/off) signals simply can’t do it. This situation
is very smartly handled by a technique called PWM or Pulse Width Modulation.

PWM is the technique used to generate analogue signals from a digital device
like a MCU. Almost all modern MCUs have dedicated hardware for PWM signal generation.
In this tutorial we will learn the basics of PWM technique and later on we will
see how to implement PWM generation with AVR microcontrollers.

PWM : Pulse Width Modulation

A digital device, like a microcontroller can only generate two levels on its
output lines, HIGH=5v and LOW=0V. But what if we want to generate 2.5v or 3.1v
or any voltage between 0-5 volt output ? For these kinds of requirement, instead
of generating a constant DC voltage output we generate a square wave, which
has high = 5V and Low = 0v (See figure 1).

A PWM signal signal

Fig. 1 – A PWM Waveform.

 

 

In the figure you can see a PWM signal. As you can see it is just a digital
signal (can easily be generated by MCUs). But let me clarify some of its properties.

  • The signal remains "ON" for some time and "OFF" for
    some time.
  • Ton = Time the output remains high.
  • Toff = Time the output remains Low.
  • When output is high the voltage is 5v’
  • When output is low the voltage is 0v
  • T = Time Period = Ton + Toff

Duty Cycle.

It is defined by

So you can see it is just the percentage of the total time the output was high.
In the above figure (Fig. 1) you can see that Ton = Toff = Half of the time
period. So the duty cycle is 50%. If the frequency of such wave is sufficiently
high (say 500 Hz) the output you get is half of 5v i.e. 2.5 volts. Thus if this
output is connected to a motor(by means of suitable drivers) it will run at
50% of its full speed at 5v. The PWM technique utilizes this fact to generate
any voltage between two extremes (for example between 0-12volts). The trick
is to vary the duty cycle between 0-100% and get same percentage of input voltage
to output. Below are some examples of PWM signals of different duty cycle.

A PWM signal signal with duty  cycle = 12.5 %

Fig. 2- A PWM Waveform. Duty Cycle = 12.5% Analog Voltage Out = 12.5%
of Vcc (5v) = 0.625 Volts

 

 

 

A PWM signal signal with duty  cycle = 75 %

Fig. 3- A PWM Waveform. Duty Cycle = 75% Analog Voltage Out = 75%
of Vcc (5v) = 3.75 Volts

 

 

So you just saw how we can generate analog voltage levels from a digital system
using PWM technique. If the output is provided with an R/C filter you will get
a pure DC signals levels instead of square waves. But this isn’t required for
driving motors or controlling LED’s brightness. You can feed the PWM signals
directly to then (by directly I mean without the R/C filter and not the drivers
(like transistor etc)).

In the next tutorial we will see how to actually generate PWM signals
from an AVR MCU
. Till then good bye. But don’t forget to leave your
comment ! May be you have some doubts, request or any feedback, feel free to
say !