As discussed in a previous example, there are 2 primary variables used to control pulse-width modulation. These variables are the duty cycle and the frequency.
The duty cycle affects the amount of time that a device is on when using a PWM signal, ranging from 0% (off) to 100% (always on). For example, a duty cycle of 50% would send power half of the time, cutting the average power the device receives in half, and a 25% duty cycle would only send power a quarter of the time, reducing the power the device receives by 75%. In a single duty cycle, the device makes a full rotation from on to off. If these duty cycles are run quickly enough, then the connected device will act functionally identical to a device that is genuinely receiving the percentage of power that PWM is attempting to emulate.
Same frequency, different duty cycles
(Source: Circuit Digest)
The frequency is a measure of how quickly the duty cycles are performed, measured in hertz (Hz). 1 Hz is equal to 1 second. A 50% duty cycle at a frequency of 1 Hz will cycle on and off over a period of 1 second, with each setting lasting for ½ of a second. This is slow, and on a device like an LED, will make it appear as though it’s blinking. There comes a point (usually around 100 Hz) where it’s no longer possible to distinguish between when the light is on and when it’s off, making the PWM “illusion” much more effective. For LEDs, I tend to set the frequency to around 500 Hz. The Raspberry Pi has a cap at 8 KHz, though this will rarely be a meaningful limitation.
Same duty cycle, different frequency
As a final note, some devices may not be able to work with PWM signals, and it’s generally good practice to research whether a particular device can handle it before attempting to use it. LEDs and most modern computer fans will have no trouble with PWM signals, but many digital devices and more complex circuitry may have issues/break if a signal is sent to it in this way.