{"id":2201,"date":"2018-11-09T18:02:03","date_gmt":"2018-11-09T23:02:03","guid":{"rendered":"http:\/\/www.circuitcrush.com\/?p=2201"},"modified":"2021-06-30T16:43:08","modified_gmt":"2021-06-30T20:43:08","slug":"pwm-tutorial","status":"publish","type":"post","link":"https:\/\/www.circuitcrush.com\/pwm-tutorial\/","title":{"rendered":"Pulse Width Modulation (PWM) Tutorial"},"content":{"rendered":"<p>At its core, pulse width modulation (PWM) is just another modulation technique like AM or FM, among others.<\/p>\n<p>Many electronics hobbyists who build robots and other moving things recognize the term as it pertains to controlling the speed of a motor.<\/p>\n<p>But PWM has a lot of other, lesser known uses also.<\/p>\n<p>Let\u2019s first start with some PWM fundamentals.<\/p>\n<h1><strong>What is Pulse Width Modulation (PWM)?<br \/>\n<\/strong><\/h1>\n<p><!--more--><\/p>\n<p>We already know that PWM is a modulation technique, hence the \u201cM\u201d in PWM. But what does that mean?<\/p>\n<p>Most of us know that amplitude modulation (AM) and frequency modulation (FM) employ a carrier signal.<\/p>\n<p>In AM, this carrier signal is usually some fixed frequency sinusoid. Information is encoded by varying the amplitude of this sinusoid.<\/p>\n<p>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\u2019re 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.<\/p>\n<p>These explanations are a bit simplified, but sufficient for our purpose.<\/p>\n<p>In PWM the carrier signal is a pulse sequence. The pulse width changes in proportion to the \u201cinformation\u201d we wish to encode. Often, this \u201cinformation\u201d is the speed of a motor or brightness of an LED. The frequency of the pulses usually stays constant.<\/p>\n<p>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.<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"alignnone size-full wp-image-2202\" src=\"http:\/\/www.circuitcrush.com\/wp-content\/uploads\/PWM.jpg\" alt=\"PWM Pulse Width Modulation\" width=\"551\" height=\"391\" srcset=\"https:\/\/www.circuitcrush.com\/wp-content\/uploads\/PWM.jpg 551w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/PWM-150x106.jpg 150w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/PWM-300x213.jpg 300w\" sizes=\"(max-width: 551px) 100vw, 551px\" \/><\/p>\n<p><strong><em>Figure 1: pulse width modulation (PWM).<\/em><\/strong><\/p>\n<p>&nbsp;<\/p>\n<h1><strong>The Practical Side of PWM<br \/>\n<\/strong><\/h1>\n<p>Enough about modulation theory, let\u2019s get a more practical view of what PWM really is.<\/p>\n<p>PWM is a technique which uses digital means to get an analog result.<\/p>\n<p>What\u2019s that mean?<\/p>\n<p>Let\u2019s use the example many of us are familiar with: driving a motor with PWM.<\/p>\n<p>To control the speed of a motor, you could use a potentiometer (which is a variable resistor for those who don\u2019t know). This technique is simple, but wasteful. There are better ways to do this.<\/p>\n<p>Enter PWM.<\/p>\n<h3 style=\"text-align: center;\">Become the Maker you were born to be. Try <a href=\"https:\/\/learnarduinonow.com\">Arduino Academy<\/a> for FREE!<\/h3>\n<p><img decoding=\"async\" class=\"aligncenter  wp-image-4238\" src=\"https:\/\/www.circuitcrush.com\/wp-content\/uploads\/FB_Cover2.png\" alt=\"\" width=\"431\" height=\"164\" srcset=\"https:\/\/www.circuitcrush.com\/wp-content\/uploads\/FB_Cover2.png 828w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/FB_Cover2-300x114.png 300w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/FB_Cover2-150x57.png 150w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/FB_Cover2-768x292.png 768w\" sizes=\"(max-width: 431px) 100vw, 431px\" \/><\/p>\n<p>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.<\/p>\n<p>The longer the duration of the pulses, the faster the motor spins as it\u2019s getting full power for a longer period of time. The shorter the pulses, the slower the motor.<\/p>\n<h2><strong>PWM and Duty Cycle<br \/>\n<\/strong><\/h2>\n<p>Before we go any further, a quick lesson on duty cycle is in order.<\/p>\n<p>Some of you may have heard this term before and others already know what it means.<\/p>\n<p>Since there are people of all ages and experience levels reading this, let\u2019s quickly delve into the topic.<\/p>\n<p>A waveform\u2019s duty cycle is simply the time it is on compared to the time it is off. It\u2019s a percentage and it describes the amount of time a digital signal is on over a time <em>period<\/em>.<\/p>\n<p>This <em>period<\/em> is the inverse of the frequency and usually denoted by a <em>T<\/em> 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 \u00b5S.<\/p>\n<p><em>T<\/em> = 1\/f\u00a0 (eq. 1)<\/p>\n<p>Similarly, if we know the period of a wave and want the frequency, we can simply use equation 2.<\/p>\n<p>f = 1\/<em>T<\/em>\u00a0 (eq. 2)<\/p>\n<p>As we can see, period and frequency are inverses (or reciprocals) of each other.<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-2203\" src=\"http:\/\/www.circuitcrush.com\/wp-content\/uploads\/PWM-and-Duty-Cycle.jpg\" alt=\"PWM-and-Duty-Cycle\" width=\"400\" height=\"438\" srcset=\"https:\/\/www.circuitcrush.com\/wp-content\/uploads\/PWM-and-Duty-Cycle.jpg 400w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/PWM-and-Duty-Cycle-137x150.jpg 137w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/PWM-and-Duty-Cycle-274x300.jpg 274w\" sizes=\"(max-width: 400px) 100vw, 400px\" \/><\/p>\n<p><strong><em>Figure 2: PWM duty cycles.<\/em><\/strong><\/p>\n<p>First, note the two extreme cases: 0% duty cycle and 100% duty cycle.<\/p>\n<p>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.<\/p>\n<p>The number of possible cases in between is referred to as the <em>resolution<\/em> of the PWM. So, if we have 256 possible pulse widths, our PWM has an 8-bit resolution since 2<sup>8<\/sup> = 256.<\/p>\n<p>Let\u2019s take a closer look at some of the more practical PWM duty cycles from figure 2 to drive the point home.<\/p>\n<p>Looking at the 50% duty cycle section of figure 2, it\u2019s easy to see that the waveform is high half the time and low the other half, thus a 50% duty cycle.<\/p>\n<p>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.<\/p>\n<h1><strong>The Practical Side of PWM, Part 2<br \/>\n<\/strong><\/h1>\n<p>Now that we know what a period and duty cycle are, let\u2019s get back to PWM.<\/p>\n<p>The best way to control the speed of a DC motor is to \u201cchop\u201d 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.<\/p>\n<p>The time that the pulse (be it long or short) is high is the <em>pulse width<\/em> and the duty cycle, as we know, is the percentage of the pulse width as part of the PWM signal\u2019s period.<\/p>\n<p>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.<\/p>\n<p>This suggests that PWM can also create sounds. And indeed, it can.<\/p>\n<p>Whether or not you want to make sound with PWM will depend on your application. But let\u2019s take a closer look at why this is true, without getting too deep into complicated math.<\/p>\n<p>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\u2019d notice that it contains 3 parts:<\/p>\n<ul>\n<li>A DC component with an amplitude directly proportional to the duty cycle<\/li>\n<li>A sinusoid at the fundamental frequency (remember, f = 1\/<em>T<\/em>)<\/li>\n<li>And an infinite number of harmonics whose frequency is a multiple of the fundamental (i.e. 2f, 3f, 4f and so on)<\/li>\n<\/ul>\n<h2><strong>Other Uses of PWM <\/strong><\/h2>\n<p>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.<\/p>\n<p>The \u201cFade\u201d 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.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-2204\" src=\"http:\/\/www.circuitcrush.com\/wp-content\/uploads\/RGB-and-PWM.jpg\" alt=\"RBG LED and PWM\" width=\"310\" height=\"310\" srcset=\"https:\/\/www.circuitcrush.com\/wp-content\/uploads\/RGB-and-PWM.jpg 600w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/RGB-and-PWM-300x300.jpg 300w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/RGB-and-PWM-100x100.jpg 100w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/RGB-and-PWM-150x150.jpg 150w\" sizes=\"(max-width: 310px) 100vw, 310px\" \/><\/p>\n<p><strong><em>Figure 3: any color can be made by mixing the primary colors at different intensities.<\/em><\/strong><\/p>\n<p>PWM signals are also extensively used in control systems for various motors, valves, heaters and more.<\/p>\n<p>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\u2019t 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 <a href=\"http:\/\/www.circuitcrush.com\/555-timers-introduction\/\" target=\"_blank\" rel=\"noopener\">An Introduction to 555 Timers<\/a>. And there are other methods of generating PWM signals.<\/p>\n<p>Hopefully you now have a better understanding of PWM and how it works if you didn\u2019t before.<\/p>\n<p>Drop us a comment and tell us what you use PWM for. Is it robotics? If so, tell us about your latest creation!<\/p>\n<h2 style=\"text-align: center;\">Become the Maker you were born to be. Try <a href=\"https:\/\/learnarduinonow.com\">Arduino Academy<\/a> for FREE!<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4238\" src=\"https:\/\/www.circuitcrush.com\/wp-content\/uploads\/FB_Cover2.png\" alt=\"\" width=\"828\" height=\"315\" srcset=\"https:\/\/www.circuitcrush.com\/wp-content\/uploads\/FB_Cover2.png 828w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/FB_Cover2-300x114.png 300w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/FB_Cover2-150x57.png 150w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/FB_Cover2-768x292.png 768w\" sizes=\"(max-width: 828px) 100vw, 828px\" \/><\/p>\n<a target=\"_blank\" href=\"https:\/\/www.drpeterscode.com\/index.php\"><img src=\"https:\/\/www.circuitcrush.com\/wp-content\/plugins\/dpabottomofpostpage\/apixel1x1.jpg\" ><\/a><table><\/table>","protected":false},"excerpt":{"rendered":"<p>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\u2019s first start [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2207,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[94,9],"tags":[124,123],"class_list":{"0":"post-2201","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-digital-electronics","8":"category-electronics","9":"tag-pulse-width-modulation","10":"tag-pwm","11":"entry"},"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/www.circuitcrush.com\/wp-content\/uploads\/PWM-Pulse-Width-Modulation.jpg","_links":{"self":[{"href":"https:\/\/www.circuitcrush.com\/wp-json\/wp\/v2\/posts\/2201","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.circuitcrush.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.circuitcrush.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.circuitcrush.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.circuitcrush.com\/wp-json\/wp\/v2\/comments?post=2201"}],"version-history":[{"count":4,"href":"https:\/\/www.circuitcrush.com\/wp-json\/wp\/v2\/posts\/2201\/revisions"}],"predecessor-version":[{"id":4286,"href":"https:\/\/www.circuitcrush.com\/wp-json\/wp\/v2\/posts\/2201\/revisions\/4286"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.circuitcrush.com\/wp-json\/wp\/v2\/media\/2207"}],"wp:attachment":[{"href":"https:\/\/www.circuitcrush.com\/wp-json\/wp\/v2\/media?parent=2201"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.circuitcrush.com\/wp-json\/wp\/v2\/categories?post=2201"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.circuitcrush.com\/wp-json\/wp\/v2\/tags?post=2201"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}