Pulse-width modulation (PWM), or pulse-duration modulation (PDM), is a commonly used technique for controlling power to inertial electrical devices, made practical by modern electronic power switches.
The average value of voltage (and current) fed to the load is controlled by turning the switch between supply and load on and off at a fast pace. The longer the switch is on compared to the off periods, the higher the power supplied to the load is.
The PWM switching frequency has to be much faster than what would affect the load, which is to say the device that uses the power. Typically switchings have to be done several times a minute in an electric stove, 120 Hz in a lamp dimmer, from few kilohertz (kHz) to tens of kHz for a motor drive and well into the tens or hundreds of kHz in audio amplifiers and computer power supplies.
The term duty cycle describes the proportion of 'on' time to the regular interval or 'period' of time; a low duty cycle corresponds to low power, because the power is off for most of the time. Duty cycle is expressed in percent, 100% being fully on.
The main advantage of PWM is that power loss in the switching devices is very low. When a switch is off there is practically no current, and when it is on, there is almost no voltage drop across the switch. Power loss, being the product of voltage and current, is thus in both cases close to zero. PWM also works well with digital controls, which, because of their on/off nature, can easily set the needed duty cycle.
PWM has also been used in certain communication systems where its duty cycle has been used to convey information over a communications channel.
In telecommunications, the widths of the pulses correspond to specific data values encoded at one end and decoded at the other.
Pulses of various lengths (the information itself) will be sent at regular intervals (the carrier frequency of the modulation).
_ _ _ _ _ _ _ _ | | | | | | | | | | | | | | | | Clock | | | | | | | | | | | | | | | | __| |____| |____| |____| |____| |____| |____| |____| |____
_ __ ____ ____ _ PWM Signal | | | | | | | | | | | | | | | | | | | | _________| |____| |___| |________| |_| |___________
Data 0 1 2 4 0 4 1 0
The inclusion of a clock signal is not necessary, as the leading edge of the data signal can be used as the clock if a small offset is added to the data value in order to avoid a data value with a zero length pulse.
_ __ ___ _____ _ _____ __ _ | | | | | | | | | | | | | | | | PWM Signal | | | | | | | | | | | | | | | | __| |____| |___| |__| |_| |____| |_| |___| |_____
Data 0 1 2 4 0 4 1 0
[edit]Sample C Code to Generate PWM
Code:
void delay_ms(unsigned int i)
{
unsigned int j;
while(i-->0)
{
for(j=0;j<500 font="font" j="j">500>
{
;
}
}
}
void delay_micro(unsigned int i)
{
unsigned int s;
for(s=0;s
{
;
}
}
void main()
{
unsigned int i;
while(1)
{
for(i=0;i<200 font="font" i="i">200>
{
led=0;
delay_micro(i);
led=1;
delay_ms(20);
}
}
}