{"id":2669,"date":"2014-10-06T10:14:10","date_gmt":"2014-10-06T17:14:10","guid":{"rendered":"https:\/\/blog.digilentinc.com\/?p=2669"},"modified":"2015-08-03T17:41:00","modified_gmt":"2015-08-04T00:41:00","slug":"pulse-width-modulation","status":"publish","type":"post","link":"https:\/\/digilent.com\/blog\/pulse-width-modulation\/","title":{"rendered":"Pulse-Width Modulation"},"content":{"rendered":"<p>Welcome back to the Digilent Blog!<\/p>\n<p>&nbsp;<\/p>\n<p>At some point,\u00a0we have all noticed (whether we chose to admit it or not) that the world we live in is growing more and more digitized. While I am personally not opposed to this (I am writing for Digilent&#8217;s online blog after all), the need to produce analog voltage or current signals will always be present. Thankfully, when using\u00a0a digital source, such as one of Digilent&#8217;s microcontrollers or FPGAs, there are a few ways to produce an analog voltage signal. One of the most fun\u00a0ways (in my opinion) to do this is to use pulse-width modulation (PWM).<\/p>\n<p>&nbsp;<\/p>\n<p>Pulse-width modulation (PWM) is a technique that takes advantage an electronic device&#8217;s capability to rapidly &#8220;pulse&#8221; one of its digital pins between logic high and logic low voltage states. The idea is that\u00a0the switching between the two voltage states in a desired pattern will\u00a0produce an &#8220;average&#8221; voltage somewhere between the high and low voltage inputs. If, within a given period, the pin is at a high voltage level more often than a low one, an overall higher voltage (but less than the full strength input voltage) will be observed.<\/p>\n<p>&nbsp;<\/p>\n<figure id=\"attachment_2671\" aria-describedby=\"caption-attachment-2671\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/High-average-voltage.png\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-2671\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/High-average-voltage.png\" alt=\"An overall high voltage within three \" width=\"600\" height=\"150\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/High-average-voltage.png 800w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/High-average-voltage-600x150.png 600w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/High-average-voltage-225x56.png 225w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-2671\" class=\"wp-caption-text\">A pin that is pulsed high more often results in a higher average voltage.<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p>Conversely, if the pin is at a low voltage level more often, an overall lower voltage (but still greater than the low voltage input) will be observed.<\/p>\n<figure id=\"attachment_2672\" aria-describedby=\"caption-attachment-2672\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/Low-average-voltage.png\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-2672\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/Low-average-voltage.png\" alt=\"A pin that is pulsed low more often results in a lower average voltage\" width=\"600\" height=\"150\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/Low-average-voltage.png 800w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/Low-average-voltage-600x150.png 600w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/Low-average-voltage-225x56.png 225w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-2672\" class=\"wp-caption-text\">A pin that is pulsed low more often results in a lower average voltage.<\/figcaption><\/figure>\n<p>The resulting average voltage can be\u00a0achieved\u00a0through the persistence of vision phenomenon, where our eyes cannot directly see the circuit switching between the high and low voltage states, or it can be accomplished through the use of an appropriate low-pass filter, which takes advantage of a capacitor to &#8220;smooth out&#8221; the pulses into a constant voltage. Because PWM is such an easy way\u00a0to produce a desired average voltage from a digital source, it is commonly used to drive DC motors at different speeds or have LEDs fade in and out.<\/p>\n<p>Digilent chipKIT boards, such as the Uno32 and the uC32, have several digital pins that are capable of performing pulse-width modulation, as indicated by the underline that appears beneath the numbers on the silk screen.<\/p>\n<figure id=\"attachment_2674\" aria-describedby=\"caption-attachment-2674\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/pwm-capable-pins.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-2674\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/pwm-capable-pins.jpg\" alt=\"The underlined pins 3, 5, 6, 9, and 10 are all capable of PWM.\" width=\"600\" height=\"450\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/pwm-capable-pins.jpg 4000w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/pwm-capable-pins-600x450.jpg 600w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/pwm-capable-pins-1024x768.jpg 1024w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/pwm-capable-pins-225x168.jpg 225w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-2674\" class=\"wp-caption-text\">The underlined pins 3, 5, 6, 9, and 10 are all capable of PWM.<\/figcaption><\/figure>\n<p>They are able to produce the PWM\u00a0signal by issuing them an analogWrite() command inside MPIDE with a given value between 0 and 255. The analog write function is technically able to handle values up to 1023, but because the timers within the Uno32 and uC32 are only 8 bits long, this restricts the time frame that the ratio of high to low voltage can be measured in to 256 ticks. \u00a0However, this still allows you to choose a voltage between 0 and 3.3V in approximately 0.013V increments, so for a lot of applications you do not usually need to be more precise than that. To learn more about how the hardware within microcontrollers works with pulse-width modulation, check out this <a title=\"How hardware Implements PWM Learn Module\" href=\"http:\/\/learn.blog.digilentinc.com\/Documents\/381\" target=\"_blank\">Learn module<\/a>!<\/p>\n<div class='watch-action'><div class='watch-position align-left'><div class='action-like'><a class='lbg-style6 like-2669 jlk' data-task='like' data-post_id='2669' data-nonce='ee750c7abc' rel='nofollow'><img src='https:\/\/digilent.com\/blog\/wp-content\/plugins\/wti-like-post-pro\/images\/pixel.gif' title='Like' \/><span class='lc-2669 lc'>0<\/span><\/a><\/div><div class='action-unlike'><a class='unlbg-style6 unlike-2669 jlk' data-task='unlike' data-post_id='2669' data-nonce='ee750c7abc' rel='nofollow'><img src='https:\/\/digilent.com\/blog\/wp-content\/plugins\/wti-like-post-pro\/images\/pixel.gif' title='Unlike' \/><span class='unlc-2669 unlc'>0<\/span><\/a><\/div><\/div> <div class='status-2669 status align-left'>Be the 1st to vote.<\/div><\/div><div class='wti-clear'><\/div>","protected":false},"excerpt":{"rendered":"<p>Pulse-width modulation (PWM) is a technique that takes advantage an electronic device&#8217;s capability to rapidly &#8220;pulse&#8221; one of its digital pins between logic high and logic low voltage states. The idea is that the switching between the two voltage states in a desired pattern will produce an &#8220;average&#8221; voltage somewhere between the high and low voltage inputs. If, within a given period, the pin is at a high voltage level more often than a low one, an overall higher voltage (but less than the full strength input voltage) will be observed.<\/p>\n","protected":false},"author":17,"featured_media":2673,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1563],"tags":[],"ppma_author":[4469],"class_list":["post-2669","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-guide"],"jetpack_featured_media_url":"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/IMG_0439.jpg","authors":[{"term_id":4469,"user_id":17,"is_guest":0,"slug":"jamescolvin","display_name":"James Colvin","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/509febded809c5de0423909f6ae6587f?s=96&d=mm&r=g","author_category":"","user_url":"","last_name":"Colvin","last_name_2":"","first_name":"James","first_name_2":"","job_title":"Applications Engineer \/ Technical Support Engineer \/ Product Support Engineer \/ Technical Writer \/ the person to bother about T&M and JTAG when the senior design engineer is busy","description":"A local Digilent employee who is sometimes tricked into making other content besides documentation and supporting customers on the Digilent Forum, but then I get to write a little more informally so that's a plus. \r\n\r\nA sassy engineer, lover of puns and dad jokes, father and husband. \r\n\r\nI know both way too much and simultaneously almost nothing about a number of nerdy topics. If you want to hear me rant, ask me what data rate USB C operates at."}],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/2669","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/users\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/comments?post=2669"}],"version-history":[{"count":0,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/2669\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/media\/2673"}],"wp:attachment":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/media?parent=2669"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/categories?post=2669"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/tags?post=2669"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=2669"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}