Example Code

RF Simulation Demo: Pulse Width Modulation

Code and Documents




This example shows you how to encode analog signal levels digitally. These signals are digitally encoded by creating a square wave of a particular duty cycle, which corresponds to an analog signal level. 




The PWM is also often called Pulse Duration Modulation and, less often, Pulse Length Modulation. In this system, we have fixed amplitude and starting time of each pulse, but the width of each pulse is made proportional to the amplitude of the signal at that instant. In PWM there may be a sequence of signal sample amplitudes of 0.9, 0.5, 0 and -0.4V. These can be represented by pulse widths of 1.9, 1.5, 1.0, and 0.6 microseconds respectively. The width corresponding to zero amplitude was chosen in this system to be 1.0 microseconds and it has been assumed that signal amplitude at this point will vary between the limits of +1V and -1V. Zero amplitude is thus the average signal level, and the average pulse width of 1 microsecond has been made to correspond to it. In this context, a negative pulse width is not possible. It would make the pulse end before it began, as it were, and thus throw out the timing in the receiver. If the pulses in a practical system have a recurrence rate of 8000 pulses per second, the time between the commencement of adjoining pulses is 106 / 8000 = 125 microseconds. This is adequate not only to accommodate the varying widths but also to permit time division multiplexing.


Pulse Width Modulation has the disadvantage, when compared with Pulse Position Modulation (PPM), that its pulses are of varying width and therefore of varying power content. This means that the transmitter must be powerful enough to handle the maximum-width pulses, although the average power transmitted is perhaps only half of the peak power. PWM still works if synchronization between transmitter and receiver fails, whereas pulse position modulation does not.

The main goal of PWM is to encode analog signal levels digitally.  These signals are digitally encoded by creating a square wave of a particular duty cycle, which corresponds to an analog signal level.  This is achieved by using high resolution counters.  A digital PWM signal is created by supplying a voltage or current to a load in a pattern of on and off pulses.  When these pulses are created, the DC power supply is either on or off which makes this a digital signal.  Typically, the on and off times represent a high and low level respectively, where the high time is the time that the voltage or current was applied to the load and the low time is when the DC source is off.  Any analog signal can be encoded this way provided there is ample bandwidth.


Figure 1 shows three examples of PWM signals with different duty cycles.  The top chart shows a signal with a 10% duty cycle, where the signal is on or high for 10% of the time per cycle and off 90% of the time.  The middle chart shows a signal with a 50% duty cycle (high 50% and low 50%) and the bottom with a 90% duty cycle.  This value also relates to the strength of the signal.  For example, if the DC supply is a 10 V supply and the signal has a 10% duty cycle, then the result will be a 1 V analog signal.


Fig 1.jpg

Figure 1: PWM signals of varying duty cycles1


Figure 2 shows a set up where there is a switch between a 9 V power supply and a lamp which can easily be controlled by PWM.  When the switch is open for 1 second and then closed for 1 second, the bulb will appear to be lit by 4.5 V because it is receiving a 50% duty cycle signal.  Now, if we increase the rate so that the switch is opened and closed 10 times per second while retaining equal time on and off, we can say that this signal has a 50% duty cycle and is being modulated at a frequency of 10 Hz.

Fig 2.jpg

Figure 2: A simple PWM circuit1


Consider the set up from Figure 2 again.  If the bulb is on for one second and off for one second, it will appear be bright for half the time as if lit by 9 V and off for half the time, rather than a constant dimly lit bulb that one would expect with a 50% duty cycle.  This is because most loads need to be modulated at a higher frequency, for both inductive and capacitive loads.  By increasing the modulation frequency to above 1-10 Hz, the lamp will appear continuously dimly lit as desired.  This same concept applies to other PWM applications.  Typically PWM rates are between 1 kHz and 200 kHz1.


The advantages of Pulse Width Modulation are:

  • Unlike PAM, noise is less since in PWM amplitude is held constant
  • Signal and noise separation is very easy
  • PWM communication does not require synchronization between transmitter and receiver


Hardware and Software Requirements


LabVIEW 7.1 or compatible


Steps to Implement or Execute Code


1) Place the Simulate Signal VI in the block diagram and configure it with the corresponding parameters

2) Place the Square Waveform VI in the block diagram in order to generate a square wave signal

3) Wire the output of the Simulate Signal VI to duty cycle of the Square Waveform VI in order to get PWM output waveform

4) Place the Timing and Transition Measurements express VI in the block diagram to get demodulated signal waveform

5) Place the Time Delay express VI in the block diagram in order to generate proper timing delay

6) Place the waveform graphs in the front panel and wire the blocks to each output function in the block diagram

7) Place the sub diagrams inside the loop in order to repeat the operation until the stop button is enabled

Pic 1.jpg


Pic 2.jpg



Additional Information or References


  1.  Barr, Michael. "Pulse Width Modulation," Embedded Systems Programming, September 2001, pp. 103-104.

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.