Using things like variable resistors, we can reduce the current within a circuit by adjusting a dial or moving a switch. These types of resistors can be useful in many situations, like dynamically reducing fan speed, motor speed, and dimming bulbs, and they are also simple to build and implement. However, they have some limitations. Certain electrical devices (like many LEDs) are not dimmable in this way and will sooner turn off than reduce their brightness. Additionally, potentiometers are analog, making them harder to work with when using something like a Raspberry Pi (which primarily works with digital signals), they can be difficult to fine-tune without digital assistance, and they often induce some power loss in circuits due to the way they’re designed. It’s when these drawbacks become significant that PWM becomes useful.
PWM (Pulse Width Modulation) is a method that is used to digitally reduce the average power that an electrical device receives. It is much more efficient than analog variable resistors, induces no power loss, and can be very carefully tuned with software. The Raspberry Pi can use PWM natively on GPIO pins 12, 13, 18, and 19.
Raspberry Pi 4 GPIO
(Source: raspberrypi.org)
To explain how PWM works, imagine flipping the power switch of a fan repeatedly so that it’s being turned on and off every two seconds. Doing this for a little while will reveal that the fan will still spin, it just won’t ever get up to full speed nor will it stop completely. On average, the fan will likely spin at about half of its full speed. If you speed up the rate at which you turn it on and off, it will become harder to see any fluctuations in fan speed (this is called the frequency), and if you increase or decrease the amount of time that you leave the fan on between each time you turn it off, its average spin speed will either increase or decrease (this is called the duty cycle). This is fundamentally how PWM works. This example extends to many different electronic devices, including LEDs. However, LEDs need to be turned on and off very quickly for the reduced brightness to look convincing (otherwise it will just appear as though it’s blinking).
Instead of reducing the voltage or current, PWM keeps them the same but only sends the power a percentage of the time that the device is on, in quickly repeated pulses. To do this, it varies (modulates) the width of the pulses it sends, hence pulse width modulation. If the pulses are half of the normal width, then the device is only receiving power half of the time and the average power it receives is also cut in half. PWM is binary – it only sends the full amount of power (on) or none at all (off), never something in between.