What is a PWM (Pulse Width Modulation) signal?

Last modified 4 months

Knowing what a PWM signal is will help us a lot in our projects, as PWM signals are increasingly used in computers (like the Raspberry Pi) and microcontrollers (like Arduino, ESP8266 and ESP32), so it is more and more important to have a clear understanding of what a PWM signal is.

Today we are going to talk about this very interesting and useful topic for electronics enthusiasts: PWM signals. What are they and what are they for? Let's have a look at them!

This article has a very practical approach. To put it into perspective, let's imagine that we want to control a 5V DC fan with a PWM signal.

But what the hell is a PWM signal?

The acronym PWM stands for Pulse Width Modulation.

This is a technique that consists of varying the time that an electrical signal is on or off, keeping its voltage and frequency constant. In this way, the amount of energy transmitted to a device, such as a motor, LED or speaker, can be controlled.

How does a PWM signal work?

Let's imagine that we have a square signal, i.e. a signal that has only two possible states: high (ON) or low (OFF). This signal has a period, which is the time it takes to complete one cycle, and a frequency, which is the number of cycles per second. The frequency is measured in hertz (Hz).

Now, within each cycle, the signal may spend more time in the high state or in the low state. This is called duty cycle, and is expressed as a percentage. For example, if the signal is 50% of the time in the high state and 50% in the low state, the duty cycle is 50%. If 75% is in the high state and 25% is in the low state, the duty cycle is 75%, and so on.

What a PWM signal does is to vary the duty cycle of a square signal, keeping its frequency and voltage constant. In this way, the average power delivered to the connected device can be regulated.

For example, if we have a motor running on a 5 V, 100 Hz signal, we can make it spin faster or slower by changing the duty cycle of the PWM signal.

If the duty cycle is 100%, the engine receives full power and rotates at full speed. If the duty cycle is 0%, the engine receives no power and stops.

If the duty cycle is 50%, the engine receives half the power and rotates at a lower speed.

What are the advantages of using a PWM signal? Well, there are many. For example, it saves energy, as only the necessary power is delivered to the device. It also allows you to precisely control the speed of motors, the brightness of LEDs or the volume of speakers. It is also a very simple technique to implement with electronic circuits or microcontrollers such as Arduino.

Easy explanation of PWM (Pulse Width Modulation)

Pulse Width Modulation is very easy to understand, if we don't get technical and make a simile.

Imagine the operation of the light bulb in the ceiling of a room and its switch...

  • If we close the switch, the light is switched on.
  • If we open the switch the light goes out
  • If we close the switch, the light is switched on.
  • If we open the switch the light goes out
  • If we close the switch, the light is switched on.
  • If we open the switch the light goes out

Now imagine that we close and open the switch continuously and very fast, so fast that the bulb doesn't even turn off completely....

What we will achieve is that the bulb does not light completely. We will be "dimming" it. Not by lowering the voltage, but by turning it on and off very quickly.

Imagine that instead of a light bulb we have a fan and we do the same operation:

If the fan is rated for 5000 RPM at 5V, when we apply 5V, the fan starts to rotate until it reaches 5000 RPM (it does not go from 0 to 5000 RPM suddenly, it will take a few moments due to its inertia) and when it reaches 5000 RPM it maintains at that speed until we remove the voltage.

What if we had super-reflexes and super-sight like Superman and were able to:

  • Close the switch to power the fan and make it rotate.
  • Observe how it increases its speed (as if in slow motion, because we are Superman).
  • Open the switch when the fan reaches 3010 RPM to turn off the power and stop the fan spinning (it will slow down slowly, because it has a lot of inertia).
  • When the speed reaches 2990 RPM (a few milliseconds) close the switch.
  • When the speed reaches 3010 RPM we open the switch.
  • When the speed reaches 2990 RPM close the switch.
  • And so on and so forth...

I think you get it...

The result of this "nonsense" would be that our fan would spin at 3000 RPM (it would actually stay between 2990 and 3010 RPM but this would be unnoticeable to us, except Superman).

We would not have reduced the voltage, it has always been 5V but changing it between ALL/NOTHING very quickly.

In practice, this means that, if half the time we have been applying 5V and the other half 0V, it would be as if we had been applying 2.5V

If the 75% of time we had applied 5V and the 25% of time 0V, it would be in practice as if we had been applying a equivalent continuous voltage from 3.75V (the 5V 75%).

Now you understand what PWM is all about, isn't it simple?

Characteristics of a PWM signal

Logically, not all PWM signals are the same. There are some basic differences, or parameters, that we need to be aware of.

These three values will allow us to know the effect it will produce and to modify the signal according to our objectives.

