Pulse Width Modulation PWM fully Explained with calculation and Arduino LED Example

(Last Updated On: February 27, 2021)

Pulse width modulation PWM:

PWM stands for pulse width modulation which consists of a square wave with the help of which we can control the up or high time. It is simple but digital way to control the digital signals that we use to vary the energy that is send to a load or to encode information within the signal.  The wave oscillation can be limit by using PWM signal maximum and minimum voltage values. The space between the maximum and minimum value is called amplitude. A cycle is the interval of the wave where you can find one full repetition the time a cycle takes to finish is called period. The frequency is 1 over a time period which gives you how many cycles are in a time unit.

pulse width modulation


Duty cycle:

A very important concept in PWM is the duty cycle the duty cycle represents how much of the period in which the signal is high in our wave the signal here is high and here is low to calculate a duty cycle you need to know how much of the period the signal is high. Let us set our high time as 6 milliseconds our down time as 4 milliseconds and our period as 10. Now let’s use a simple rule to calculate the percentage of the period in which the signal is high 6 relates to 10 the same way as our variable in percentage relates to 100%.

Duty cycle= 6/10 x 100

Duty cycle = 60%

We get 60% meaning that the signal is high 60% of the time. Let us consider that we have a PWM signal that oscillates between 0 and 5 volts let’s say this signal has a duty cycle of 50% something interesting will happen to the output voltage instead of being 5 volts is expected it will now be 2.5 volts simply putting it.

Vout = Vmax * duty cycle

Vout = 5 x 0.5

Vout = 2.5V

When I increase the duty cycle the output voltage also increases and when I decrease the duty cycle the output voltage also decreases keep in mind that the output voltage can only oscillate between the minimum and the maximum of the PWM signal.

So, here by changing the duty cycle of the waveform, we are actually changing the average value of the output waveform. And by doing so, we are actually changing the average voltage which is applied to the load. So, by changing this duty cycle, we can control the different devices.



Generation of the PWM signal:

The simple way of generating this PWM output signal is using the comparator circuit. So, here the triangular wave is applied at the non-inverting node. While the control voltage is applied at the inverting node of the comparator. So, whenever this triangular wave voltage is less than the control voltage, then the output of the comparator will be low. Whenever the voltage of this triangular wave just goes above this control voltage, then the output of the comparator will become high. So, depending on the value of this control voltage, the width of the pulse can be changed. So, suppose if we reduce this control voltage, in that case, the width of the output pulse will increase.

pulse width modulation

In this way by controlling this voltage it is possible to change the pulse width of the output waveform. So, this is a very simple way by which we can generate this PWM output signal.

pulse width modulation

Apart from that this signal can also be generated using the 555 timer IC. And we have already discussed this circuit whenever we have talked about the astable mode of operation of the 555 timer IC. So here, by changing the value of the resistance R1, it is possible to change the duty cycle of the output waveform. This circuit can be used for generating the PWM output signal.

Duty cycle = R1 / R1 + R2

pulse width modulation

Apart from these methods, it is also possible to generate this PWM output signal using the microcontrollers. So, suppose if someone uses the microcontroller board like Arduino, then it has dedicated output pins for the PWM output signals. So, using these pins, it is possible to generate the PWM output signal. And using this PWM output waveform, we can control the different devices.


Controlling led with the PWM:

In this case we are going to pulse width modulating an LED which means that we are pulsing the power supply on and off at a certain frequency and with a certain pulse width. Pulse width modulation allows us to control the brightness of lighting whether that’s a standard bulb or LEDs or it allows us to control the speed of motors. Pulse width modulation also allows us to make very efficient power supplies such as step up step down and regulated power supplies all known as switch mode power supplies. The switch mode in the name refers to the pulse width modulation technique now bear in mind that every circuit does not run fast enough for me to show us what the LED would do but in this case the LED would actually dim and brighten depending on the pulse width that we are sending to the LED.

