{"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 – Pulse Width Modulation Tutorial | CCP Module"},"content":{"rendered":"\n\n\n\n\n
\"Previous<\/a><\/td>\nPrevious Tutorial<\/strong><\/a><\/td>\nTutorial 15<\/span><\/strong><\/span><\/td>\nNext Tutorial<\/strong><\/a><\/td>\n\"Next<\/td>\n<\/tr>\n
<\/td>\nPWM\u00a0Tutorial | CCP Modules<\/strong><\/span><\/td>\n<\/td>\n<\/tr>\n
<\/td>\nIntermediate Level\u00a0\u2605\u2605\u2606\u2606\u2606<\/strong><\/span><\/td>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

 <\/p>\n

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 Microcontrollers. We’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’ll hook an LED to our PIC Microcontroller chip and create an LED Dimmer application. So let’s get started!<\/span><\/p>\n

[toc]<\/p>\n


\n

\u00a0 \u00a0Required Components\u00a0 \u00a0<\/span><\/strong><\/span><\/h3>\n\n\n\n\n\n\n\n\n\n\n\n\n\n
Qty.<\/strong><\/span><\/td>\nComponent Name<\/strong><\/span><\/td>\nBuy<\/strong> On Amazon.com<\/span><\/td>\n<\/tr>\n
1<\/td>\nPIC16F877A<\/span><\/td>\nAdd<\/a><\/td>\n<\/tr>\n
1<\/td>\nBreadBoard<\/span><\/td>\nAdd<\/a><\/td>\n<\/tr>\n
1<\/td>\nLED<\/span><\/td>\nAdd<\/a>\u00a0 \u00a0 Add<\/a><\/td>\n<\/tr>\n
1<\/td>\nResistors Kit<\/span><\/td>\nAdd<\/a>\u00a0 \u00a0 Add<\/a><\/td>\n<\/tr>\n
1<\/td>\nCapacitors Kit<\/span><\/td>\nAdd<\/a>\u00a0 \u00a0 Add<\/a><\/td>\n<\/tr>\n
1<\/td>\nJumper Wires Pack<\/span><\/td>\nAdd<\/a>\u00a0 \u00a0 Add<\/a><\/td>\n<\/tr>\n
1<\/td>\nLM7805 Voltage Regulator (5v)<\/span><\/td>\nAdd<\/a><\/td>\n<\/tr>\n
1<\/td>\nCrystal Oscillator<\/span><\/td>\nAdd<\/a><\/td>\n<\/tr>\n
1<\/td>\nPICkit2 or 3 Programmer<\/span><\/td>\nAdd<\/a><\/td>\n<\/tr>\n
2<\/td>\n9v Battery or DC Power Supply<\/span><\/td>\nAdd<\/a>\u00a0 \u00a0 Add<\/a>\u00a0 \u00a0 Add<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

The Prototyping Board Setup<\/a><\/strong><\/p>\n

\"Prototyping<\/a><\/p>\n


\n

\u00a0 \u00a0What Is A PWM Signal?\u00a0 \u00a0<\/span><\/strong><\/span><\/h3>\n

 <\/p>\n

PWM<\/strong> stands for Pulse Width Modulation. This feature provides microcontrollers by a mean of, seemingly<\/strong>, outputting analog values of voltage between (0-5)v. Instead of outputting digital values that are either Low<\/strong> (0v) or High<\/strong> (5v). The PWM signal plot looks something like as shown in the diagram below.<\/span><\/p>\n\n\n\n\n
\"PWM-signal-with-variable-duty-cycle\"<\/td>\n<\/tr>\n
image source\u00a0https:\/\/www.elprocus.com<\/a><\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

The plot for this signal captures two features: (Frequency & Duty Cycle). And we’ll discuss each of them in more detail hereafter.<\/span><\/p>\n

\u00a0PWM Frequency\u00a0<\/span><\/h4>\n

As you know from elementary physics that a frequency of a signal is a measure of how many cycles are completed each second. It’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’re controlling the frequency of the output signal. In fact, that’s what we’ll be doing hereafter in this tutorial’s LAB.<\/span><\/p>\n

\u00a0PWM Duty Cycle\u00a0<\/span><\/h4>\n

The duty cycle is a measure for how long does the output line stays ON (High) to the cycle’s full-time period as a ratio. Which means the duty cycle can be easily determined using the following formula.<\/span><\/p>\n

\"PWM<\/p>\n

PWM Signal’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

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\n\n\n\n
\"pwm<\/td>\n<\/tr>\n
image source\u00a0http:\/\/www.pyroelectro.com<\/a><\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

This analogy while not being 100% accurate in a theoretical sense. But practically, it’s the way we add sweet & 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>\n


\n

\u00a0 \u00a0What Are The Applications Of PWM?\u00a0 \u00a0<\/span><\/strong><\/span><\/h3>\n

 <\/p>\n

There are numerous situations in which it’s ideal to have a PWM output signal. That’s why there are countless applications which are mainly dependent on the pulse width modulation technique. I’m going to introduce some of them down below:<\/span><\/p>\n