Home / Softwares Tips & Tricks / MATLAB / Generate PWM signal in MATLAB Simulink of desired frequency

Generate PWM signal in MATLAB Simulink of desired frequency

In this article you will learn how to generate PWM signal in MATLAB Simulink.  This article will help the user to vary the frequency and duty cycle of PWM waves according to the requirement. All the required details like blocks, connection diagram, setting of block parameters and simulation will be discussed in detail.


PWM stands for pulse width modulation. It is a periodic square wave which controls the power delivery. It has a variety of applications in electrical and electronics engineering. It is a special method of delivering controlled power to equipment without dissipating it in other devices. PWM waves are generated by quickly turning on & off the power switch such that device only feels its effect. There are few terminologies which must be learnt in order to understand PWM.

Frequency:  It is the number of cycles per unit second. In terms if PWM, it is the amount of times switch  opens and closes in one second. The frequency is set according the requirements.

Duty Cycle: Duty cycle shows the amount of time the signal is high or switch is on. Mathematically It is the ratio of the on time to the total period of one cycle.

Duty cycle is the proportion of time during which a component, device, or system is On or operating. Duty cycle can be expressed as a ratio or as a percentage. Consider a bulbs glows for 1 second and then it shuts for continuous 9 seconds. After that it again glows for 1 second and shuts again for 9 seconds and so on. So the bulb glows for 1 seconds out of 10 seconds or 1/10 of the time. The duty cycle is therefore 10%. Following figure shows the various duty cycles.

PWM shape

The effectiveness of PWM and it duty cycle can be seen from one of its most common application which is  speed of DC motor with PWM.

Need to generate PWM signal in MATLAB Simulink manually.

MATLAB is one of the most widely used simulation & programming software used by Engineers of different fields. It has a rich library enabling the user to perform variety of simulations and analysis. However not every type of functions and blocks are available readily. For example there is no block in the MATLAB Simulink which can produce PWM waves. Therefore available building blocks must be used to generate PWM signal in MATLAB Simulink of desired frequency and Duty Cycle.

Building Blocks

Following blocks are used to generate PWM signal n MATLAB Simulink.

  • Repeating Sequence: This block generates a sawtooth wave. The Frequency of the waveform can be adjusted in the parameter settings of the block.
  • Constant: it gives a constant output number.
  • Relational operator. It compares two values at its input and gives output according to its settings. It can perform greater, less, equal and many other such logical operations.
  • Data Type conversion: This block usually converts one data type to another one. It makes different blocks compatible to each other. For example if one block works on Boolean data type while other on double, this block can be used to convert Boolean type output to double type and make it compatible with other block.
  • Multiplexer: It combines different signals and overlap them on each other. Its output can be seen on the scope.
  • Scope: This block displays the signal against the time.

block diagram to Generate PWM signal in MATLAB Simulink


The above described blocks works as follows. Sawtooth wave is generated. It is than compared with the constant by the comparator. If the value sawtooth is higher than constant, output is high else it is low. The output of this is a PWM wave however it may not be compatible with many other blocks. Therefore it is converted from Boolean to double data type to make it compatible with most of the blocks. In order to show the comparison, different signals are overlapped using mux and displayed on scope.

Simulation results

Simulation results PWM 1

Simulation results PWM 2

How to vary frequency of PWM Signal

In order to vary the frequency, the parameters of repeating sequence block needs to be changed.

vary frequency PWM

As shown in the above figure, first slot can be used adjust time period. The time slot specifies the time period. Difference between two values in time slot gives the time period. In given figure, first value is 0 and second value is 0.2 so time period is 0.2. The frequency can therefore be calculated as

f= 1/T = 1/0.2 = 5Hz.

How to vary duty cycle of PWM Signal

In order to vary the duty cycle the value of constant needs to be varied. Its minimum value can be 0 and maximum value can be 1. If the value of 1 set, the duty cycle of PWM will be 0 or 0%. Conversely if the constant value is taken as 0, the duty cycle is 1 or 100%. The relation between the duty cycle and constant is inverse that is the higher the value of constant, the lower will be the duty cycle and vice versa.

Duty cycle = 1- constant value                               ( 0 <= Constant value <= 1)

For example if value of constant is 0.2

Duty cycle = 1 – 0.2 = 0.8 or 80%

About Syed Noman ud din

Syed Noman ud din is an Electrical Engineer and working in Industry from last 3 years. He writes technical articles for electrical and electronic engineers. He has also published several research publications in renowned international journals.

Check Also

Block diagram-Modelling of overcurrent relay in Simulink MATLAB

Modeling of overcurrent relay in Simulink MATLAB

This article discusses the modeling of overcurrent relay in simulink MATLAB. Which Blocks are used for …

Leave a Reply

Your email address will not be published. Required fields are marked *