Basic – Pulse Width Modulation (PWM)

PWM is a powerful and commonly used technique for controlling the analog circuits or power to inertial electrical devices and made practical by a processor’s digital output such as the modern electronic power switches.

BY: Lim Keng Hui

PWM is a powerful and commonly used technique for controlling the analog circuits or power to inertial electrical devices and made practical by a processor’s digital output such as the modern electronic power switches.

1.0 Introduction

A digital device like a microcontroller can easily work with inputs and outputs. It has only two states, on and off. So you can use it to control a LED’s state such as on or off. In the same way, you can use it to control any electrical device on and off by using proper drivers such as transistor, relays, etc. But sometimes you need more than just “on” and “off” control over the device. For example you want to control the brightness of a LED or any lamp, or the speed of DC motor, then the digital signal (on/off) simply can’t do it. This situation is very smartly handled by a technique called PWM or Pulse Width Modulation. PWM is the technique used to generate analogue signals from a digital device like a MCU. Digital control is used to create a square wave, a signal switched between on and off. This on-off pattern can simulate voltages in between full on (5V) and off (0V) by changing the portion of the time the signal spends on versus the time that the signal spends off. The duration of “on time” is called the pulse width. To get varying analog values, you might change or modulate the pulse width. If you repeat this on-off pattern fast enough with an LED for example, the result is as if the signal is a steady voltage between 0V and 5V controlling the brightness of the LED.

2.0 How Are They Used?

PWM is using digital pulses to create some analog value other than just ‘high’ and ‘low’ signal levels. Most of the digital systems are powered by 5V power supply. So for example, if you filter a signal that has 50% duty cycle, you will get an average voltage of 2.5V. Other duty cycles produce any voltage in the range of 0-100% of the ‘high’ voltage is depending upon the PWM resolution. The duty cycle is defined as the percentage of digital ‘high’ to digital ‘low’ signals present during a PWM period. The PWM resolution is defined as the maximum number of pulses that you can pack into a PWM period. The PWM period is an arbitrarily time period in which PWM takes place. It is chosen to give the best results for your particular use.

Duty cycle

Figure 1: Duty Cycle

By controlling analog circuits digitally, system costs and power consumption can be drastically reduced. PWM is a way of digitally encoding analog signal levels. Through the use of high-resolution counters, the duty circle of a square wave is modulated to encode a specific analog signal level. PWM signal is still digital because at any given instant of time, the full DC supply is either fully on or off. The voltage or current source is supplied to the analog load by means of a repeating series of on and off pulse. The on-time is the time during which the DC supply is applied to the load, and the off-time is the periods during which that supply is switched off. Given a sufficient bandwidth, any analog value can be encoded with PWM.

Figure 2 below shows three different PWM signals. Figure 2a shows a PWM output at a 10% duty cycle. It means that the signal is on for 10% of the period and off the other 90%. Figure 2b shows the PWM outputs at 50%, means that the signal is on for 50% of the period and off the other 50%. Figure 2c shows the PWM outputs at 90%, means that the signal is on for 90% of the period and off the other 10%. These three PWM outputs encode three different analog signal values which are at 10%, 50% and 90% of the full strength. For example, if the supply given is 9V, and the duty cycle is 10%, then the analog signal results will come out with 0.9V.

PWM signals of varying duty cycles

Figure 2: PWM signals of varying duty cycles

Figure 3 shows a simple circuit that could be driven using PWM. In the figure, a 9V battery powers an incandescent light bulb. If we closed the switch and connecting the battery and lamp for 50ms, the bulb would receive 9V during that interval. If we opened the switch for next 50ms, the bulb would receive 0V. If we keep repeating this cycle 10times a second, the bulb will be lit as though it were connected to a 4.5V battery. It means that it is 50% of the battery supply (9V). We can say that the duty cycle is 50% and the modulating frequency s 10Hz.

A simple PWM circuit

Figure 3: A simple PWM circuit

