You may have encountered Pulse Width Modulation when working with Arduino or other microcontrollers. PWM is a powerful tool that is often implemented to control motors and dim LEDs in electronics projects. While it’s typical applications are not always audio-centric PWM can still be a useful tool in our tool belt. PWM can be used to modify the sound of our humble square wave oscillators and is foundational to many digital to analog converters.
Duty Cycle
The duty cycle of a square wave is the ratio of time the wave spends in its high state to the time it spends low. A perfect square wave would have a duty cycle of 50%. If you increase the time the wave spends high the duty cycle increases, if you decrease it the duty cycle decreases. A duty cycle of 100% would represent a DC voltage (at the output voltage of your source). Meanwhile a duty cycle of 0% would represent a steady 0V.
Below I have graphed some basic PWM signals (blue) along with their duty cycle and average voltage (orange):
Average Voltage
The key to understanding how to use PWM often lies in understanding average voltage. In the graph above you can see the average voltage (orange line) is highest when the duty cycle is high. This should make intuitive sense since with, for example, a 75% duty cycle the voltage is high for 75% of the time and 0V for 25% of the time. You can find the average voltage of any PWM signal by multiplying the duty cycle by the max voltage. In a 5V circuit for instance, with a 20% duty cycle, the average voltage would be 1V (5*0.2=1).
A Quick Word On Frequency
As you might guess the frequency of a PWM signal can be critical to your system design. In an audio oscillator the role of frequency is largely unchanged, The frequency will set the tone of your output while the pulse width will allow you to adjust the fullness of your sound.
When you are using PWM to create an analog signal things get a bit more complicated. Typically a specific frequency is chosen and the subsequent filters are designed based on it (I will go further into this process when I discuss filtering PWM signals in a future post) . In choosing this frequency we would be looking for a balance between speed and control. At faster frequencies it is easier to filter the signal (to obtain a steady voltage) and you can change the output voltage quicker. However, the faster the signal the harder it becomes to control the exact duty cycle. You want to maintain the fastest frequency possible where you can still adjust the duty cycle with the precision your application requires.
I hope this has given you a framework to begin working with PWM. Keep an eye out for my next post; I will be looking at filtering these PWM signals to obtain a steady voltage.