EE 109 Unit 10 - Pulse Width Modulation
EE 109 Unit 10 - Pulse Width Modulation
Power
• Recall (or learn) that Power is a measure of:
– Energy per unit time
• In an electronic circuit, P = I * V
– Power = Current & Voltage (each may be varying w/ time)
• A circuit that draws a constant 2 mA of current at a constant 5V would
consume 10 mW
• Since voltage and current may change rapidly, it is often helpful to
calculate the average power
• Just sum the total power and divide by the total time
1s 1s
5V Average Power
.5s .3s
I = 1A = (1*5*.8)/2 = 2W
0V
3
Output Devices
• What do the following have in common?
– Servo motor that can rotate to any angle w/in 180 degrees
– Light dimmer
– Oven or microwave with various power levels
• They are controlled by Pulse Width Modulation (PWM)
– Usually a 3-pin interface: Power (Vcc), GND, PWM Signal
4
Duty Cycle
• A pulse is just a short window of time when a signal is
'on'
• We could repeat the pulse at some regular period, T
• We define the duty cycle as
Duty Cycle % = (ON Time / T) * 100
T T T T
5V
Duty Cycle T/2
= 50%
0V
T
5V
Duty Cycle
= 25%
0V
T/4
5
+ VLED -
Vs
fast rate
– Fast so that the human eye can't
detect it flashing
– Average power would be ½ the T T
original always 'on' power
5V
– Result would be a 'dimmer' LED T/2
glow
0V
PORTxx 'on' 50% of time
6
In-Class Activity
• Write a program with a loop that turns on the LED
(PORT B5) for x milliseconds and then turns it off for
100-x milliseconds
– Initially set x = 100
– Now set x = 50
– Now set x = 20
– Now set x = 10
– Now set x = 2
• Notice result may be non-linear
• A similar tactic is used in your microwave oven when
you want to cook something at 80%, 70%, etc. power.
7
PWM
• Modulation refers to changing a value based on some signal
(i.e. changing one signal based on another)
• Pulse width modulation refers to modifying the width of a
pulse based on another signal
• It can be used to transform one signal into another
– Example below of
sine wave represented
as pulses w/ different
widths
• Or it can just be used
to alter average power
as in the last activity
8
Simple Digital-To-Analog
• Connecting a PWM output to a resistor-capacitor circuit
as shown causes the voltage at Vc to "integrate" the
digital PWM signal (charge the capacitor)
– Analogy: Imagine you have a leaky bucket (i.e. capacitor) and
you want to produce a variable level (i.e. analog voltage, Vc)
of water by only turning the hose (digital output) on or off
5V
VPWM
0V
5V
Vc
0V
9
Servo Motors
• Many embedded systems use servo
motors to move or rotate mechanical
devices
• Most servo motors use some form of
pulse width modulation to control the
direction and speed of their rotation
• 2 Kinds
– Standard servo motors: can only rotate
through a certain arc (usually 180 degrees)
– Continuous: can keep spinning round and
round while pulses are provided
10
20 ms 20 ms 20 ms
Implementing PWM
• Can use delays or timers to make your own pulse
signals
• Most microcontrollers have hardware to
automatically generate PWM signals based on the
contents of some control registers
• Many microcontrollers use the Timers to also serve
as PWM signals
– Recall the timer module gave us a counter that would
increment until it hit some 'modulus' (MAX) count which
would cause it to restart and also generate an interrupt
13
OCRxA
OCRxB
0 0
Exercise
• Try to use PWM to make your LED glow at
various brightness levels similar to what you
did earlier with normal digital I/O