Most of the loads, inductive and capacitive are alike. They require a much higher modulating frequency than 10Hz. Lets imagine that our lamp was switched on and off for five seconds. If we keep repeating these steps, the duty cycle would still be 50%, but the bulb would appear brightly lit for the first five seconds and off for the next. In order for the bulb to see a voltage of 4.5 volts, the cycle period must be short relative to the load’s response time to a change in the switch state. To achieve the desired effect of a dimmer lamp, it is necessary to increase the modulating frequency. The same is true in other applications of PWM. Common modulating frequencies range from 1 kHz to 200 kHz.

3.0 Types of PWM

There are three types of Pulse Width Modulation (PWM):

  1. The pulse center may be fixed in the center of the time window and both edges of the pulse moved to compress or expand the width.
  2. The lead edge can be held at the lead edge of the window and the tail edge modulated.
  3. The tail edge can be fixed and the lead edge modulated.


Figure 4: Three types of PWM

Figure 4 on top shows the three types of PWM signals (blue): leading edge modulation (top), trailing edge modulation (middle) and centered pulses which both edges are modulated (bottom). The green lines are the sawtooth waveform (first and second cases) and a triangle waveform (third case) used to generate the PWM waveforms. Sawtooth waveform means the waveform is a repeating waveform that rises from zero to maximum value linearly drops back to zero and repeats.

4.0 Examples of using PWM

4.1 How to Control LED Brightness with A Simple PWM Circuit

Figure5 below shows a simple circuit for generating variable duty-cycle PWM. For testing purposes, it is best to start with a safe, small and simple load. Then we will start by adjusting the brightness of a single LED with a PWM wave.


Figure 5: Schematic of an LED connected to the square wave output of the PWM circuit

In Figure 5, we will start with the connection of a 180 ohm resistor and a LED of any color to pin4 of 74AC14 chip. Turning the potentiometer to dial changes the amount of “on time” the LED receives, thus changing the brightness. If the LED is not in “ON” position, check to see if it is in backwards. If the LED is too dim regardless of the potentiometer setting, you can use a lower resistor in series with the LED. Or, perhaps you are using a weaker output chip like the 74HC14, instead of the recommended 74AC14. The potentiometer dial may not work the way we are expecting, as turning it to the left will make the LED brighter, not dimmer. This is because the output of the first inverter logic gate is being inverted again by the second logic gate. That is, the output is the opposite of the original signal.

4.2 How to Set the PWM using PIC microcontroller

Regarding the setup for PWM operation, the following steps should be taken when configuring the CCP module for PWM operation:

  1. Set the PWM period by writing to the PR2 register.
  2. Set the PWM duty cycle by writing to the CCPR1L register and CCP1CON<5:4> bits.
  3. Make the CCP1 pin an output by clearing the TRISC<2>bit.
  4. Set the TMR2 prescale value and enable Timer2 by writing to T2CON.
  5. Configure the CCP1 module for PWM operation.

In this example, we would like to use the microcontroller PIC16F877A. The sample source code below shows how to set the speed of a DC motor using PWM with PIC16F877A. First, we need to initialize the CCP1 module to operate in PWM mode. Next we need to use a formula to calculate PR2: PR2 = PWM period / (4xToscxTMR2 prescale). PWM period = 1/frequency. We set frequency = 4.88 kHz. So, the PR2 = 256.147, but the maximum could be reach is 255, so at the end, it will automatically go to 255 in decimal and FFh in hexadecimal. So the maximum speed that the motor can reach is 255 in CCPR1L. For example if CCPR1L = 0, that means the speed is 0% of the full speed, if CCPR1L = 255, that means the speed is 100% of the full speed.


After initialize the CCP1 module to operate PWM mode, we now can write the program for control the speed of DC motor. M1 and M2 are connecting to a DC motor. Program below shows how to increase and decrease the speed of a DC motor.


Complete source code can be found on the attachment below.

Figure 6 below shows the connection between SK40C and motor driver.


Figure 6: hardware part of the motor driver and SK40C board

Figure 7 below shows the schematic For the SK40C with motor driver.


Figure 7: Schematic for Motor Driver with SK40C

4.3 How to Control Motor Speed with A PWM Circuit

PWM is an effective method for adjusting the amount of power delivered to an electrical load. A simple circuit containing an inverter chip, diodes, trim pot, and capacitor creates the variable duty-cycle PWM. A resistor and transistor switch heavier loads than 74AC14 chip that can drive by itself. For example, if you have a nice DC gear motor for your robot and the gear motor is marginally too fast for debugging the robot, or perhaps even too fast for final usage. For debugging, you would like the motor to run as slowly as 1/3 or 1/4 of the full speed. For solving this problem, PWM is the effective way to controlling the speed of a motor.


Figure 8: Schematic of a PWM controlling the speed of a motor

The speed of the motor can be controlled by the duty cycle of the square wave. There are two differences between the driver portion of this circuit and the LED circuit:

– The transistor, Q3, should be more powerful than a 2N3904. A 2N2222 is suitable for smaller motors.

– A diode, D3, has been added to reroute inductive motor spikes. A 1N914 or 1N3001 is suitable for smaller motors.

In figure 8, we can know that the motor can receive 12V even though the 74AC14 logic chip is only powered by 5V. This is possible because the logic chip output feeds into the resistor of the transistor, and not directly to the motor. The resistor, transistor, and diode are all help to isolate the logic voltages from the motor voltages.

By using this PWM circuit, it can able to change the speed of the gear motor from 145 RPM at 5V to as little as 0.18 RPM at 5V. As you can imagine, the power usage dropped as well since the motor was off for much of the time. In fact, to get the gear motor to run slowly, you should reduce the PWM frequency to only 100Hz by substituting a 1 µF capacitor for C2. This gave each “on” pulse enough time to power up the motor for a slight movement before pausing for the “off” time.

Another interesting trick for PWM motor control is to apply twice the standard voltage to the motor. Normally, the PWM is run at 50% of the duty cycle so that the overall motor speed is unchanged (twice the voltage but half the “on” time). But, with the added voltage, the robot builder can now adjust the motor speed above or below the normal speed. A weird aspect of PWM on motors is that, it can create audible whining. Basically, if you select a PWM frequency in a human-audible range, the mechanical device will likely oscillate audibly. By increasing the frequency above 20 kHz, it may silence the motor whining. But, some motors, transistors or motor driver chips are unable to switch on and off that quickly. Last but not least, you will need to experiment to select the correct frequency for your particular motor, mounting system, semiconductors and load. At here, I selected 1 kHz because it is likely to fork for most readers’ motors and even if it isn’t quiet or electrically optimal.

5.0 Conclusion

As a conclusion, we can know that it is not difficult to use PWM to control the brightness of the LED and the speed of the motors. These all circuitry is superior of using a fixed or variable resistor for heavy or varying electrical load such as motors and LED displays. A microcontroller based PWM solution uses fewer components and has the flexibility of varying the duty cycle and frequency. This can be an advantage in mini sumo battle, ,where searching might be performed at a slower motor speed, but the duty cycle needs to be increased to 100% “on” for pushing an opponent. In the other way, PWM is the least expensive way to get an analog voltage output from a microcontroller. Some other is use to operate relays and solenoids that require high ‘pull-in’ current and more moderate ‘hold’ current. There is also having additional ways to improve PWM, such as using LC filters. Although they cost more, they will not drop the voltage as do RC filters.


1. Sample Code – SK40C 16F877A PWM


Leave a Comment

Your email address will not be published.

Share this Tutorial

Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest
Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest

Latest Tutorial

BLTouch Installation for Ender 3 with 32-bit V4.2.2 Board
Pick and Send Random Meal’s Option and Locations through Telegram Bot Using Grove WiFi 8266 on micro:bit
DIY Automated Vacuum Cleaner Using REKA:BIT With Micro:bit
Rainbow Spark in Mini House using Maker Uno.
TinyML on Arduino using Edge Impulse
Tutorials of Cytron Technologies Scroll to Top