If we go back to the example above, we could make several variations:

  • Choose different voltageWe could have chosen to feed the fan with a 3.3V PWM signal, instead of 5V (logically, its maximum speed would have been lower).
  • Changing the ratio between on and off timeThe longer we keep the switch in the on position, and less in the off position, the faster the fan will spin.
  • Move the switch fasterDepending on whether the switch is being operated by Superman, Batman or me, we could each move the fan at maximum speed. Superman might be able to open and close the switch thousands of times per second, without batting an eyelid, but I don't think I could manage more than 4 or 5 per second and in a few minutes I'd have enough tendonitis to run to the emergency room.

These three variations define the main characteristics of a PWM signal, each of which is given a name:

  • AmplitudeThe amplitude: This is the voltage we apply to the fan. In our case we can say that the amplitude is 5V.
  • Duty cycleThe proportion of time that the signal is at "high level" (at 5V). In our examples we have used 50% and 75% duty cycles.
  • FrequencyIt's how fast we turn the "high level" (the 5V) on and off. Superman would do it maybe 25000 times per second (25000 Hertz, or 25Khz) and I would do it four or five times per second (4Hz or 5Hz).

Take a few seconds to think about how each of these three parameters affects the way the fan moves and imagine how the movement would change by varying them.

Note, for example, that it is the Duty Cycle that will modify the output "equivalent voltage". (for a fixed input of X Volts). If the input is 5V and the duty cycle is 50%, the equivalent voltage will be 2.5V, regardless of whether the frequency is 4Hz or 25Khz (I operate the switch myself or Superman does it 6250 times faster).

So what is the difference between the PWM signal being 4Hz or 25Khz?

Quite simply, the "smoothness" with which the fan will run.

If I only turn the switch on and off four times per second (0.25 seconds per cycle), the fan will move more "jerky" than if I do it 25000 times per second.

Imagine that instead of running at a frequency of 4Hz, it would run at a frequency of 0.04Hz (100 times slower, or every 25 seconds). The fan would stop for most of the 25 seconds (although it would then be on again for almost 25 seconds).

This change in frequency, in the "speeding up and slowing down of the fan" has a very important consequence: THE NOISE.

A fan running at 1000 RPM can be quite quiet, but if I am continuously moving it between 0 or 1000 RPM (every 25 seconds) what I will have is a horrible noise caused by speed changes, instead of a light blowing of the fan running at a steady speed.

Which PWM signal do we need?

Now that we know what the main parameters of a PWM signal are, we can better define what a PWM signal should look like. ideal PWM signal for motor speed control on a Raspberry Pi, for example.

Amplitude

The amplitude will be kept at 5V. Why?

Because it doesn't make sense to turn it up. The fan we use is 5V and if we put more than that we will burn it out.

Nor does it make sense to lower it. If we put, for example, 3.3V, we will limit the maximum speed at which we can make the fan spin and it will also work worse (the motor torque and its energy efficiency will drop). To lower the speed we already have the duty cycle. If we want and we are interested, we can make it so that it never exceeds a point (for example, that the duty cycle never exceeds 80%).

Duty cycle

The duty cycle is what will allow us to regulate the fan speed, so we want it to have the maximum possible range. Should the duty cycle be variable between 0 and 100%? Probably not.

A fan of this type needs a minimum duty cycle to start running, just as it needs a minimum voltage below which it will not run. The fan will not move if we apply a signal with a duty cycle of 1% or 5% to it, just as it would not move if we fed it 0.05V or 0.25V.

So what should be the minimum duty cycle What should we choose for the PWM signal?

Very simple to say, and not so easy to do. The minimum duty cycle should be the minimum at which the particular fan we are using starts to move (plus a small safety margin to make sure it always "starts").

When we write our control programme we will see that we can do the following software optimisations and improvementsfor example:

Maybe our fan needs a minimum duty cycle of 60% to start but, once in motion, it is able to go down to 40%. We can make that, to start the fan from a standstill, our program applies for a few moments a duty cycle of 60% (to ensure that it starts) but then lowers it to 40%.

Of course, the duty cycle can always be set to 0%. In practice this means that there will be no voltage and the fan will be completely off.

It is important to note that if the fan does not start because the duty cycle is too small (as if the voltage is too low), the fan continues to draw current. In fact, it may even get hotter than normal and burn out. It is therefore important to make sure that the duty cycle is such that the fan never stops without ever dropping below that (unless the duty cycle is 0%, or off, of course).

Frequency

Let's choose a frequency of 25Khz for our PWM signal. Why?

Because if we chose it lower we could hear it.

In addition, a sufficiently high frequency will help the motor not to sense voltage changes.

Lower frequency limit of the PWM signal

The human ear is capable of hearing up to 20 or 22Khz, with some people able to hear a little more. If we apply a 25Khz signal we will ensure that we hear nothing of the regulation operation.

Imagine that, if we were to choose a signal of, say, 12Khz, the fan could "vibrate"at that frequency or become a "loudspeaker"This would result in a permanent high-pitched tone that could be annoying.

Older readers will probably remember that when we were children we could hear on those old black-and-white TV sets if the TV "was on"even if it was without volume. This was because children are able to hear higher frequencies than adults and we heard, faintly, the internal oscillators of these TV sets.

Upper frequency limit of PWM signal

We are also not interested in going much higher than 25 or 30Khz. Why?

Because we no longer find advantages and yet we find disadvantages such as:

  • We would have to use faster transistors. If we stay below 25 or 30Khz, there will be more transistors that we can use.
  • Losses start to be higher. We would start to lose energy "on nothing" instead of dedicating it to moving the fan.
  • As frequencies go up, the circuits start to become more critical and can produce some problems, such as self-oscillations, which makes it necessary to be careful with the layout of the components, length of their terminals, etc.
  • Increased heat generation
  • Fewer fans normal that support higher frequencies.

PWM signal glossary and FAQ

What is modulation of a signal?

Signal modulation is the process of varying a property of a signal called the "carrier signal" in order to transmit additional information. This technique is widely used in communications, as it allows information to be sent over different transmission media, such as cables, optical fibres or electromagnetic waves, such as radio signals.

The main purpose of modulation is to allow information to be carried efficiently and to adapt to the characteristics of the transmission medium. Some of the properties of the carrier signal that can be modulated include amplitude, frequency, phase and pulse width. These are the most common types of modulation:

  1. Amplitude Modulation (AM): In amplitude modulation, the amplitude of the carrier signal is varied according to the information signal. Amplitude modulation is commonly used in the transmission of AM radio signals.
  2. Frequency Modulation (FM): In frequency modulation, the frequency of the carrier signal is varied according to the information signal. Frequency modulation is used in the transmission of FM radio signals and in the transmission of high quality audio.
  3. Phase Modulation (PM): In phase modulation, the phase of the carrier signal is varied according to the information signal. Phase modulation is common in telecommunications applications and data transmission.
  4. Pulse Width Modulation (PWM): In pulse width modulation, the pulse duration of the carrier signal is varied according to the information signal. It is used in control applications such as motor control and power modulation in electronics.
  5. Quadrature Amplitude Modulation (QAM): Quadrature amplitude modulation is a combination of amplitude modulation and phase modulation. It is used in the transmission of digital signals in applications such as cable television and data transmission.

Modulation allows information to be transmitted efficiently, as modulated signals are more resistant to noise and interference in the transmission medium. It also allows multiple signals to share the same transmission medium, as each modulated signal can occupy a different frequency range. In summary, modulation is essential for the effective transmission of information in communication and control systems.

What is a square wave?

A square wave is a type of waveform characterised by two distinct voltage levels and abrupt transitions between them. In a square wave, the signal varies between two constant values: a high value (usually represented as a positive voltage level) and a low value (usually represented as a voltage level close to zero).

The main characteristics of a square wave are as follows:

  1. Duty cycle: The ratio of the time the wave is at its high level to the total time of a complete cycle is called the duty cycle. It is usually expressed as a percentage and determines the relative duration of the high level in the square wave. A 50% duty cycle means that the square wave is equally divided between the high level and the low level.
  2. Frequency: The frequency of a square wave refers to the number of complete cycles repeated per unit time. It is measured in hertz (Hz) and determines how quickly the wave changes between its high and low levels.
  3. Amplitude: Amplitude refers to the maximum value of the high level in the square wave, usually measured in volts (V).

Square waves are commonly used in electronics and telecommunications to generate digital signals, as their fast and clear transition between the two levels makes them suitable for representing binary signals, such as those used in digital systems.

Square waves are commonly used in applications such as generating clock pulses in digital circuits, test and measurement, digital communications, and in modulating signals to transmit digital data. Their clear waveform and ease of distinguishing between high and low levels make them very useful in a variety of technical applications.

What is the duty cycle?

Duty cycle, also known as duty cycle, is a measure used to describe the ratio of the time a signal is in its active or high state (e.g., a high voltage level) to the total time of a complete cycle of the periodic signal. It is usually expressed as a percentage and is commonly used to describe the relative duration of an active state in a periodic waveform, such as a square wave.

The duty cycle is calculated as follows:

Duty cycle (%) = (Time in active state / Total time of one cycle) × 100

In a periodic signal, such as a square wave, the duty cycle indicates how long the signal is at its high (active) level compared to the total cycle time. A 50% duty cycle means that the signal is equally divided between its active level and its low level. For example, in a square wave with a 50% duty cycle, the high level and low level have the same duration.

If the duty cycle is less than 50%, it means that the signal spends more time in its low state than in its high state, and if it is greater than 50%, it means that the signal spends more time in its high state than in its low state.

Duty cycle is an important feature in a variety of applications, such as in the generation of clock pulses in digital systems, in the modulation of signals, and in the description of periodic waveforms in electronics and telecommunications. It allows control of the proportion of time a signal is active, which is essential to ensure the proper functioning of many systems and circuits.

What is frequency?

Frequency is a measure that describes the number of repetitions of a periodic event in a unit of time. In the context of signals, frequency is used to describe how fast a periodic signal repeats itself in terms of cycles per second. The unit of measurement for frequency is the hertz (Hz), which is equivalent to one cycle per second.

In simpler terms, frequency refers to the number of times an event or phenomenon is repeated in a unit of time. For example, if a wave repeats 100 times in one second, its frequency is 100 hertz (Hz).

Frequency is applied in a wide variety of contexts:

  1. Frequency in waves and signals: In the realm of waves, such as sound waves or electrical signals, frequency determines how fast the wave oscillates in terms of cycles per second. High-frequency signals oscillate faster than low-frequency signals.
  2. Frequency in music: In music, frequency relates to the pitch of a musical tone. Higher musical notes have higher frequencies, while lower notes have lower frequencies.
  3. Frequency in electronics: In electronics, frequency is used to describe the speed at which electrical circuits operate, such as the frequency of a clock in a microprocessor.
  4. Frequency in physics: In physics, frequency also applies to phenomena such as vibrations, oscillations and electromagnetic waves.

Frequency is a fundamental property in many scientific and technical disciplines, and is used to characterise and analyse a wide range of periodic and cyclic phenomena.

What is amplitude?

Amplitude refers to the magnitude or maximum value of a quantity in the context of a periodic wave or signal. In simplest terms, amplitude represents the maximum distance from the centre point or midpoint of the signal to its highest (positive) or lowest (negative) point. In the case of a sine wave, the amplitude is the distance from the midpoint to the peak or trough of the wave.

Amplitude is expressed in units of measurement relevant to the phenomenon in question. For example, in the case of a sound wave, amplitude is measured in units of pressure, such as pascals or decibels (dB), and in the case of electrical signals, amplitude is measured in volts (V) or amperes (A).

In summary, amplitude is an important characteristic of a signal or periodic wave that describes its maximum magnitude and, therefore, its strength or intensity. Changes in the amplitude of a signal can have a significant impact on its perception or its ability to carry information, as in the case of sound signals, where amplitude can be related to loudness, or in the case of electrical signals, where amplitude can represent the intensity of a current or voltage level.

What is a square wave with a duty cycle of 50% called?

A square wave with a 50% duty cycle is called a "symmetrical square wave" or "symmetrical square pulse". This means that the signal has a duty cycle in which it is active (high) for half of the period and inactive (low) for the other half of the period.

What is a square wave with, for example, a duty cycle of 30% called?

A square wave with a duty cycle of 30% is called an "asymmetric square wave" or "asymmetric square pulse". In this case, the signal has a duty cycle in which it is active (high) for 30% of the period and inactive (low) for the remaining 70% of the period.

What is the difference between a square wave and a PWM signal?

The main difference between a square wave and a PWM (Pulse Width Modulation) signal lies in the fact that its application and purpose:

  1. Square Wave:
  • A square wave is a periodic signal characterised by a constant and symmetrical duty cycle, meaning that the signal is active (high) for a fixed part of the period and inactive (low) for the other part of the period.
  • It is commonly used as a clock in digital electronics, to generate timing signals, and in situations where a constant on/off signal is needed.
  1. PWM (Pulse Width Modulation) signal:
  • PWM is a technique in which the duty cycle of a square wave is modified to control the power delivered to a device or system. The duty cycle can be varied to achieve different output power levels.
  • It is used in applications such as motor speed control, LED dimming, temperature control in heating and cooling systems, among others.
  • The variation of the duty cycle allows precise control of the amount of power delivered to the device, making it useful in control applications.

In summary, a square wave is a signal with a fixed, symmetrical duty cycle, while the PWM signal is a technique that modifies the duty cycle of a square wave to control power or current in various control applications.

What next?

If you want to know more about what a PWM signal is (which you don't need for what we're talking about here), I recommend you to read this Wikipedia article on PWMwhere it is very well explained.

If you haven't done it, this article, in which I explain how a PWM signal is used to regulate the speed of a fan, may also be of interest to you:

I hope you enjoyed this post about PWM signals and that you are encouraged to experiment with them. See you next time!

Leave a comment