The microcontroller that is sending pulses at a certain frequency and at a certain width turning a power supply on and off. So when this power supply is turned on it is sending current to the LED. So for every on pulse there is three volts and 20 milliamps current going through this LED and for every off or low pulse there is no current flowing through the circuit. In this case 50% means that current is flowing through the circuit 50% of the time and current is not flowing through the circuit 50% of the time so that “on time” is equal to the “off time”.   If we increase the pulse width to 80% we can see the result on the graph at the top. Current flowing through the circuit is now “ON” more of the time versus the off time. So the current flowing through the circuit is flowing 80% of the time during the pulse width and no current flows through the circuit for only 20% of the time. If we further increase the pulse width to 100% you can see what’s happened because the pulse width is 100% current is flowing through the circuit 100% of the time. So there is now no point in time where the circuit is turned off. If I decrease the pulse width to 10% we can see that the circuit is off most of the time and only pulses on very quickly 10% of the time. If we change the pulse width to 0% we can see that the circuit remains off all of the time the other thing to look at here is the frequency at which we are pulsing this circuit currently we are pulsing it at one kilohertz if we increase the frequency of these pulses we can see that the pulses get much faster and appear shorter on the graph although this is still 50% pulse width modulation it is on 50% of the time and off 50% of the time. But now the frequency has gone from one kilohertz all the way to 10 kilohertz and this means that we are pulsing this circuit 10,000 times per second. We can also slow it down by decreasing the frequency we can now see that the frequency is 502 Hertz which means that this would be pulsing 502 times per second in this case one-way pulse width thing in LED if we pulse it too slow we would be able to see the visible pulses which would not be ideal so let’s go on to a visual example of pulse width modulation using an Arduino to dim and brighten an LED.

In our example we are applying pulse width modulation on an LED for that so we have got the Arduino microcontroller which is sending pulse width modulation to this LED and connect resistor with it. So in this case the potentiometer is used as a voltage divider so that we can get a digital value from 0 to 255 to change the pulse width to the LED.

pulse width modulation

This can be done to control motors and even to create very efficient power supplies so just note that this LED is not being dimmed in an analogue fashion with a variable resistor its dimming through pulse width modulation through this microcontroller which is sending those on and off pulses to the LED. It is just that the on and off pulses are so fast within the kilohertz range meaning it is pulsing at thousands of times per second so the pulses are not visible to the human eye but the effect of the dimming and brightening of the LED is visible to the human eye because the LED in this case when it is dimmer is off more of the time and when it is brighter is on more of the time so in effect it is changing the average voltage to that LED and the good thing about this is that I can apply this not only to one led but I could use a series of transistors to apply this to much bigger loads like huge high power motors.  I can send a digital pulse width signal to speed the motor up and down but we will get into things.


PWM Arduino code:

PWM code Explanation:

We have to create a program to control the PWM port assign the port that you connected your lighting to into a variable pwm and assign it to pin number 3. Now create byte variable with pwmvalue. The reason I chose a byte variable is because the PWM in the Arduino has a resolution of 8 bits so you have 8 bits or a byte to encode the value of your duty cycle zero means zero percent and 225 means 100%. In order to control the PWM port in an Arduino you use the analog write function the first parameter is the number of the pin with which you want to control and the second is the value of the duty cycle ranging from 0 to 225 we want the PWM value to slowly increase with time for that we will increment the variable each time the program goes through the loop. In order for this process to be slow we will have to introduce a little delay 20 milliseconds should do now we do not have to reset the byte value because when it reaches its maximum or 225.


Applications of the Pulse width modulation:

The pulse width modulation technique is used to control the analog devices and mainly the power which is delivered to the analog device is controlled using this technique. Now, because of it have high efficiency, low power loss and its ability to precisely control the power, this technique is used in many applications. For example, today for dimming the LED lights, or for controlling the fan speed of the CPU, this PWM technique is preferred over the conventional methods and in some laptops, even the brightness of the screen is also controlled using this pulse width modulation technique.

Recommended For You

About the Author: Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

%d bloggers like this: