{"id":2204,"date":"2018-08-30T19:24:18","date_gmt":"2018-08-30T19:24:18","guid":{"rendered":"https:\/\/deepbluembedded.com\/?p=2204"},"modified":"2023-08-17T23:53:09","modified_gmt":"2023-08-17T20:53:09","slug":"pwm-pulse-width-modulation-tutorial","status":"publish","type":"post","link":"https:\/\/deepbluembedded.com\/pwm-pulse-width-modulation-tutorial\/","title":{"rendered":"PWM &#8211; Pulse Width Modulation Tutorial | CCP Module"},"content":{"rendered":"<table style=\"border-collapse: collapse; width: 100%;\" border=\"1\">\n<tbody>\n<tr>\n<td style=\"background-color: #ffffff; width: 5%;\"><a href=\"https:\/\/deepbluembedded.com\/ccp-modules-capture-compare-pwm\/\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1666 alignleft\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/play-256L-150x150.png\" alt=\"Previous Tutorial\" width=\"35\" height=\"35\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/play-256L-150x150.png 150w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/play-256L-160x160.png 160w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/play-256L-320x320.png 320w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/play-256L-50x50.png 50w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/play-256L.png 256w\" sizes=\"auto, (max-width: 35px) 100vw, 35px\" \/><\/a><\/td>\n<td style=\"background-color: #ffffff; width: 30%; text-align: left;\" colspan=\"2\"><a href=\"https:\/\/deepbluembedded.com\/ccp-modules-capture-compare-pwm\/\"><strong>Previous Tutorial<\/strong><\/a><\/td>\n<td style=\"background-color: #ffffff; width: 30%; text-align: center;\" colspan=\"2\"><span style=\"color: #000000;\"><strong><span style=\"font-size: 14pt;\">Tutorial 15<\/span><\/strong><\/span><\/td>\n<td style=\"background-color: #ffffff; width: 30%; text-align: right;\" colspan=\"2\"><a href=\"https:\/\/deepbluembedded.com\/dc-motor-speed-control-l293d-motor-driver\/\"><strong>Next Tutorial<\/strong><\/a><\/td>\n<td style=\"background-color: #ffffff; width: 5%;\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1667 alignright\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/play-256-150x150.png\" alt=\"Next Tutorial\" width=\"35\" height=\"35\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/play-256-150x150.png 150w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/play-256-160x160.png 160w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/play-256-320x320.png 320w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/play-256-50x50.png 50w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/play-256.png 256w\" sizes=\"auto, (max-width: 35px) 100vw, 35px\" \/><\/td>\n<\/tr>\n<tr>\n<td style=\"background-color: #ffffff; width: 5%;\"><\/td>\n<td style=\"background-color: #ffffff; text-align: center;\" colspan=\"6\"><span style=\"font-size: 14pt;\"><strong>PWM\u00a0Tutorial | CCP Modules<\/strong><\/span><\/td>\n<td style=\"background-color: #ffffff; width: 5%;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"background-color: #ffffff; width: 5%;\"><\/td>\n<td style=\"width: 20%; background-color: #ffffff; text-align: center;\" colspan=\"6\"><span style=\"font-size: 10pt;\"><strong>Intermediate Level\u00a0\u2605\u2605\u2606\u2606\u2606<\/strong><\/span><\/td>\n<td style=\"background-color: #ffffff; width: 5%;\"><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">In this tutorial, you&#8217;ll learn what is Pulse Width Modulation? What are the applications of PWM? And How to generate PWM signals with PIC Microcontrollers. We&#8217;ll discuss the last mode of operation for the CCP Modules which is PWM and develop the necessary firmware to drive the CCP Module in PWM mode. Finally, we&#8217;ll hook an LED to our PIC Microcontroller chip and create an LED Dimmer application. So let&#8217;s get started!<\/span><\/p>\n<p>[toc]<\/p>\n<hr \/>\n<h3 style=\"text-align: left;\"><span style=\"background-color: #7094ff;\"><strong><span style=\"color: #ffffff;\">\u00a0 \u00a0Required Components\u00a0 \u00a0<\/span><\/strong><\/span><\/h3>\n<table style=\"width: 100%; border-collapse: collapse; border-style: solid; background-color: #ffffff; border-color: #000000;\">\n<tbody>\n<tr>\n<td style=\"width: 10%; text-align: center; background-color: #212121;\"><span style=\"color: #ffffff;\"><strong>Qty.<\/strong><\/span><\/td>\n<td style=\"width: 33.3333%; text-align: center; background-color: #212121;\"><span style=\"color: #ffffff;\"><strong>Component Name<\/strong><\/span><\/td>\n<td style=\"width: 33.3333%; text-align: center; background-color: #212121;\"><span style=\"color: #ffffff;\"><strong>Buy<\/strong> On Amazon.com<\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 10%; text-align: center; background-color: #ffffff;\">1<\/td>\n<td style=\"width: 33.3333%; text-align: center; background-color: #ffffff;\"><span style=\"color: #000000; font-family: arial, helvetica, sans-serif;\">PIC16F877A<\/span><\/td>\n<td style=\"width: 33.3333%; text-align: center; background-color: #ffffff;\"><a href=\"https:\/\/amzn.to\/2S35QkL\" target=\"_blank\" rel=\"noopener noreferrer\">Add<\/a><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 10%; text-align: center; background-color: #ffffff;\">1<\/td>\n<td style=\"width: 33.3333%; text-align: center; background-color: #ffffff;\"><span style=\"color: #000000; font-family: arial, helvetica, sans-serif;\">BreadBoard<\/span><\/td>\n<td style=\"width: 33.3333%; text-align: center; background-color: #ffffff;\"><a href=\"https:\/\/amzn.to\/2LFYimS\" target=\"_blank\" rel=\"noopener noreferrer\">Add<\/a><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 10%; text-align: center; background-color: #ffffff;\">1<\/td>\n<td style=\"width: 33.3333%; text-align: center; background-color: #ffffff;\"><span style=\"color: #000000; font-family: arial, helvetica, sans-serif;\">LED<\/span><\/td>\n<td style=\"width: 33.3333%; text-align: center; background-color: #ffffff;\"><a href=\"https:\/\/amzn.to\/2XyQNWb\" target=\"_blank\" rel=\"noopener noreferrer\">Add<\/a>\u00a0 \u00a0 <a href=\"https:\/\/amzn.to\/2LFQ0eU\" target=\"_blank\" rel=\"noopener noreferrer\">Add<\/a><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 10%; text-align: center; background-color: #ffffff;\">1<\/td>\n<td style=\"width: 33.3333%; text-align: center; background-color: #ffffff;\"><span style=\"color: #000000; font-family: arial, helvetica, sans-serif;\">Resistors Kit<\/span><\/td>\n<td style=\"width: 33.3333%; text-align: center; background-color: #ffffff;\"><a href=\"https:\/\/amzn.to\/2XufO4F\" target=\"_blank\" rel=\"noopener noreferrer\">Add<\/a>\u00a0 \u00a0 <a href=\"https:\/\/amzn.to\/2JhXGSK\" target=\"_blank\" rel=\"noopener noreferrer\">Add<\/a><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 10%; text-align: center; background-color: #ffffff;\">1<\/td>\n<td style=\"width: 33.3333%; text-align: center; background-color: #ffffff;\"><span style=\"color: #000000; font-family: arial, helvetica, sans-serif;\">Capacitors Kit<\/span><\/td>\n<td style=\"width: 33.3333%; text-align: center; background-color: #ffffff;\"><a href=\"https:\/\/amzn.to\/2LAIQsi\" target=\"_blank\" rel=\"noopener noreferrer\">Add<\/a>\u00a0 \u00a0 <a href=\"https:\/\/amzn.to\/2Jjt4Aq\" target=\"_blank\" rel=\"noopener noreferrer\">Add<\/a><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 10%; text-align: center; background-color: #ffffff;\">1<\/td>\n<td style=\"width: 33.3333%; text-align: center; background-color: #ffffff;\"><span style=\"color: #000000; font-family: arial, helvetica, sans-serif;\">Jumper Wires Pack<\/span><\/td>\n<td style=\"width: 33.3333%; text-align: center; background-color: #ffffff;\"><a href=\"https:\/\/amzn.to\/2Xvs4gu\" target=\"_blank\" rel=\"noopener noreferrer\">Add<\/a>\u00a0 \u00a0 <a href=\"https:\/\/amzn.to\/32cgJWk\" target=\"_blank\" rel=\"noopener noreferrer\">Add<\/a><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 10%; text-align: center; background-color: #ffffff;\">1<\/td>\n<td style=\"width: 33.3333%; text-align: center; background-color: #ffffff;\"><span style=\"color: #000000; font-family: arial, helvetica, sans-serif;\">LM7805 Voltage Regulator (5v)<\/span><\/td>\n<td style=\"width: 33.3333%; text-align: center; background-color: #ffffff;\"><a href=\"https:\/\/amzn.to\/2XtVETJ\" target=\"_blank\" rel=\"noopener noreferrer\">Add<\/a><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 10%; text-align: center; background-color: #ffffff;\">1<\/td>\n<td style=\"width: 33.3333%; text-align: center; background-color: #ffffff;\"><span style=\"color: #000000; font-family: arial, helvetica, sans-serif;\">Crystal Oscillator<\/span><\/td>\n<td style=\"width: 33.3333%; text-align: center; background-color: #ffffff;\"><a href=\"https:\/\/amzn.to\/2LBpWS9\" target=\"_blank\" rel=\"noopener noreferrer\">Add<\/a><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 10%; text-align: center; background-color: #ffffff;\">1<\/td>\n<td style=\"width: 33.3333%; text-align: center; background-color: #ffffff;\"><span style=\"color: #000000; font-family: arial, helvetica, sans-serif;\">PICkit2 or 3 Programmer<\/span><\/td>\n<td style=\"width: 33.3333%; text-align: center; background-color: #ffffff;\"><a href=\"https:\/\/amzn.to\/2LHhJvy\" target=\"_blank\" rel=\"noopener noreferrer\">Add<\/a><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 10%; text-align: center; background-color: #ffffff;\">2<\/td>\n<td style=\"width: 33.3333%; text-align: center; background-color: #ffffff;\"><span style=\"color: #000000; font-family: arial, helvetica, sans-serif;\">9v Battery or DC Power Supply<\/span><\/td>\n<td style=\"width: 33.3333%; text-align: center; background-color: #ffffff;\"><a href=\"https:\/\/amzn.to\/2LIlPDG\" target=\"_blank\" rel=\"noopener noreferrer\">Add<\/a>\u00a0 \u00a0 <a href=\"https:\/\/amzn.to\/2XsWoNE\" target=\"_blank\" rel=\"noopener noreferrer\">Add<\/a>\u00a0 \u00a0 <a href=\"https:\/\/amzn.to\/2JhneiW\" target=\"_blank\" rel=\"noopener noreferrer\">Add<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong><a href=\"https:\/\/deepbluembedded.com\/microcontroller-programming-tutorial\/#_Prototyping_Board\" target=\"_blank\" rel=\"noopener noreferrer\">The Prototyping Board Setup<\/a><\/strong><\/p>\n<p><a href=\"https:\/\/deepbluembedded.com\/microcontroller-programming-tutorial\/#_Prototyping_Board\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1962\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/Prototyping-Board-Embedded-Systems-Tutorials-With-PIC-MCUs-1024x489.jpg\" alt=\"Prototyping Board - Embedded Systems Tutorials With PIC MCUs\" width=\"750\" height=\"358\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/Prototyping-Board-Embedded-Systems-Tutorials-With-PIC-MCUs-1024x489.jpg 1024w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/Prototyping-Board-Embedded-Systems-Tutorials-With-PIC-MCUs-300x143.jpg 300w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/Prototyping-Board-Embedded-Systems-Tutorials-With-PIC-MCUs-768x367.jpg 768w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/Prototyping-Board-Embedded-Systems-Tutorials-With-PIC-MCUs.jpg 1500w\" sizes=\"auto, (max-width: 750px) 100vw, 750px\" \/><\/a><\/p>\n<hr \/>\n<h3 style=\"text-align: left;\"><span style=\"background-color: #7094ff;\"><strong><span style=\"color: #ffffff;\">\u00a0 \u00a0What Is A PWM Signal?\u00a0 \u00a0<\/span><\/strong><\/span><\/h3>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\"><strong>PWM<\/strong> stands for Pulse Width Modulation. This feature provides microcontrollers by a mean of, <strong>seemingly<\/strong>, outputting analog values of voltage between (0-5)v. Instead of outputting digital values that are either <strong>Low<\/strong> (0v) or <strong>High<\/strong> (5v). The PWM signal plot looks something like as shown in the diagram below.<\/span><\/p>\n<table style=\"border-collapse: collapse; width: 100%;\" border=\"1\">\n<tbody>\n<tr>\n<td style=\"width: 100%; background-color: #ffffff;\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-2472 aligncenter\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-signal-with-variable-duty-cycle.jpg\" alt=\"PWM-signal-with-variable-duty-cycle\" width=\"399\" height=\"274\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-signal-with-variable-duty-cycle.jpg 650w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-signal-with-variable-duty-cycle-300x206.jpg 300w\" sizes=\"auto, (max-width: 399px) 100vw, 399px\" \/><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 100%; background-color: #ffffff; text-align: center;\"><span style=\"font-size: 10pt;\">image source\u00a0<a href=\"https:\/\/www.elprocus.com\/generating-pwm-signals-variable-duty-cycle-fpga\/\">https:\/\/www.elprocus.com<\/a><\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">The plot for this signal captures two features: (Frequency &amp; Duty Cycle). And we&#8217;ll discuss each of them in more detail hereafter.<\/span><\/p>\n<h4><span style=\"color: #ffffff; background-color: #333333;\">\u00a0PWM Frequency\u00a0<\/span><\/h4>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">As you know from elementary physics that a frequency of a signal is a measure of how many cycles are completed each second. It&#8217;s measured in Hz (Hertz), and we can also refer to it as the inverse of the time period of each cycle (F = 1 \/ T). Which means by varying the time period of each cycle, we&#8217;re controlling the frequency of the output signal. In fact, that&#8217;s what we&#8217;ll be doing hereafter in this tutorial&#8217;s LAB.<\/span><\/p>\n<h4><span style=\"color: #ffffff; background-color: #333333;\">\u00a0PWM Duty Cycle\u00a0<\/span><\/h4>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">The duty cycle is a measure for how long does the output line stays ON (High) to the cycle&#8217;s full-time period as a ratio. Which means the duty cycle can be easily determined using the following formula.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2445\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-Duty-Cycle.png\" alt=\"PWM Duty Cycle\" width=\"310\" height=\"48\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-Duty-Cycle.png 310w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-Duty-Cycle-300x46.png 300w\" sizes=\"auto, (max-width: 310px) 100vw, 310px\" \/><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">PWM Signal&#8217;s Effective Voltage. The controllability of the PWM duty cycle enables microcontroller devices to extend the hard-limited ON\/OFF (High\/Low) digital output pattern. Thus our microcontroller can now, seemingly not exactly, output analog values between (0-5v).\u00a0<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">You can think of it as changing the duty cycle of a PWM signal corresponds to changing the average (effective) voltage of the output line. For example, a PWM signal with 50% DC is seemingly considered a 2.5v voltage level on the output line. Hence, 80% DC is 4v, and 100% DC is 5v, and so on.<\/span><\/p>\n<table style=\"border-collapse: collapse; width: 100%;\" border=\"1\">\n<tbody>\n<tr>\n<td style=\"width: 100%; background-color: #ffffff;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-2457\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/pwm_fade-300x136.gif\" alt=\"pwm fade\" width=\"300\" height=\"136\" \/><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 100%; background-color: #ffffff; text-align: center;\"><span style=\"font-size: 10pt;\">image source\u00a0<a href=\"http:\/\/www.pyroelectro.com\/tutorials\/fading_led_pwm\/theory.html\">http:\/\/www.pyroelectro.com<\/a><\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">This analogy while not being 100% accurate in a theoretical sense. But practically, it&#8217;s the way we add sweet &amp; smooth control over light instead of On-OFF behavior with the full lighting power which can be annoying for specific applications. And we can also add a soft-start soft-end motor speed control instead of running an RC robot with 100% speed and breaking to 0% instantaneously which will result in undesirable drifts.<\/span><\/p>\n<p>&nbsp;<\/p>\n<hr \/>\n<h3 style=\"text-align: left;\"><span style=\"background-color: #7094ff;\"><strong><span style=\"color: #ffffff;\">\u00a0 \u00a0What Are The Applications Of PWM?\u00a0 \u00a0<\/span><\/strong><\/span><\/h3>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">There are numerous situations in which it&#8217;s ideal to have a PWM output signal. That&#8217;s why there are countless applications which are mainly dependent on the pulse width modulation technique. I&#8217;m going to introduce some of them down below:<\/span><\/p>\n<ul>\n<li><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Light intensity control.<\/span><\/li>\n<li><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Motor speed control.<\/span><\/li>\n<li>\u0650<span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Audio signal generation.<\/span><\/li>\n<li><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Servo control (valves, motors, etc).<\/span><\/li>\n<li style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Voltage regulation.\u00a0By switching voltage to the load with the appropriate duty cycle, the output will approximate a voltage at the desired level. The switching noise is usually filtered with an LC network.<\/span><\/li>\n<li><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">The solar tracking and charging systems.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">And much more, you can google &#8220;PWM Applications&#8221; to find out hundreds of applications related to PWM.<\/span><\/p>\n<p>&nbsp;<\/p>\n<hr \/>\n<h3 style=\"text-align: left;\"><span style=\"background-color: #7094ff;\"><strong><span style=\"color: #ffffff;\">\u00a0 \u00a0CCP Modules &#8211; PWM Mode\u00a0 \u00a0<\/span><\/strong><\/span><\/h3>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">In PWM Mode, the CCPx pin (CCP1=RC2, CCP2=RC1) produces up-to a 10-Bit resolution PWM output signal. Since the CCP1 pin is multiplexed with the PORTC data latch, the RC2 pin must be configured as an output pin by clearing the TRISC2 bit. And the same goes for the CCP2 pin (RC1).<\/span><\/p>\n<h4><span style=\"color: #ffffff; background-color: #333333;\">\u00a0PWM Mode Diagram\u00a0<\/span><\/h4>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Here is the logic (Block) diagram for the PWM mode as found in the datasheet (8.3 page-67)<\/span><\/p>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2458 size-medium\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-Block-Diagram-300x274.png\" alt=\"Pulse Width Modulation - PWM Block Diagram\" width=\"300\" height=\"274\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-Block-Diagram-300x274.png 300w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-Block-Diagram.png 414w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Or equivalently this hand-drawn version for the same diagram<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2473\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-Block-Diagram-1024x1007.jpg\" alt=\"Pulse Width Modulation - PWM Block Diagram\" width=\"400\" height=\"393\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-Block-Diagram-1024x1007.jpg 1024w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-Block-Diagram-300x295.jpg 300w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-Block-Diagram-768x755.jpg 768w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-Block-Diagram-50x50.jpg 50w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<h4><span style=\"color: #ffffff; background-color: #333333;\">\u00a0CCP Operation In PWM Mode\u00a0<\/span><\/h4>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">As you might have noticed in the above diagram, the operation of the ccp module in PWM mode goes as follows. First, the CCP module is set to operate in PWM mode by writing to the CCP1CON register. Then, the required PWM frequency will tell us the PWM time period which we&#8217;ll be loading to the <strong>PR2<\/strong> register. Timer2 should be turned ON with the pre-specified prescaler value. And the duty cycle is set by writing to the [ CCPR1L register &amp; CCP1CON&lt;5:4&gt; Bits ].<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">From the PWM Block Diagram, the PR2 value is constantly compared against the TMR2 register until a match occurs which indicates the end of the PWM cycle and the starting of a new cycle. Hence, the value of PR2 determines the time period of each PWM cycle and it consequently provides a mean of controlling the PWM output Frequency!<\/span><\/p>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2458\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-Block-Diagram.png\" alt=\"Pulse Width Modulation - PWM Block Diagram\" width=\"300\" height=\"274\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-Block-Diagram.png 414w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-Block-Diagram-300x274.png 300w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">The CCP1 pin (RC2) is set (<strong>High = 1<\/strong>) at the beginning of each cycle. And the DC (duty cycle) 10-Bit value is latched to the [ CCPR1H + 2-Bit internal latches ]. This DC value is constantly compared against the [ TMR2 + 2-Bit Q Clock ]. When a match occurs, the output latch for the CCP1 pin (RC2) is reset (<strong>Low = 0<\/strong>). And it stays low till the end of the current PWM cycle. At the beginning of a new cycle, the output latch is set again to High and so on. Hence, the DC value controls for how long the PWM output pin will be set (High). Hence, the DC 10-Bit value provides a mean for controlling the PWM output Duty Cycle!<\/span><\/p>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-2455\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-Output-Signal-Diagram-300x182.png\" alt=\"PWM Output Signal Diagram\" width=\"300\" height=\"182\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-Output-Signal-Diagram-300x182.png 300w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-Output-Signal-Diagram.png 587w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<h4><span style=\"color: #ffffff; background-color: #333333;\">\u00a0Notes For PWM Mode\u00a0<\/span><\/h4>\n<p><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">I- <strong>Clearing the CCP1CON register<\/strong>:<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\"> Clearing the CCP1CON register will force the CCP1 PWM output latch to the default low level. This is not the PORTC I\/O data latch.<\/span><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">II- <strong>Timer2 postscaler<\/strong>:<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">The Timer2 postscaler is not used in the determination of the PWM frequency. The postscaler could be used to have a servo update rate at a different frequency than the PWM output.<\/span><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">III- <strong>Duty Cycle value<\/strong>:<\/span><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\"> If the PWM duty cycle value is longer than the PWM period, the CCP1 pin will not be cleared.<\/span><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">IV- <strong>Double-Buffering &amp; G<\/strong><\/span><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\"><strong>litches<\/strong>:<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\"> CCPR1L and CCP1CON&lt;5:4&gt; can be written to at any time, but the duty cycle value is not latched into CCPR1H until after a match between PR2 and TMR2 occurs (i.e., the period is complete). In PWM mode, CCPR1H is a read-only register. The CCPR1H register and a 2-bit internal latch are used to double-buffer the PWM duty cycle. This double-buffering is essential for glitch-free PWM operation.<\/span><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">V- <strong>PWM Resolution<\/strong>:<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">The PWM duty cycle is specified by writing to the CCPR1L register and to the CCP1CON&lt;5:4&gt; bits. Up to 10-bit resolution is available. The maximum PWM resolution (bits) for a given PWM frequency is given by the following formula.<\/span><br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2454 size-full\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-Resolution-Eqn.png\" alt=\"Pulse Width Modulation - PWM Resolution Eqn\" width=\"231\" height=\"52\" \/><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">The typical resolution (in bits) for a specific PWM frequency, with a specific Prescaler ratio PS, for a system running at Fosc clock rate is given by the equation down below<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2474 size-full\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-Resolution-Eqation.png\" alt=\"Pulse Width Modulation - PWM Resolution Eqation\" width=\"239\" height=\"49\" \/><\/p>\n<p>&nbsp;<\/p>\n<hr \/>\n<h3 style=\"text-align: left;\"><span style=\"background-color: #7094ff;\"><strong><span style=\"color: #ffffff;\">\u00a0 \u00a0Configuring CCP Modules For PWM Mode\u00a0 \u00a0<\/span><\/strong><\/span><\/h3>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Here is the step-by-step procedure that you should follow in order to operate the CCP module in the PWM mode to generate the PWM output signal with your desired duty cycle and frequency.<\/span><\/p>\n<p><span style=\"color: #ffffff; background-color: #333333;\">\u00a0Configuring The CCP1 Module For PWM Operation\u00a0<\/span><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\"><strong>Step1<\/strong> &#8211;\u00a0 Configure the CCP module for PWM operation<\/span><\/p>\n<ul>\n<li style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">As mentioned in the datasheet in the CCP1CON register&#8217;s description. Writing 11xx to the CCP1M0:CCP1M3 4-Bits selects the PWM mode of operation. Note that xx means that Bit0,1 are don&#8217;t cares! that&#8217;s why we&#8217;ll neglect them while writing the code.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\"><strong>Step2<\/strong> &#8211;\u00a0 Configure the CCP1 pin (RC2) to be an output pin by clearing the corresponding TRISC2 bit<\/span><\/p>\n<ul>\n<li><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">The CCP1\/RC2 pin should be configured as an output pin.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\"><strong>Step3<\/strong> &#8211;\u00a0 Determine the PWM frequency and get the PWM period value<\/span><\/p>\n<ul>\n<li style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Let&#8217;s say we need to have a PWM signal with a frequency of 5kHz. This means that the time period should be = 1\/F = 1\/5000 = 200\u00b5s<\/span><\/li>\n<\/ul>\n<p><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\"><strong>Step4<\/strong> &#8211;\u00a0 Using The PWM period, Calculate the value which we&#8217;ll load to the <strong>PR2<\/strong> Register<\/span><\/p>\n<ul>\n<li style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">This step involves choosing a value for Timer2 prescaler prior to determining the PR2 register&#8217;s value. For this step, we&#8217;ll be using the following equation<\/span><\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2446 size-full\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-Time-Period.png\" alt=\"Pulse Width Modulation - PWM Time Period\" width=\"526\" height=\"19\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-Time-Period.png 526w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-Time-Period-300x11.png 300w\" sizes=\"auto, (max-width: 526px) 100vw, 526px\" \/><\/p>\n<ul>\n<li style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">We now have the PWM<sub>period<\/sub>, the T<sub>osc<\/sub> = 1\/F<sub>osc<\/sub>, and we&#8217;ll pick an initial value for Timer2Prescaler (say 1 for 1:1 ratio). It should be easy to solve for <strong>PR2<\/strong> to get its value. The rule of thumb is that PR2 register is an 8-Bit register which means its value ranges from (0 to 255). Your result should never exceed this 255 cap. Otherwise, you should try another value for the Timer2Prescaler.\u00a0<\/span><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Finally, after getting the PR2 value, we should move this number to the PR2 register.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\"><strong>Step5<\/strong> &#8211;\u00a0 Set the prescaler of Timer2<\/span><\/p>\n<ul>\n<li style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Adjust the Timer2 prescaler value to match the value which you&#8217;ve already used in the previous step. This is done by writing to the T2CON register T2CKPSx bits.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\"><strong>Step6<\/strong> &#8211;\u00a0 Step The PWM Duty Cycle, by writing to the CCPR1L register &amp; CCP1CON&lt;5:4&gt; Bits<\/span><\/p>\n<ul>\n<li style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">To set or change the Duty Cycle of the PWM output signal, you should calculate and write the DC value to the 10-Bit buffer register which consists of CCPR1L:CCP1CON&lt;5:4&gt;. The CCPR1L contains the eight MSBs and the CCP1CON&lt;5:4&gt; contains the two LSbs. This 10-bit value is represented by CCPR1L:CCP1CON&lt;5:4&gt;.<\/span><\/li>\n<li><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\"> The following equation is used to calculate the PWM duty cycle in time:<\/span><\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2459 size-full\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-Duty-Cycle-1.png\" alt=\"Pulse Width Modulation - PWM Duty Cycle\" width=\"616\" height=\"21\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-Duty-Cycle-1.png 616w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-Duty-Cycle-1-300x10.png 300w\" sizes=\"auto, (max-width: 616px) 100vw, 616px\" \/><\/p>\n<ul>\n<li style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Let&#8217;s say you&#8217;re willing to get 40% DC for your PWM output. Then you should multiply 0.40 by PWM<sub>period\u00a0<\/sub>To get the PWM<sub>DutyCycle\u00a0<\/sub>in time. Now, substitute for PWM<sub>DutyCycle\u00a0<\/sub>, T<sub>osc\u00a0<\/sub>, TMR2<sub>prescaler\u00a0<\/sub>in the previous equation and solve for the 10-Bit value between the ( )<\/span><\/li>\n<li style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">After getting the 10-Bit duty cycle from your calculations, just write this value to the buffer\u00a0CCPR1L:CCP1CON&lt;5:4&gt;<\/span><\/li>\n<\/ul>\n<p><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\"><strong>Step7<\/strong> &#8211;\u00a0 Turn ON Timer2<\/span><\/p>\n<ul>\n<li style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Don&#8217;t forget to turn ON the Timer2 module! it should be obvious that it&#8217;s the working-horse of our system. However, it happens to find out that you&#8217;ve missed doing so and that&#8217;s why nothing is working at all.<\/span><\/li>\n<\/ul>\n<hr \/>\n<h3 style=\"text-align: left;\"><span style=\"background-color: #7094ff;\"><strong><span style=\"color: #ffffff;\">\u00a0 \u00a0Implementing PWM Firmware Driver\u00a0 \u00a0<\/span><\/strong><\/span><\/h3>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ffffff; background-color: #333333;\">\u00a0Implementing CCP1 PWM Output Driver\u00a0<\/span><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\"><strong>Step1<\/strong> &#8211; Configure the CCP module for PWM operation<\/span><\/p>\n<pre class=\"lang:c decode:true\">\/\/--[ Configure The CCP1 Module For PWM Mode ]--\r\nCCP1M2 = 1;\r\nCCP1M3 = 1;<\/pre>\n<p><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\"><strong>Step2<\/strong> &#8211; Configure the CCP1 pin (RC2) to be an output pin<\/span><\/p>\n<pre class=\"lang:c decode:true \">TRISC2 = 0;  \/\/ RC2\/CCP1(PWM) Output Pin<\/pre>\n<p><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\"><strong>Step3<\/strong> &#8211; Determine the PWM frequency<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Let&#8217;s say our objective is to generate a 2kHz PWM signal, and that is our desired PWM<sub>Frequency<\/sub>. And let&#8217;s also assume our microcontroller is running at 4MHz. Well, it&#8217;s time to choose the TMR2<sub>prescaler<\/sub> value to be capable of using the following equation in order to get the PR2 register&#8217;s value. let&#8217;s pick 1:1 PS and substitute in the frequency equation<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2446\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-Time-Period.png\" alt=\"PWM Time Period\" width=\"526\" height=\"19\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-Time-Period.png 526w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-Time-Period-300x11.png 300w\" sizes=\"auto, (max-width: 526px) 100vw, 526px\" \/><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">After substitution, our equation will be as follows\u00a0<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2462 size-full\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-freqency-step.png\" alt=\"Pulse Width Modulation - PWM freqency step\" width=\"326\" height=\"42\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-freqency-step.png 326w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-freqency-step-300x39.png 300w\" sizes=\"auto, (max-width: 326px) 100vw, 326px\" \/><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Solving for PR2 will yield the following result. (PR2 = 499), which exceeds the 8-Bit range (0-255). Then it&#8217;s not OK to use this prescaler value and we&#8217;ll pick another value!<\/span><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Let&#8217;s check a prescaler of 1:4 and solve for PR2 once again.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2463 size-full\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-Freq-Step3.png\" alt=\"Pulse Width Modulation - PWM Freq Step3\" width=\"330\" height=\"42\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-Freq-Step3.png 330w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-Freq-Step3-300x38.png 300w\" sizes=\"auto, (max-width: 330px) 100vw, 330px\" \/><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Solving for PR2 will now yield the following result. (PR2 = 124). which falls in the 8-Bit range (0-255). So, it&#8217;s OK to use this prescaler value and let&#8217;s proceed to the next step.<\/span><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\"><strong>Step4<\/strong> &#8211; Write to the <strong>PR2<\/strong> Register<\/span><\/p>\n<pre class=\"lang:c decode:true \">PR2 = 124;<\/pre>\n<p><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\"><strong>Step5<\/strong> &#8211; Set the prescaler of Timer2<\/span><\/p>\n<pre class=\"lang:c decode:true\">\/\/ Set Timer2 Prescaler To Match The (1:4) Ratio\r\nT2CKPS0 = 1;\r\nT2CKPS1 = 0;<\/pre>\n<p><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\"><strong>Step6<\/strong> &#8211; Set The PWM Duty Cycle<\/span><\/p>\n<p><span style=\"color: #000000; font-family: arial, helvetica, sans-serif;\">This step involves both calculating and writing the 10-Bit DC value as discussed earlier. Let&#8217;s say you&#8217;re willing to get a 70% Duty Cycle for your PWM output. Then the PWM<sub>DutyCycle<\/sub> Time is calculated by multiplying the PWM<sub>period<\/sub> by 0.70 as shown down below<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2467\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-DC-step.png\" alt=\"PWM DC step\" width=\"242\" height=\"42\" \/><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Now, we&#8217;re able to use the duty cycle equation which we&#8217;ve seen earlier. And solve for the 10-Bit value between the ( )<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2459\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-Duty-Cycle-1.png\" alt=\"PWM Duty Cycle\" width=\"616\" height=\"21\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-Duty-Cycle-1.png 616w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-Duty-Cycle-1-300x10.png 300w\" sizes=\"auto, (max-width: 616px) 100vw, 616px\" \/><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">This will result in (CCPR1L:CCP1CON&lt;5:4&gt;) = 350<\/span><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">This actually means that 70% DC maps to 350<\/span><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Therefore, 100% DC maps to (100*350)\/70 = 500, and 50% DC maps to 250, and so on!<\/span><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Now, we&#8217;ve got to write the 350 value to the 10-Bit buffer register. This is done as shown below<\/span><\/p>\n<pre class=\"lang:c decode:true\">CCP1CONbits.CCP1Y = (350) &amp; 1; \/\/ Get bit-0 (The LSB)\r\nCCP1CONbits.CCP1X = (350) &amp; 2; \/\/ Get bit-1\r\nCCPR1L = (350) &gt;&gt; 2;           \/\/ Move The 8 MSBs To CCPR1L register<\/pre>\n<p><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\"><strong>Step7<\/strong> &#8211; Turn ON Timer2<\/span><\/p>\n<pre class=\"lang:c decode:true\">TMR2ON = 1;<\/pre>\n<p>&nbsp;<\/p>\n<hr \/>\n<h3 style=\"text-align: left;\"><span style=\"background-color: #7094ff;\"><strong><span style=\"color: #ffffff;\">\u00a0 \u00a0LED Dimmer &#8211; LAB\u00a0 \u00a0<\/span><\/strong><\/span><\/h3>\n<p>&nbsp;<\/p>\n<table style=\"background-color: #ffffff;\">\n<tbody>\n<tr>\n<td style=\"background-color: #ffffff; text-align: left; width: 20%;\"><span style=\"font-family: arial, helvetica, sans-serif;\"><strong><span style=\"color: #000000;\">Lab Name<\/span><\/strong><\/span><\/td>\n<td style=\"background-color: #ffffff; text-align: left; width: 80%;\"><span style=\"color: #000000; font-family: arial, helvetica, sans-serif;\">PWM &#8211; LED Dimmer<\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"background-color: #ffffff; text-align: left; width: 20%;\"><span style=\"font-family: arial, helvetica, sans-serif;\"><strong><span style=\"color: #000000;\">Lab Number<\/span><\/strong><\/span><\/td>\n<td style=\"background-color: #ffffff; text-align: left; width: 80%;\"><span style=\"color: #000000; font-family: arial, helvetica, sans-serif;\">13<\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"background-color: #ffffff; text-align: left; width: 20%;\"><span style=\"font-family: arial, helvetica, sans-serif;\"><strong><span style=\"color: #000000;\">Lab Level<\/span><\/strong><\/span><\/td>\n<td style=\"background-color: #ffffff; text-align: left; width: 80%;\"><span style=\"color: #000000; font-family: arial, helvetica, sans-serif;\">Beginner<\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"background-color: #ffffff; text-align: left; width: 20%;\"><span style=\"font-family: arial, helvetica, sans-serif;\"><strong><span style=\"color: #000000;\">Lab Objectives<\/span><\/strong><\/span><\/td>\n<td style=\"background-color: #ffffff; text-align: justify; width: 80%;\"><span style=\"color: #000000; font-family: arial, helvetica, sans-serif;\">Learn how to use CCP modules to generate PWM signals with a desired frequency of 2kHz and variable duty cycle. We&#8217;ll use the output PWM signal to control the brightness of a small LED in this LAB. The duty cycle should be gradually increasing from 0 up-to 100% then it should start to gradually drop back to 0% and so on.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h4 style=\"text-align: center;\"><span style=\"color: #ffffff; background-color: #453f3f;\"><strong>\u00a0 \u00a0 \u00a0 \u00a01. Coding\u00a0 \u00a0 \u00a0 \u00a0<\/strong><\/span><\/h4>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\"><strong>Open<\/strong> the MPLAB IDE and create a new project name it &#8220;PWM_LED_Dimmer&#8221;. If you have some issues doing so, you can always refer to the previous tutorial using the link below.<\/span><\/p>\n<p><a href=\"https:\/\/deepbluembedded.com\/creating-new-project-with-mplab\/\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1991\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/07\/link_frame_newproject.jpg\" alt=\"Create New Project With MPLAB IDE\" width=\"375\" height=\"107\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/07\/link_frame_newproject.jpg 700w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/07\/link_frame_newproject-300x86.jpg 300w\" sizes=\"auto, (max-width: 375px) 100vw, 375px\" \/><\/a><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\"><strong>Set<\/strong> the configuration bits to match the generic setting which we&#8217;ve stated earlier. And if you also find troubles creating this file, you can always refer to the previous tutorial using the link below.<\/span><\/p>\n<p><a href=\"https:\/\/deepbluembedded.com\/configuration-bits-fuses-for-microcontrollers\/\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1581\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/tutorial_link_frame_config.jpg\" alt=\"\" width=\"375\" height=\"107\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/tutorial_link_frame_config.jpg 700w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/tutorial_link_frame_config-300x86.jpg 300w\" sizes=\"auto, (max-width: 375px) 100vw, 375px\" \/><\/a><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Now, open the <strong>main.c<\/strong> file and let&#8217;s start developing the firmware for our project.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Our first task is to configure the CCP module to operate in PWM mode. And also configure the PWM output frequency to be 2kHz. Then in the main routine loop, we&#8217;ll be gradually increasing the Duty Cycle of the PWM output from 0 to 100% and gradually drop it back to 0% once again and so on.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">It&#8217;s been always a better practice to create a dedicated function for each task of these. However, for this lab, I&#8217;ll be only creating a function for manipulating the DC value in order to fit it in the PWM DC buffer register. The rest of the code will reside normally in the main routine.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">The full code listing for this lab is as follows<\/span><\/p>\n<pre class=\"lang:c decode:true\">\/*\r\n* LAB Number: 13\r\n* LAB Name: PWM - LED Dimmer\r\n* Author: Khaled Magdy\r\n* For More Information Visit My Website @ DeepBlueMbedded.com\r\n*\r\n*\/\r\n#include &lt;xc.h&gt;\r\n#include &lt;stdint.h&gt;\r\n#include \"config.h\"\r\n#define _XTAL_FREQ 4000000\r\n\/\/================================\r\nvoid PWM1_Set_Duty(uint16_t);\r\n\r\nvoid main(void) \r\n{\r\n  \/\/--[ Configure The CCP Module For PWM Mode ]--\r\n  CCP1M3 = 1;\r\n  CCP1M2 = 1;\r\n  TRISC2 = 0; \/\/ The CCP1 Output Pin (PWM)\r\n  \/\/ Set The PWM Frequency\r\n  PR2 = 124;\r\n  \/\/ Set The PS For Timer2 (1:4 Ratio)\r\n  T2CKPS0 = 1;\r\n  T2CKPS1 = 0;\r\n  \/\/ Start CCP1 PWM !\r\n  TMR2ON = 1;\r\n\r\n  uint16_t DC=0; \r\n  \/\/ Write The System's Main Routine\r\n  while(1)\r\n  {\r\n    DC = 0; \/\/ Start With 0% DutyCycle\r\n\r\n    \/\/ Gradually increase LED brightness\r\n    while (DC&lt;500)\r\n    {\r\n      PWM1_Set_Duty(DC);\r\n      DC++;\r\n      __delay_ms(2);\r\n    }\r\n    __delay_ms(100);\r\n\r\n    \/\/ Gradually decrease LED brightness\r\n    while(DC&gt;0)\r\n    {\r\n      PWM1_Set_Duty(DC);\r\n      DC--;\r\n      __delay_ms(2);\r\n    }\r\n\r\n    __delay_ms(100);\r\n  }\r\n  return;\r\n}\r\n\r\nvoid PWM1_Set_Duty(uint16_t DC)\r\n{\r\n  \/\/ Check The DC Value To Make Sure it's Within 10-Bit Range\r\n  if(DC&lt;1024)\r\n  {\r\n    CCP1Y = DC &amp; 1;\r\n    CCP1X = DC &amp; 2;\r\n    CCPR1L = DC &gt;&gt; 2;\r\n  }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<h4 style=\"text-align: center;\"><span style=\"color: #ffffff; background-color: #453f3f;\"><strong>\u00a0 \u00a0 \u00a0 \u00a02. Simulation\u00a0 \u00a0 \u00a0 \u00a0<\/strong><\/span><\/h4>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">To simulate the project, just hook an LED to the RC2\/CCP1 pin with a 300\u03a9 resistor. Add the hex file and run the simulator. Here is the schematic for this lab<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2481\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-LAB-Schematic.png\" alt=\"Pulse Width Modulation - PWM LAB Schematic\" width=\"350\" height=\"329\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-LAB-Schematic.png 478w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-LAB-Schematic-300x282.png 300w\" sizes=\"auto, (max-width: 350px) 100vw, 350px\" \/><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">It&#8217;s highly recommended to hook an oscilloscope to the PWM output line in order to have a clear vision of what is exactly going on. Due to many factors, LED dimming may not be running in real-time on simulators. And here is the typical simulation results!<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2484 size-full\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-Dimmer-Simulation.gif\" alt=\"Pulse Width Modulation - PWM Dimmer Simulation\" width=\"854\" height=\"434\" \/><\/p>\n<p>&nbsp;<\/p>\n<h4 style=\"text-align: center;\"><span style=\"color: #ffffff; background-color: #453f3f;\"><strong>\u00a0 \u00a0 \u00a0 \u00a03. Prototyping\u00a0 \u00a0 \u00a0 \u00a0<\/strong><\/span><\/h4>\n<p>&nbsp;<\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Prototyping this project should also be an easy task. The tough part has been already done before writing the code. And here is the output in case you&#8217;re curious.<\/span><\/p>\n<table style=\"border-collapse: collapse; width: 100%;\" border=\"1\">\n<tbody>\n<tr>\n<td style=\"width: 100%; background-color: #ffffff;\"><a href=\"https:\/\/www.youtube.com\/watch?v=S0SJynksLV8\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2053\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/07\/Play-Video-On-YouTube.jpg\" alt=\"Play Video On YouTube\" width=\"560\" height=\"70\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/07\/Play-Video-On-YouTube.jpg 560w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/07\/Play-Video-On-YouTube-300x38.jpg 300w\" sizes=\"auto, (max-width: 560px) 100vw, 560px\" \/><\/a><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 100%; background-color: #ffffff; text-align: center;\"><iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube-nocookie.com\/embed\/S0SJynksLV8?rel=0\" frameborder=\"0\" allow=\"autoplay; encrypted-media\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<hr \/>\n<h3 style=\"text-align: left;\"><span style=\"background-color: #7094ff;\"><strong><span style=\"color: #ffffff;\">\u00a0 \u00a0Tips + Concluding Remarks\u00a0 \u00a0<\/span><\/strong><\/span><\/h3>\n<p>&nbsp;<\/p>\n<p><span style=\"font-size: 18pt; color: #ffffff; background-color: #333333;\">\u00a0 <strong>1<\/strong>\u00a0\u00a0<\/span><\/p>\n<p><strong>PWM Calculations<\/strong><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">From my own experience, I&#8217;ve seen many embedded practitioners struggling with the calculations part of operating the PWM module. That&#8217;s why I&#8217;m going to summarize it all up once again for you.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Where are actually calculating a couple of values:<\/span><\/p>\n<ul>\n<li style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">The period to be loaded in the PR2 register<\/span><\/li>\n<li style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">The duty cycle 10-Bit value to be loaded in the [ CCPR1L register &amp; CCP1CON&lt;5:4&gt; Bits ]<\/span><\/li>\n<\/ul>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">If you&#8217;re still remembering the first section of this tutorial when I&#8217;ve told you that the PWM signal plot captures 2 features: (The PWM frequency &amp; The PWM Duty Cycle). And we (the programmers) specify each of those features to meet the design requirements.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Obviously, setting the output PWM frequency is done by writing to the PR2 register. And the output PWM Duty Cycle is set by writing to the [ CCPR1L register &amp; CCP1CON&lt;5:4&gt; Bits ].<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">That&#8217;s why we&#8217;ve to calculate these two values. We&#8217;ve to do so in order to control both the Frequency and Duty Cycle of the output PWM signal.<\/span><\/p>\n<p><span style=\"color: #000000;\"><strong>1-<\/strong> Frequency Control<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">To control the frequency of the PWM output, we write to the PR2 register and its value is given by the following equation<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2446\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-Time-Period.png\" alt=\"PWM Time Period\" width=\"526\" height=\"19\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-Time-Period.png 526w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-Time-Period-300x11.png 300w\" sizes=\"auto, (max-width: 526px) 100vw, 526px\" \/><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">The only missing parameter other than PR2 is the TMR2PS which you should try guessing &amp; testing. This should be done in such a way that gives you an 8-Bit value for the PR2 register. If the result has exceeded the (0-255) range for 8-Bit values, you should try another value for the prescaler until this condition is met.<\/span><\/p>\n<p><span style=\"color: #000000;\"><strong>2-<\/strong> Duty Cycle Control<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">To control the DutyCycle of the PWM output, we write to the DC buffer 10-Bit register\u00a0which consists of (CCPR1L:CCP1CON&lt;5:4&gt;). Calculating the value which we should write to this register in order to get a specific DC percentage % is done using the formula down below.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2459\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-Duty-Cycle-1.png\" alt=\"PWM Duty Cycle\" width=\"616\" height=\"21\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-Duty-Cycle-1.png 616w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-Duty-Cycle-1-300x10.png 300w\" sizes=\"auto, (max-width: 616px) 100vw, 616px\" \/><\/p>\n<p><span style=\"background-color: #333333; color: #ffffff; font-size: 18pt;\"><strong>\u00a0 2\u00a0\u00a0<\/strong><\/span><\/p>\n<p><strong>PWM Timer2 Utilization<\/strong><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">The CCPx modules (CCP1 &amp; CCP2) in PWM Mode both are utilizing the Timer2 module as a hardware resource for controlling the timing features of the PWM output signal. This fact necessarily means that both CCP1 &amp; CCP2 will be restricted to generate\u00a0PWM signals with the exact same frequency. And also you can not use the Timer2 module for any other function within your system. However, you can disable the respective CCPx module for a specific time period during which you can use the Timer2 as a general purpose 8-Bit Timer. Then you can switch back to enable the PWM output, only if it&#8217;s feasible for your system, and it should be carefully implemented.<\/span><\/p>\n<p><span style=\"background-color: #333333; color: #ffffff; font-size: 18pt;\"><strong>\u00a0 3\u00a0\u00a0<\/strong><\/span><\/p>\n<p><strong>PWM Resolution<\/strong><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">You can always double-check the output typical resolution for your PWM signal using the formula shown down below as I&#8217;ve stated earlier in this tutorial.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2474\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-Resolution-Eqation.png\" alt=\"PWM Resolution Eqation\" width=\"239\" height=\"49\" \/><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">But for now, let&#8217;s use this knowledge in a practical sense. In the previous Lab, our system was being clocked at a 4Mhz rate, with a 1:4 PS ratio, and the desired output PWM frequency was 2kHz. Well, the previous equation is now telling us the following results<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2475\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-Resolution-Example.png\" alt=\"PWM Resolution Example\" width=\"318\" height=\"53\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-Resolution-Example.png 318w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-Resolution-Example-300x50.png 300w\" sizes=\"auto, (max-width: 318px) 100vw, 318px\" \/><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Well, this results means that we typically need 9-Bits to represent the Duty Cycle for the PWM output ranging from (0 to 512). What I&#8217;ve done here is a <strong>ceiling<\/strong> process. Which is approximating the result to the upper next integer.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2476\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-Resolution-Ceiling.png\" alt=\"PWM Resolution Ceiling\" width=\"415\" height=\"20\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-Resolution-Ceiling.png 415w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/PWM-Resolution-Ceiling-300x14.png 300w\" sizes=\"auto, (max-width: 415px) 100vw, 415px\" \/><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">However, it&#8217;s not an accurate estimation for the maximum duty cycle range. Yes, we need only 9-Bits in resolution in order to get a 0-to-100% DutyCycle variation capability. But it&#8217;s not exactly what we should be doing.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Interestingly, raising 2 to the power of this exact resolution 8.965 yields the following result<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000; font-size: 14pt;\">2<sup>8.965<\/sup>\u00a0= 500<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Which is exactly the maximum value for the duty cycle! Which will give you a 100% DC if you&#8217;ve loaded it to the PWM buffer register (CCPR1L:CCP1CON&lt;5:4&gt;).<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<table style=\"border-collapse: collapse; width: 100%;\" border=\"1\">\n<tbody>\n<tr>\n<td style=\"background-color: #ffffff; width: 5%;\"><a href=\"https:\/\/deepbluembedded.com\/ccp-modules-capture-compare-pwm\/\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1666 alignleft\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/play-256L-150x150.png\" alt=\"Previous Tutorial\" width=\"35\" height=\"35\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/play-256L-150x150.png 150w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/play-256L-160x160.png 160w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/play-256L-320x320.png 320w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/play-256L-50x50.png 50w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/play-256L.png 256w\" sizes=\"auto, (max-width: 35px) 100vw, 35px\" \/><\/a><\/td>\n<td style=\"background-color: #ffffff; width: 30%; text-align: left;\" colspan=\"2\"><a href=\"https:\/\/deepbluembedded.com\/ccp-modules-capture-compare-pwm\/\"><strong>Previous Tutorial<\/strong><\/a><\/td>\n<td style=\"background-color: #ffffff; width: 30%; text-align: center;\" colspan=\"2\"><span style=\"color: #000000;\"><strong><span style=\"font-size: 14pt;\">Tutorial 15<\/span><\/strong><\/span><\/td>\n<td style=\"background-color: #ffffff; width: 30%; text-align: right;\" colspan=\"2\"><a href=\"https:\/\/deepbluembedded.com\/dc-motor-speed-control-l293d-motor-driver\/\"><strong>Next Tutorial<\/strong><\/a><\/td>\n<td style=\"background-color: #ffffff; width: 5%;\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1667 alignright\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/play-256-150x150.png\" alt=\"Next Tutorial\" width=\"35\" height=\"35\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/play-256-150x150.png 150w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/play-256-160x160.png 160w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/play-256-320x320.png 320w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/play-256-50x50.png 50w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/play-256.png 256w\" sizes=\"auto, (max-width: 35px) 100vw, 35px\" \/><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn what is Pulse Width Modulation? What are the applications of PWM? And How to generate PWM signals with PIC Microcontrollers. We&#8217;ll discuss the PWM mode and develop the necessary firmware to create an LED Dimmer application<\/p>\n","protected":false},"author":1,"featured_media":2443,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[28,30,27],"tags":[],"class_list":["post-2204","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-embedded-systems","category-embedded-tutorials","category-microchip-pic","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33","no-featured-image-padding"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Pulse Width Modulation - PWM Tutorial | DeepBlue Embedded Tutorials<\/title>\n<meta name=\"description\" content=\"In this tutorial, you&#039;ll learn what is Pulse Width Modulation? What are the applications of PWM? And How to generate PWM signals with PIC MCU\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/deepbluembedded.com\/pwm-pulse-width-modulation-tutorial\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Pulse Width Modulation - PWM Tutorial | DeepBlue Embedded Tutorials\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you&#039;ll learn what is Pulse Width Modulation? What are the applications of PWM? And How to generate PWM signals with PIC MCU\" \/>\n<meta property=\"og:url\" content=\"https:\/\/deepbluembedded.com\/pwm-pulse-width-modulation-tutorial\/\" \/>\n<meta property=\"og:site_name\" content=\"DeepBlue\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/khaled.elrawy.359\/\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/khaled.elrawy.359\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-08-30T19:24:18+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-08-17T20:53:09+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/pwm.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"720\" \/>\n\t<meta property=\"og:image:height\" content=\"340\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Khaled Magdy\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Khaled Magdy\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"17 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/deepbluembedded.com\\\/pwm-pulse-width-modulation-tutorial\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/deepbluembedded.com\\\/pwm-pulse-width-modulation-tutorial\\\/\"},\"author\":{\"name\":\"Khaled Magdy\",\"@id\":\"https:\\\/\\\/deepbluembedded.com\\\/#\\\/schema\\\/person\\\/30259d66cf68c6c681562dbe551b7867\"},\"headline\":\"PWM &#8211; Pulse Width Modulation Tutorial | CCP Module\",\"datePublished\":\"2018-08-30T19:24:18+00:00\",\"dateModified\":\"2023-08-17T20:53:09+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/deepbluembedded.com\\\/pwm-pulse-width-modulation-tutorial\\\/\"},\"wordCount\":3188,\"commentCount\":14,\"publisher\":{\"@id\":\"https:\\\/\\\/deepbluembedded.com\\\/#\\\/schema\\\/person\\\/30259d66cf68c6c681562dbe551b7867\"},\"image\":{\"@id\":\"https:\\\/\\\/deepbluembedded.com\\\/pwm-pulse-width-modulation-tutorial\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/deepbluembedded.com\\\/wp-content\\\/uploads\\\/2018\\\/08\\\/pwm.jpg\",\"articleSection\":[\"Embedded Systems\",\"Embedded Tutorials\",\"Microchip PIC\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/deepbluembedded.com\\\/pwm-pulse-width-modulation-tutorial\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/deepbluembedded.com\\\/pwm-pulse-width-modulation-tutorial\\\/\",\"url\":\"https:\\\/\\\/deepbluembedded.com\\\/pwm-pulse-width-modulation-tutorial\\\/\",\"name\":\"Pulse Width Modulation - PWM Tutorial | DeepBlue Embedded Tutorials\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/deepbluembedded.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/deepbluembedded.com\\\/pwm-pulse-width-modulation-tutorial\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/deepbluembedded.com\\\/pwm-pulse-width-modulation-tutorial\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/deepbluembedded.com\\\/wp-content\\\/uploads\\\/2018\\\/08\\\/pwm.jpg\",\"datePublished\":\"2018-08-30T19:24:18+00:00\",\"dateModified\":\"2023-08-17T20:53:09+00:00\",\"description\":\"In this tutorial, you'll learn what is Pulse Width Modulation? What are the applications of PWM? And How to generate PWM signals with PIC MCU\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/deepbluembedded.com\\\/pwm-pulse-width-modulation-tutorial\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/deepbluembedded.com\\\/pwm-pulse-width-modulation-tutorial\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/deepbluembedded.com\\\/pwm-pulse-width-modulation-tutorial\\\/#primaryimage\",\"url\":\"https:\\\/\\\/deepbluembedded.com\\\/wp-content\\\/uploads\\\/2018\\\/08\\\/pwm.jpg\",\"contentUrl\":\"https:\\\/\\\/deepbluembedded.com\\\/wp-content\\\/uploads\\\/2018\\\/08\\\/pwm.jpg\",\"width\":720,\"height\":340,\"caption\":\"pwm-pulse-width-modulation\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/deepbluembedded.com\\\/pwm-pulse-width-modulation-tutorial\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/deepbluembedded.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PWM &#8211; Pulse Width Modulation Tutorial | CCP Module\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/deepbluembedded.com\\\/#website\",\"url\":\"https:\\\/\\\/deepbluembedded.com\\\/\",\"name\":\"DeepBlueMbedded\",\"description\":\"Embedded Systems And Computer Engineering Tutorials &amp; Articles\",\"publisher\":{\"@id\":\"https:\\\/\\\/deepbluembedded.com\\\/#\\\/schema\\\/person\\\/30259d66cf68c6c681562dbe551b7867\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/deepbluembedded.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/deepbluembedded.com\\\/#\\\/schema\\\/person\\\/30259d66cf68c6c681562dbe551b7867\",\"name\":\"Khaled Magdy\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a8b5dc7385ec807e129b584974394d27e41c9979f3a2d0fd785b552b73529032?s=96&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a8b5dc7385ec807e129b584974394d27e41c9979f3a2d0fd785b552b73529032?s=96&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a8b5dc7385ec807e129b584974394d27e41c9979f3a2d0fd785b552b73529032?s=96&r=g\",\"caption\":\"Khaled Magdy\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a8b5dc7385ec807e129b584974394d27e41c9979f3a2d0fd785b552b73529032?s=96&r=g\"},\"description\":\"Principal Embedded Systems Engineer with years of experience in embedded software and hardware design. I work in the Automotive &amp; e-Mobility industry. However, I still do Hardware design and SW development for DSP, Control Systems, Robotics, AI\\\/ML, and other fields I'm passionate about. I love reading, writing, creating projects, and teaching. A reader by day and a writer by night, it's my lifestyle. I believe that the combination of brilliant minds, bold ideas, and a complete disregard for what is possible, can and will change the world! I will be there when it happens, will you?\",\"sameAs\":[\"https:\\\/\\\/deepbluembedded.com\",\"https:\\\/\\\/www.facebook.com\\\/khaled.elrawy.359\\\/\",\"https:\\\/\\\/www.instagram.com\\\/deepbluembedded\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/khaled-magdy-\\\/\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCzLSrNZD4rCjSCOVU9hknvA\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Pulse Width Modulation - PWM Tutorial | DeepBlue Embedded Tutorials","description":"In this tutorial, you'll learn what is Pulse Width Modulation? What are the applications of PWM? And How to generate PWM signals with PIC MCU","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/deepbluembedded.com\/pwm-pulse-width-modulation-tutorial\/","og_locale":"en_US","og_type":"article","og_title":"Pulse Width Modulation - PWM Tutorial | DeepBlue Embedded Tutorials","og_description":"In this tutorial, you'll learn what is Pulse Width Modulation? What are the applications of PWM? And How to generate PWM signals with PIC MCU","og_url":"https:\/\/deepbluembedded.com\/pwm-pulse-width-modulation-tutorial\/","og_site_name":"DeepBlue","article_publisher":"https:\/\/www.facebook.com\/khaled.elrawy.359\/","article_author":"https:\/\/www.facebook.com\/khaled.elrawy.359\/","article_published_time":"2018-08-30T19:24:18+00:00","article_modified_time":"2023-08-17T20:53:09+00:00","og_image":[{"width":720,"height":340,"url":"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/pwm.jpg","type":"image\/jpeg"}],"author":"Khaled Magdy","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Khaled Magdy","Est. reading time":"17 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/deepbluembedded.com\/pwm-pulse-width-modulation-tutorial\/#article","isPartOf":{"@id":"https:\/\/deepbluembedded.com\/pwm-pulse-width-modulation-tutorial\/"},"author":{"name":"Khaled Magdy","@id":"https:\/\/deepbluembedded.com\/#\/schema\/person\/30259d66cf68c6c681562dbe551b7867"},"headline":"PWM &#8211; Pulse Width Modulation Tutorial | CCP Module","datePublished":"2018-08-30T19:24:18+00:00","dateModified":"2023-08-17T20:53:09+00:00","mainEntityOfPage":{"@id":"https:\/\/deepbluembedded.com\/pwm-pulse-width-modulation-tutorial\/"},"wordCount":3188,"commentCount":14,"publisher":{"@id":"https:\/\/deepbluembedded.com\/#\/schema\/person\/30259d66cf68c6c681562dbe551b7867"},"image":{"@id":"https:\/\/deepbluembedded.com\/pwm-pulse-width-modulation-tutorial\/#primaryimage"},"thumbnailUrl":"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/pwm.jpg","articleSection":["Embedded Systems","Embedded Tutorials","Microchip PIC"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/deepbluembedded.com\/pwm-pulse-width-modulation-tutorial\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/deepbluembedded.com\/pwm-pulse-width-modulation-tutorial\/","url":"https:\/\/deepbluembedded.com\/pwm-pulse-width-modulation-tutorial\/","name":"Pulse Width Modulation - PWM Tutorial | DeepBlue Embedded Tutorials","isPartOf":{"@id":"https:\/\/deepbluembedded.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/deepbluembedded.com\/pwm-pulse-width-modulation-tutorial\/#primaryimage"},"image":{"@id":"https:\/\/deepbluembedded.com\/pwm-pulse-width-modulation-tutorial\/#primaryimage"},"thumbnailUrl":"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/pwm.jpg","datePublished":"2018-08-30T19:24:18+00:00","dateModified":"2023-08-17T20:53:09+00:00","description":"In this tutorial, you'll learn what is Pulse Width Modulation? What are the applications of PWM? And How to generate PWM signals with PIC MCU","breadcrumb":{"@id":"https:\/\/deepbluembedded.com\/pwm-pulse-width-modulation-tutorial\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/deepbluembedded.com\/pwm-pulse-width-modulation-tutorial\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/deepbluembedded.com\/pwm-pulse-width-modulation-tutorial\/#primaryimage","url":"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/pwm.jpg","contentUrl":"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/pwm.jpg","width":720,"height":340,"caption":"pwm-pulse-width-modulation"},{"@type":"BreadcrumbList","@id":"https:\/\/deepbluembedded.com\/pwm-pulse-width-modulation-tutorial\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/deepbluembedded.com\/"},{"@type":"ListItem","position":2,"name":"PWM &#8211; Pulse Width Modulation Tutorial | CCP Module"}]},{"@type":"WebSite","@id":"https:\/\/deepbluembedded.com\/#website","url":"https:\/\/deepbluembedded.com\/","name":"DeepBlueMbedded","description":"Embedded Systems And Computer Engineering Tutorials &amp; Articles","publisher":{"@id":"https:\/\/deepbluembedded.com\/#\/schema\/person\/30259d66cf68c6c681562dbe551b7867"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/deepbluembedded.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/deepbluembedded.com\/#\/schema\/person\/30259d66cf68c6c681562dbe551b7867","name":"Khaled Magdy","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/a8b5dc7385ec807e129b584974394d27e41c9979f3a2d0fd785b552b73529032?s=96&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/a8b5dc7385ec807e129b584974394d27e41c9979f3a2d0fd785b552b73529032?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/a8b5dc7385ec807e129b584974394d27e41c9979f3a2d0fd785b552b73529032?s=96&r=g","caption":"Khaled Magdy"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/a8b5dc7385ec807e129b584974394d27e41c9979f3a2d0fd785b552b73529032?s=96&r=g"},"description":"Principal Embedded Systems Engineer with years of experience in embedded software and hardware design. I work in the Automotive &amp; e-Mobility industry. However, I still do Hardware design and SW development for DSP, Control Systems, Robotics, AI\/ML, and other fields I'm passionate about. I love reading, writing, creating projects, and teaching. A reader by day and a writer by night, it's my lifestyle. I believe that the combination of brilliant minds, bold ideas, and a complete disregard for what is possible, can and will change the world! I will be there when it happens, will you?","sameAs":["https:\/\/deepbluembedded.com","https:\/\/www.facebook.com\/khaled.elrawy.359\/","https:\/\/www.instagram.com\/deepbluembedded\/","https:\/\/www.linkedin.com\/in\/khaled-magdy-\/","https:\/\/www.youtube.com\/channel\/UCzLSrNZD4rCjSCOVU9hknvA"]}]}},"jetpack_featured_media_url":"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/08\/pwm.jpg","jetpack_shortlink":"https:\/\/wp.me\/p9SirL-zy","jetpack-related-posts":[{"id":3885,"url":"https:\/\/deepbluembedded.com\/servo-motor-control-with-pic-microcontrollers-pwm-pt1\/","url_meta":{"origin":2204,"position":0},"title":"Servo Motor Control With PIC Microcontrollers &#8211; PWM PT1","author":"Khaled Magdy","date":"November 13, 2019","format":false,"excerpt":"In this Article \/ Tutorial, we'll discuss how servo motor works. And how to control servo motor with pic microcontrollers. Why it's difficult to generate 50Hz PWM with CCP in PIC Microcontrollers. And what are the different design solutions to control servo motors with microcontrollers? This article will address the\u2026","rel":"","context":"In &quot;Articles&quot;","block_context":{"text":"Articles","link":"https:\/\/deepbluembedded.com\/embedded-systems\/embedded-systems-articles\/"},"img":{"alt_text":"Servo Motor Tutorial With PIC Microcontroller Part1 Thumbnail","src":"https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2019\/11\/Servo-Motor-Tutorial-PT1-Thumbnail.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2019\/11\/Servo-Motor-Tutorial-PT1-Thumbnail.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2019\/11\/Servo-Motor-Tutorial-PT1-Thumbnail.jpg?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2019\/11\/Servo-Motor-Tutorial-PT1-Thumbnail.jpg?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":3966,"url":"https:\/\/deepbluembedded.com\/servo-motor-control-with-pic-microcontroller-16bit-pwm-pt3\/","url_meta":{"origin":2204,"position":1},"title":"Servo Motor Control With PIC Microcontroller &#8211; 16Bit PWM PT3","author":"Khaled Magdy","date":"November 19, 2019","format":false,"excerpt":"Previous Tutorial Tutorial 32 Next Tutorial Servo Motor Control With PIC Microcontrollers | 16Bit PWM Intermediate Level \u2605\u2605\u2606\u2606\u2606 \u00a0 Servo Motor Control Tutorial - PART 3 \u00a0 In this Article \/ Tutorial, we'll discuss how to control servo motor with PIC microcontroller using 16-Bit High-Resolution PWM signals. This depends on\u2026","rel":"","context":"In &quot;Articles&quot;","block_context":{"text":"Articles","link":"https:\/\/deepbluembedded.com\/embedded-systems\/embedded-systems-articles\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2019\/11\/Servo-Motor-Tutorial-PT3-Thumbnail.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2019\/11\/Servo-Motor-Tutorial-PT3-Thumbnail.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2019\/11\/Servo-Motor-Tutorial-PT3-Thumbnail.jpg?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2019\/11\/Servo-Motor-Tutorial-PT3-Thumbnail.jpg?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":9026,"url":"https:\/\/deepbluembedded.com\/arduino-pwm-analogwrite-tutorial\/","url_meta":{"origin":2204,"position":2},"title":"Arduino PWM analogWrite() Tutorial &#8211; Analog Output","author":"Khaled Magdy","date":"May 1, 2023","format":false,"excerpt":"In this tutorial, you'll learn how to use Arduino PWM analog output pins using the analogWrite() function. We'll start from the basics of PWM signal, its frequency, duty cycle, and resolution, and discuss in detail how it works and how to use it in various Arduino control projects. Pulse Width\u2026","rel":"","context":"In &quot;Arduino&quot;","block_context":{"text":"Arduino","link":"https:\/\/deepbluembedded.com\/embedded-systems\/embedded-tutorials\/arduino\/"},"img":{"alt_text":"Arduino PWM analogWrite Tutorial","src":"https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2023\/05\/Arduino-PWM-analogWrite-Tutorial.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2023\/05\/Arduino-PWM-analogWrite-Tutorial.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2023\/05\/Arduino-PWM-analogWrite-Tutorial.jpg?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2023\/05\/Arduino-PWM-analogWrite-Tutorial.jpg?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2023\/05\/Arduino-PWM-analogWrite-Tutorial.jpg?resize=1050%2C600&ssl=1 3x"},"classes":[]},{"id":9220,"url":"https:\/\/deepbluembedded.com\/arduino-led-dimmer\/","url_meta":{"origin":2204,"position":3},"title":"Arduino LED Dimmer (Potentiometer + PWM)","author":"Khaled Magdy","date":"May 2, 2023","format":false,"excerpt":"In this project tutorial, we'll create an Arduino LED Dimmer Project Using Potentiometer & PWM (analog output). You'll learn how PWM works, and how to create a LED Dimmer with Arduino controlled by a potentiometer. And we'll simulate and run the project code example to test its functionality. Without further\u2026","rel":"","context":"In &quot;Arduino Projects&quot;","block_context":{"text":"Arduino Projects","link":"https:\/\/deepbluembedded.com\/embedded-systems\/embedded-projects\/arduino-projects-category\/"},"img":{"alt_text":"Arduino LED Dimmer Project","src":"https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2023\/05\/Arduino-LED-Dimmer-Project.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2023\/05\/Arduino-LED-Dimmer-Project.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2023\/05\/Arduino-LED-Dimmer-Project.jpg?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2023\/05\/Arduino-LED-Dimmer-Project.jpg?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2023\/05\/Arduino-LED-Dimmer-Project.jpg?resize=1050%2C600&ssl=1 3x"},"classes":[]},{"id":3924,"url":"https:\/\/deepbluembedded.com\/servo-motor-control-with-pic-microcontroller-software-pwm-pt2\/","url_meta":{"origin":2204,"position":4},"title":"Servo Motor Control With PIC Microcontroller &#8211; Software PWM PT2","author":"Khaled Magdy","date":"November 14, 2019","format":false,"excerpt":"Previous Tutorial Tutorial 31 Next Tutorial Servo Motor Control With PIC Microcontrollers | Software PWM Intermediate Level \u2605\u2605\u2606\u2606\u2606 \u00a0 Servo Motor Control Tutorial - PART 2 \u00a0 In this Article \/ Tutorial, we'll discuss how to control servo motor with PIC microcontroller using the Software PWM technique. We'll generate a\u2026","rel":"","context":"In &quot;Articles&quot;","block_context":{"text":"Articles","link":"https:\/\/deepbluembedded.com\/embedded-systems\/embedded-systems-articles\/"},"img":{"alt_text":"Servo Motor Tutorial With PIC Part2 - Thumbnail","src":"https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2019\/11\/Servo-Motor-Tutorial-With-PIC-Part2-Thumbnail.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2019\/11\/Servo-Motor-Tutorial-With-PIC-Part2-Thumbnail.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2019\/11\/Servo-Motor-Tutorial-With-PIC-Part2-Thumbnail.jpg?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2019\/11\/Servo-Motor-Tutorial-With-PIC-Part2-Thumbnail.jpg?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":9200,"url":"https:\/\/deepbluembedded.com\/arduino-fading-led\/","url_meta":{"origin":2204,"position":5},"title":"Arduino Fading LED (Analog PWM Output)","author":"Khaled Magdy","date":"May 2, 2023","format":false,"excerpt":"In this project tutorial, we'll create an Arduino Fading LED Project using PWM (analog output). You'll learn how PWM works, and how to create a LED Fading effect with Arduino (Fade-in and Fade-out effects). And we'll simulate and run the project code example to test its functionality. Without further ado,\u2026","rel":"","context":"In &quot;Arduino Projects&quot;","block_context":{"text":"Arduino Projects","link":"https:\/\/deepbluembedded.com\/embedded-systems\/embedded-projects\/arduino-projects-category\/"},"img":{"alt_text":"Arduino Fading LED PWM Output Project","src":"https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2023\/05\/Arduino-Fading-LED-PWM-Output-Project.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2023\/05\/Arduino-Fading-LED-PWM-Output-Project.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2023\/05\/Arduino-Fading-LED-PWM-Output-Project.jpg?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2023\/05\/Arduino-Fading-LED-PWM-Output-Project.jpg?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2023\/05\/Arduino-Fading-LED-PWM-Output-Project.jpg?resize=1050%2C600&ssl=1 3x"},"classes":[]}],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/deepbluembedded.com\/wp-json\/wp\/v2\/posts\/2204","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/deepbluembedded.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/deepbluembedded.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/deepbluembedded.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/deepbluembedded.com\/wp-json\/wp\/v2\/comments?post=2204"}],"version-history":[{"count":28,"href":"https:\/\/deepbluembedded.com\/wp-json\/wp\/v2\/posts\/2204\/revisions"}],"predecessor-version":[{"id":3447,"href":"https:\/\/deepbluembedded.com\/wp-json\/wp\/v2\/posts\/2204\/revisions\/3447"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/deepbluembedded.com\/wp-json\/wp\/v2\/media\/2443"}],"wp:attachment":[{"href":"https:\/\/deepbluembedded.com\/wp-json\/wp\/v2\/media?parent=2204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/deepbluembedded.com\/wp-json\/wp\/v2\/categories?post=2204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/deepbluembedded.com\/wp-json\/wp\/v2\/tags?post=2204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}