At its core, pulse width modulation (PWM) is just another modulation technique like AM or FM, among others.
Many electronics hobbyists who build robots and other moving things recognize the term as it pertains to controlling the speed of a motor.
But PWM has a lot of other, lesser known uses also.
Let’s first start with some PWM fundamentals.
What is Pulse Width Modulation (PWM)?
We already know that PWM is a modulation technique, hence the “M” in PWM. But what does that mean?
Most of us know that amplitude modulation (AM) and frequency modulation (FM) employ a carrier signal.
In AM, this carrier signal is usually some fixed frequency sinusoid. Information is encoded by varying the amplitude of this sinusoid.
In FM, the carrier signal is again a sinusoid. Only with FM, the information is encoded by varying the frequency of the signal; the amplitude stays constant. When you tune into an FM station, you’re tuning into the carrier frequency while the sounds you hear are encoded by varying this frequency some amount. As an interesting side-note, this is why FM is more immune to noise than AM.
These explanations are a bit simplified, but sufficient for our purpose.
In PWM the carrier signal is a pulse sequence. The pulse width changes in proportion to the “information” we wish to encode. Often, this “information” is the speed of a motor or brightness of an LED. The frequency of the pulses usually stays constant.
Figure 1 illustrates this concept. Notice that the pulses are wider where the amplitude of the sine wave is higher and narrower where the amplitude is lower.
Figure 1: pulse width modulation (PWM).
The Practical Side of PWM
Enough about modulation theory, let’s get a more practical view of what PWM really is.
PWM is a technique which uses digital means to get an analog result.
What’s that mean?
Let’s use the example many of us are familiar with: driving a motor with PWM.
To control the speed of a motor, you could use a potentiometer (which is a variable resistor for those who don’t know). This technique is simple, but wasteful. There are better ways to do this.
A better way to control the speed of a motor is to feed it short on/off pulses. The pulses are really fast, and the motor is relatively slow to respond. Because of this, the motor averages out the ons and offs which effectively restricts the voltage that gets to the motor. This ends up being more efficient.
The longer the duration of the pulses, the faster the motor spins as it’s getting full power for a longer period of time. The shorter the pulses, the slower the motor.
PWM and Duty Cycle
Before we go any further, a quick lesson on duty cycle is in order.
Some of you may have heard this term before and others already know what it means.
Since there are people of all ages and experience levels reading this, let’s quickly delve into the topic.
A waveform’s duty cycle is simply the time it is on compared to the time it is off. It’s a percentage and it describes the amount of time a digital signal is on over a time period.
This period is the inverse of the frequency and usually denoted by a T in science. It gives you the amount of time that one cycle of a waveform lasts. For example, if our frequency is 10 kHz one cycle will last 100 µS.
T = 1/f (eq. 1)
Similarly, if we know the period of a wave and want the frequency, we can simply use equation 2.
f = 1/T (eq. 2)
As we can see, period and frequency are inverses (or reciprocals) of each other.
In the world of digital, on is usually 5 V or 3.3 V and off is usually 0 V or close to it. However, with PWM the on/off voltages can vary depending on the motor. Some motors may run on 12 V, so in this case on would be 12 V.
Feast your eyes on figure 2, which graphically depicts PWM signals with several common duty cycles. Each vertical green line represents one cycle of the waveform, and, if the horizontal axis is time, it represents one period of the wave.
Figure 2: PWM duty cycles.
First, note the two extreme cases: 0% duty cycle and 100% duty cycle.
In this situation the motor would be completely off (0%) or running at full speed (100%). PWM duty cycles do have limitations in the real world. Usually, these are somewhere in between the two extremes.
The number of possible cases in between is referred to as the resolution of the PWM. So, if we have 256 possible pulse widths, our PWM has an 8-bit resolution since 28 = 256.
Let’s take a closer look at some of the more practical PWM duty cycles from figure 2 to drive the point home.
Looking at the 50% duty cycle section of figure 2, it’s easy to see that the waveform is high half the time and low the other half, thus a 50% duty cycle.
Similarly, we can see that the waveform below that one is on 75% of the time and off the other 25%, thus giving us a 75% duty cycle.
The Practical Side of PWM, Part 2
Now that we know what a period and duty cycle are, let’s get back to PWM.
The best way to control the speed of a DC motor is to “chop” the power it uses into different size chunks. We now know that longer pulses result in a faster spin and short pulses slow the motor down.
The time that the pulse (be it long or short) is high is the pulse width and the duty cycle, as we know, is the percentage of the pulse width as part of the PWM signal’s period.
Because PWM turns the coils of the motor it drives on and off, the motor itself will often vibrate at the speed of the pulse width modulation. This sometimes produces an audible hum.
This suggests that PWM can also create sounds. And indeed, it can.
Whether or not you want to make sound with PWM will depend on your application. But let’s take a closer look at why this is true, without getting too deep into complicated math.
If you were to feed an ideal PWM signal into a spectrum analyzer (a device that breaks a signal down into its frequency components) you’d notice that it contains 3 parts:
- A DC component with an amplitude directly proportional to the duty cycle
- A sinusoid at the fundamental frequency (remember, f = 1/T)
- And an infinite number of harmonics whose frequency is a multiple of the fundamental (i.e. 2f, 3f, 4f and so on)
Other Uses of PWM
Those familiar with the Arduino Uno know that certain pins (the ones with a tilde or ~ next to them) are PWM capable. This is no surprise given that the analogWrite() function uses PWM. In fact, many modern day microcontrollers have PWM capability built into them.
The “Fade” sketch comes along with the Arduino IDE and fades an LED on and off. This fading is done using PWM. One can also use PWM to control the color of RGB LEDs by varying the duty cycle of each of the three primary colors thus controlling the intensity of each of the three colors. Different intensities of each of three colors can render just about any color you want. Figure 3 shows how this is possible.
Figure 3: any color can be made by mixing the primary colors at different intensities.
PWM signals are also extensively used in control systems for various motors, valves, heaters and more.
Today, most microcontrollers can accomplish pulse width modulation either in software, hardware, or a combination or both. However, there are other ways to do PWM. Some are as simple as using a 555 timer. This does have some limitations (can’t go lower than 50% duty cycle) but depending on your requirements you may be able to spare a pin or two on your micro and use a cheap 555 chip. For more about 555 timers see An Introduction to 555 Timers. And there are other methods of generating PWM signals.
Hopefully you now have a better understanding of PWM and how it works if you didn’t before.
Drop us a comment and tell us what you use PWM for. Is it robotics? If so, tell us about your latest creation!