{"id":6033,"date":"2012-08-24T22:43:16","date_gmt":"2012-08-25T02:43:16","guid":{"rendered":"http:\/\/embedded-lab.com\/blog\/?p=6033"},"modified":"2013-04-23T21:36:04","modified_gmt":"2013-04-24T01:36:04","slug":"chipkit-tutorial-5-pulse-width-modulation-pwm","status":"publish","type":"post","link":"https:\/\/embedded-lab.com\/blog\/chipkit-tutorial-5-pulse-width-modulation-pwm\/","title":{"rendered":"chipKIT Tutorial 5: Pulse width modulation (PWM)"},"content":{"rendered":"<p>Pulse width modulation (PWM) is a technique of controlling the amount of power delivered to an electronic load using an on-off digital signal. The key idea behind this technique is that the average DC value of the digital signal, and hence the power delivered to the load, can be varied by varying the duty cycle of the signal. This method is commonly used for controlling speeds of DC motors and brightness of lamps. The switching mode power supplies are also based on the PWM technique. In this tutorial, we will discuss about the PWM pins of the chipKIT Uno32 board and illustrate the concept by controlling the brightness of two external LEDs.<\/p>\n<div id=\"attachment_6037\" style=\"width: 575px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/08\/T5_Title.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6037\" class=\"size-full wp-image-6037\" title=\"T5_Title\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/08\/T5_Title.jpg\" alt=\"\" width=\"565\" height=\"409\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/08\/T5_Title.jpg 565w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/08\/T5_Title-550x398.jpg 550w\" sizes=\"auto, (max-width: 565px) 100vw, 565px\" \/><\/a><p id=\"caption-attachment-6037\" class=\"wp-caption-text\">PWM<\/p><\/div>\n<p><!--more--><\/p>\n<p><strong>Theory<\/strong><\/p>\n<p>Pulse-width modulation (PWM)\u00a0is a way of delivering energy through a\u00a0succession of pulses rather than a continuously varying signal. By increasing or decreasing the pulse\u00a0width (while frequency remaining unchanged), it is possible to control the output power. The fraction of the period for which the signal is on is known as the duty cycle. The average DC value of the signal can be varied by varying the duty cycle. The duty cycle can be anywhere between 0 (signal is always off) to 1 (signal is constantly on). Suppose, if the signal has +5 V while it is on and 0 V during off condition, then by changing the duty cycle of the signal, any voltage between 0-5 V can be simulated. This method is commonly used for controlling speeds of DC motors and brightness of lamps. The principle of PWM is illustrated in the picture below.<\/p>\n<div id=\"attachment_6040\" style=\"width: 526px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/08\/PWM.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6040\" class=\"size-full wp-image-6040\" title=\"PWM\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/08\/PWM.png\" alt=\"\" width=\"516\" height=\"490\" \/><\/a><p id=\"caption-attachment-6040\" class=\"wp-caption-text\">PWM<\/p><\/div>\n<p><strong>Circuit setup<\/strong><\/p>\n<p>On chipKIT Uno32 board, the I\/O pins 3, 5, 6, 9, and 10 can be configured to provide PWM outputs. We will connect two LEDs (red and green) to PWM pins 3 and 5 and control their brightness by varying the duty cycles of the PWM signals driving those LEDs. Two 100? resistors are used to limit the current through the LEDs.<\/p>\n<div id=\"attachment_6034\" style=\"width: 575px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/08\/T5_Circuit.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6034\" class=\"size-full wp-image-6034\" title=\"T5_Circuit\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/08\/T5_Circuit.png\" alt=\"\" width=\"565\" height=\"460\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/08\/T5_Circuit.png 565w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/08\/T5_Circuit-550x447.png 550w\" sizes=\"auto, (max-width: 565px) 100vw, 565px\" \/><\/a><p id=\"caption-attachment-6034\" class=\"wp-caption-text\">Circuit setup<\/p><\/div>\n<div id=\"attachment_6036\" style=\"width: 575px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/08\/T5_Setup.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6036\" class=\"size-full wp-image-6036\" title=\"T5_Setup\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/08\/T5_Setup.jpg\" alt=\"\" width=\"565\" height=\"460\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/08\/T5_Setup.jpg 565w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/08\/T5_Setup-550x447.jpg 550w\" sizes=\"auto, (max-width: 565px) 100vw, 565px\" \/><\/a><p id=\"caption-attachment-6036\" class=\"wp-caption-text\">Actual setup on breadboard<\/p><\/div>\n<p><strong>Writing sketch<\/strong><\/p>\n<p>PWM signals in an Arduino platform can be generated using <em>analogWrite()<\/em> command. Inside the parenthesis, you specify the pin number and the desired duty cycle (0-255). Duty cycle of &#8216;0&#8217; corresponds to always off and &#8216;255&#8217; corresponds to always on. When the <em>analogWrite()<\/em> is called, a steady square wave with the specified duty cycle is generated at the specified PWM pin. The following sketch controls the output power delivered to the two LEDs using varying duty cycle PWM signals. The two LEDs fade in and out alternately, which means when one is at peak intensity, other is dimmed low, and vice-versa.<\/p>\n\n<div class=\"wp_syntax\"><table><tr><td class=\"code\"><pre class=\"c\" style=\"font-family:monospace;\"><span style=\"color: #993333;\">int<\/span> redLED   <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">3<\/span><span style=\"color: #339933;\">;<\/span>     <span style=\"color: #666666; font-style: italic;\">\/\/ Red LED is connected to pin 3<\/span>\n<span style=\"color: #993333;\">int<\/span> greenLED <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">5<\/span><span style=\"color: #339933;\">;<\/span>     <span style=\"color: #666666; font-style: italic;\">\/\/ Green LED is connected to pin 5<\/span>\n<span style=\"color: #993333;\">int<\/span> redCount   <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">255<\/span><span style=\"color: #339933;\">;<\/span> <span style=\"color: #666666; font-style: italic;\">\/\/ Initial PWM values, RED is full<\/span>\n<span style=\"color: #993333;\">int<\/span> greenCount <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">1<\/span><span style=\"color: #339933;\">;<\/span>   <span style=\"color: #666666; font-style: italic;\">\/\/ Green is dim<\/span>\n<span style=\"color: #993333;\">int<\/span> i <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">;<\/span>    \n&nbsp;\n<span style=\"color: #993333;\">void<\/span> setup<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span>\n<span style=\"color: #009900;\">&#123;<\/span>\n  pinMode<span style=\"color: #009900;\">&#40;<\/span>redLED<span style=\"color: #339933;\">,<\/span>   OUTPUT<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>   \n  pinMode<span style=\"color: #009900;\">&#40;<\/span>greenLED<span style=\"color: #339933;\">,<\/span> OUTPUT<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>   \n<span style=\"color: #009900;\">&#125;<\/span>\n&nbsp;\n<span style=\"color: #993333;\">void<\/span> loop<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span>\n<span style=\"color: #009900;\">&#123;<\/span>\n  <span style=\"color: #b1b100;\">if<\/span> <span style=\"color: #009900;\">&#40;<\/span>i <span style=\"color: #339933;\">&lt;<\/span> <span style=\"color: #0000dd;\">255<\/span><span style=\"color: #009900;\">&#41;<\/span>      <span style=\"color: #666666; font-style: italic;\">\/\/ First phase<\/span>\n  <span style=\"color: #009900;\">&#123;<\/span>\n    redCount   <span style=\"color: #339933;\">=<\/span> redCount<span style=\"color: #339933;\">-<\/span><span style=\"color: #0000dd;\">1<\/span><span style=\"color: #339933;\">;<\/span>   <span style=\"color: #666666; font-style: italic;\">\/\/ Red down<\/span>\n    greenCount <span style=\"color: #339933;\">=<\/span> greenCount<span style=\"color: #339933;\">+<\/span><span style=\"color: #0000dd;\">1<\/span><span style=\"color: #339933;\">;<\/span> <span style=\"color: #666666; font-style: italic;\">\/\/ Green up<\/span>\n   <span style=\"color: #009900;\">&#125;<\/span>\n  <span style=\"color: #b1b100;\">else<\/span> <span style=\"color: #b1b100;\">if<\/span> <span style=\"color: #009900;\">&#40;<\/span>i <span style=\"color: #339933;\">&lt;<\/span> <span style=\"color: #0000dd;\">509<\/span><span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #666666; font-style: italic;\">\/\/ Second phase<\/span>\n  <span style=\"color: #009900;\">&#123;<\/span>\n   redCount  <span style=\"color: #339933;\">=<\/span> redCount<span style=\"color: #339933;\">+<\/span><span style=\"color: #0000dd;\">1<\/span><span style=\"color: #339933;\">;<\/span>    <span style=\"color: #666666; font-style: italic;\">\/\/ Red up<\/span>\n   greenCount <span style=\"color: #339933;\">=<\/span> greenCount<span style=\"color: #339933;\">-<\/span><span style=\"color: #0000dd;\">1<\/span><span style=\"color: #339933;\">;<\/span> <span style=\"color: #666666; font-style: italic;\">\/\/ Green down<\/span>\n  <span style=\"color: #009900;\">&#125;<\/span> \n  <span style=\"color: #b1b100;\">else<\/span>              <span style=\"color: #666666; font-style: italic;\">\/\/ Re-set<\/span>\n  <span style=\"color: #009900;\">&#123;<\/span>\n    i <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000dd;\">0<\/span><span style=\"color: #339933;\">;<\/span>\n  <span style=\"color: #009900;\">&#125;<\/span>  \n  i <span style=\"color: #339933;\">+=<\/span> <span style=\"color: #0000dd;\">1<\/span><span style=\"color: #339933;\">;<\/span>      \n  analogWrite<span style=\"color: #009900;\">&#40;<\/span>redLED<span style=\"color: #339933;\">,<\/span>   redCount<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>   <span style=\"color: #666666; font-style: italic;\">\/\/ Write current values to LED pins<\/span>\n  analogWrite<span style=\"color: #009900;\">&#40;<\/span>greenLED<span style=\"color: #339933;\">,<\/span> greenCount<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span> \n&nbsp;\n  delay<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #0000dd;\">10<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>        <span style=\"color: #666666; font-style: italic;\">\/\/ Pause for 10 millisecond<\/span>\n<span style=\"color: #009900;\">&#125;<\/span><\/pre><\/td><\/tr><\/table><\/div>\n\n<p>&nbsp;<\/p>\n<p><strong>Output<\/strong><\/p>\n<p>In the program the duty cycle is varied from 1 to 255 in a step of 1. At the beginning, the red LED is driven with a PWM signal of duty cycle 255, which means it will be fully turned on. The PWM signal driving the green LED has the duty cycle of 1, which means it has minimum intensity. In every 10 ms, the duty cycle of the PWM signal driving the red LED\u00a0is decreased by 1 and that of the green LED is increased by 1. After approximately 2550 ms, the brightness conditions are reversed, and the green LED is fully turned on. This repeats for ever creating fade in and out effects alternately.<\/p>\n<div id=\"attachment_6035\" style=\"width: 575px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/08\/T5_OP.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6035\" class=\"size-full wp-image-6035\" title=\"T5_OP\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/08\/T5_OP.jpg\" alt=\"\" width=\"565\" height=\"442\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/08\/T5_OP.jpg 565w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/08\/T5_OP-550x430.jpg 550w\" sizes=\"auto, (max-width: 565px) 100vw, 565px\" \/><\/a><p id=\"caption-attachment-6035\" class=\"wp-caption-text\">Fade in and out<\/p><\/div>\n<div id=\"attachment_6042\" style=\"width: 575px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/08\/T5_OP1.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6042\" class=\"size-full wp-image-6042\" title=\"T5_OP1\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/08\/T5_OP1.jpg\" alt=\"\" width=\"565\" height=\"443\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/08\/T5_OP1.jpg 565w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/08\/T5_OP1-550x431.jpg 550w\" sizes=\"auto, (max-width: 565px) 100vw, 565px\" \/><\/a><p id=\"caption-attachment-6042\" class=\"wp-caption-text\">Red LED at maximum intensity, and green is  dimmed low<\/p><\/div>\n<div id=\"attachment_6043\" style=\"width: 575px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/08\/T5_OP2.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6043\" class=\"size-full wp-image-6043\" title=\"T5_OP2\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/08\/T5_OP2.jpg\" alt=\"\" width=\"565\" height=\"464\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/08\/T5_OP2.jpg 565w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/08\/T5_OP2-550x451.jpg 550w\" sizes=\"auto, (max-width: 565px) 100vw, 565px\" \/><\/a><p id=\"caption-attachment-6043\" class=\"wp-caption-text\">Green is at maximum intensity and red is faint<\/p><\/div>\n<p>LEDs operate at very low current and, therefore, it was feasible to drive them directly through the PWM outputs from the UNO32 board. For DC motor control, an external motor driver circuit, such as H-bridge, is required, which basically amplifies the low current PWM signals coming from the microcontroller pins into high current PWM signals capable of sourcing enough current to drive the motor.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Pulse width modulation (PWM) is a technique of controlling the amount of power delivered to an electronic load using an on-off digital signal. The key idea behind this technique is that the average DC value of the digital signal, and hence the power delivered to the load, can be varied by varying the duty cycle of the signal. This method is commonly used for controlling speeds of DC motors and brightness of lamps. The switching mode power supplies are also based on the PWM technique. In this tutorial, we will discuss about the PWM pins of the chipKIT Uno32 board<\/p>\n","protected":false},"author":1,"featured_media":6034,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[401],"tags":[1314,407,154,153],"class_list":["post-6033","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-chipkit","tag-chipkit","tag-mpide","tag-pulse-width-modulation","tag-pwm"],"_links":{"self":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/6033","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/comments?post=6033"}],"version-history":[{"count":10,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/6033\/revisions"}],"predecessor-version":[{"id":7442,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/6033\/revisions\/7442"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media\/6034"}],"wp:attachment":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media?parent=6033"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/categories?post=6033"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/tags?post=6033"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}