{"id":1702,"date":"2024-06-26T17:24:41","date_gmt":"2024-06-26T17:24:41","guid":{"rendered":"https:\/\/iotthinghub.com\/?p=1702"},"modified":"2024-08-11T16:56:58","modified_gmt":"2024-08-11T16:56:58","slug":"pwm-pulse-width-modulation","status":"publish","type":"post","link":"https:\/\/iotthinghub.com\/?p=1702","title":{"rendered":"PWM (Pulse width Modulation)"},"content":{"rendered":"\n<p class=\"has-text-color has-link-color wp-elements-5b66c1e67931d4ba8b4db2b1fa038985 wp-block-paragraph\" style=\"color:#5c5c5c\"><strong>PWM<\/strong> stands for pulse wide modulation. In AVR family there are three types of waveform generating modes<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<ol style=\"color:#5c5c5c;list-style-type:lower-roman\" class=\"wp-block-list has-text-color has-link-color wp-elements-13f6b977dc3e55ab2cd8dd815e26c643\">\n<li>Fast PWM<\/li>\n\n\n\n<li>Phase Correct PWM<\/li>\n\n\n\n<li>Frequency and Phase Correct PWM<\/li>\n<\/ol>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img fetchpriority=\"high\" decoding=\"async\" width=\"590\" height=\"225\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/PWM-signal.png\" alt=\"\" class=\"wp-image-1705\" style=\"width:330px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/PWM-signal.png 590w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/PWM-signal-300x114.png 300w\" sizes=\"(max-width: 590px) 100vw, 590px\" \/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-5e480a8d01e9aa923913e8cdc3b5f6ff wp-block-paragraph\" style=\"color:#5c5c5c\">The relationship between output voltage and duty cycle is<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img decoding=\"async\" width=\"459\" height=\"70\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-6.jpg\" alt=\"\" class=\"wp-image-1708\" style=\"width:289px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-6.jpg 459w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-6-300x46.jpg 300w\" sizes=\"(max-width: 459px) 100vw, 459px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-c4717972bfcc618079a62a0f45c977ad wp-block-paragraph\" style=\"color:#5c5c5c\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A PWM signal is nothing but a square wave in which the tine period (T<sub>on<\/sub>+T<sub>off<\/sub>) remain the same but the duty cycle changes. Basically all PWM mode operate with two modes<\/p>\n\n\n\n<ol style=\"color:#5c5c5c;list-style-type:lower-roman\" class=\"wp-block-list has-text-color has-link-color wp-elements-65da5b43b54c97b38c61de1bdfd0bf82\">\n<li>Non inverting mode(set at BOTTOM, clear at compare match)<\/li>\n\n\n\n<li>Inverting mode (set at compare match, clear at BOTTOM)<\/li>\n<\/ol>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:33.33%\">\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" width=\"226\" height=\"44\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-7.jpg\" alt=\"\" class=\"wp-image-1713\" style=\"width:170px;height:auto\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-798bcb506ba4bce48e544c07d64edfd8 wp-block-paragraph\" style=\"color:#5c5c5c\">In this mode Timer generate a saw tooth wave and compare it with OCRx(Output compare register) and we get a PWM output.<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:66.66%\">\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"550\" height=\"268\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/Fast-PWM.png\" alt=\"\" class=\"wp-image-1712\" style=\"width:462px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/Fast-PWM.png 550w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/Fast-PWM-300x146.png 300w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:33.33%\">\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"369\" height=\"40\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-8.jpg\" alt=\"\" class=\"wp-image-1716\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-8.jpg 369w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-8-300x33.jpg 300w\" sizes=\"(max-width: 369px) 100vw, 369px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-370ecec95481f1b34055fd09baf318d7 wp-block-paragraph\" style=\"color:#5c5c5c\">In this mode Timer generates a triangular waveform and compare it with OCRx(Output compare register), thus we get a PWM signal as shown in figure-<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:66.66%\">\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"550\" height=\"259\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/Phase-correct-PWM.png\" alt=\"\" class=\"wp-image-1717\" style=\"width:478px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/Phase-correct-PWM.png 550w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/Phase-correct-PWM-300x141.png 300w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:33.33%\">\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"603\" height=\"42\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-9.jpg\" alt=\"\" class=\"wp-image-1719\" style=\"width:316px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-9.jpg 603w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-9-300x21.jpg 300w\" sizes=\"(max-width: 603px) 100vw, 603px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-f5cb2c63271cc32296d343724951442d wp-block-paragraph\" style=\"color:#5c5c5c\">In this mode Timer generates a variable TOP sawtooth wave and compare it with OCRx(Output compare register), thus we get a PWM output at OCn pin. In this PWM mode the frequency continuously changing as shown in picture-<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:66.66%\">\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"502\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/Frequency-and-Phaes-Correct-PWM-1-1024x502.jpg\" alt=\"\" class=\"wp-image-1723\" style=\"width:441px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/Frequency-and-Phaes-Correct-PWM-1-1024x502.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/Frequency-and-Phaes-Correct-PWM-1-300x147.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/Frequency-and-Phaes-Correct-PWM-1-768x377.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/Frequency-and-Phaes-Correct-PWM-1-1536x754.jpg 1536w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/Frequency-and-Phaes-Correct-PWM-1.jpg 1920w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-0f2dd297100bf7a82447c30af9ed26c1 wp-block-paragraph\" style=\"color:#5c5c5c\">The basic difference between PWM mode and CTC mode is that in PWM mode the OCRx register is used to store the duty cycle. The procedure is just multiply the duty cycle with the TOP\/MAXimum value and store in the OCRx register.<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-36a737c11d49541ebd136a366d331b03 wp-block-paragraph\" style=\"color:#5c5c5c\">Thus for 8bit PWM mode 45% duty cycle would be obtain as follow-<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-9afafcb93d0dcd7621107485ae81b09e wp-block-paragraph\" style=\"color:#5c5c5c\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OCRx=(2^8-1)*45%<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-f38a59959fdacf52b9d726ac2e70aad1 wp-block-paragraph\" style=\"color:#5c5c5c\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =114.75\u2248115<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-5fa7a8675ba5ec6fe146a104f651699c wp-block-paragraph\" style=\"color:#5c5c5c\">Let make a program to control the brightness of an LED using Timer0. The Timer should operate in Fast PWM using 256 prescalling non inverting mode.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"794\" height=\"41\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-10.jpg\" alt=\"\" class=\"wp-image-1727\" style=\"width:504px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-10.jpg 794w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-10-300x15.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-10-768x40.jpg 768w\" sizes=\"(max-width: 794px) 100vw, 794px\" \/><\/figure>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nTCCR0|=(1&lt;&lt;WGM01)|(1&lt;&lt;WGM00); \/*** Fast PWM with TOP value 255 *********\/\nTCCR0|=(1&lt;&lt;COM01);  \/\/NON Inverting mode\n\/********** In non inverting mode if we increase the OCRx register value PWM output voltage will increase ********\/ \nTCCR0|=(1&lt;&lt;CS02);  \/\/ Prescaler = 256\nDDRB|=(1&lt;&lt;DDB3); \/\/Output OC0 pin of ATmega8\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-f6575ac8721f8200d9e3777a32442669 wp-block-paragraph\" style=\"color:#5c5c5c\">Since in non inverting PWM mode output voltage increases by increasing the OCRx register value. So the brightness and darkness condition would be-<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"791\" height=\"48\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-11.jpg\" alt=\"\" class=\"wp-image-1732\" style=\"width:544px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-11.jpg 791w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-11-300x18.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-11-768x47.jpg 768w\" sizes=\"(max-width: 791px) 100vw, 791px\" \/><\/figure>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nfor(uint8_t  i=0;i&lt;255;i++)\n    {\n     OCR2=i;_delay_ms(50);\n    }\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"779\" height=\"50\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-12.jpg\" alt=\"\" class=\"wp-image-1735\" style=\"width:483px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-12.jpg 779w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-12-300x19.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-12-768x49.jpg 768w\" sizes=\"(max-width: 779px) 100vw, 779px\" \/><\/figure>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nfor(uint8_t  i=255;i&gt;0;i--)\n    {\n      OCR2=i;_delay_ms(50);\n    }\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-0f7f431923d87e74f34a476ae43536ce wp-block-paragraph\" style=\"color:#5c5c5c\">Download the pwm.c and pwm.h file and the main program as follow-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\n  #include&lt;avr\/io.h&gt;\n  #include&lt;util\/delay.h&gt;\n  #include&quot;pwm.h&quot;\nint main(void)\n {\n  init_PWM();\n  while(1)\n  {\n\tbrightness();\n\t_delay_ms(200);\n\tdarkness();\n  }\n  return 0;\n }\n<\/pre><\/div>\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:33.33%\">\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-3e41869c wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/Brightness-Control.rar\" style=\"padding-top:var(--wp--preset--spacing--30);padding-right:var(--wp--preset--spacing--40);padding-bottom:var(--wp--preset--spacing--30);padding-left:var(--wp--preset--spacing--40)\">download<\/a><\/div>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:66.66%\">\n<p class=\"has-text-color has-link-color has-upper-heading-font-size wp-elements-0aacc57fae645a7e9dde693ba358e76c wp-block-paragraph\" style=\"color:#252525\"><a href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/Brightness-Control.rar\">Brightness control using PWM.rar<\/a><\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>PWM stands for pulse wide modulation. In AVR family there are three types of waveform generating modes The relationship between output voltage and duty cycle is &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A PWM signal is nothing but a square wave in which the tine period (Ton+Toff) remain the same but the duty cycle changes. Basically all PWM mode operate with [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-1702","post","type-post","status-publish","format-standard","hentry","category-pwm"],"_links":{"self":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/1702","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1702"}],"version-history":[{"count":15,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/1702\/revisions"}],"predecessor-version":[{"id":2607,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/1702\/revisions\/2607"}],"wp:attachment":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1702"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1702"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1702"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}