A single output pin on a microcontroller can really only do two things: sit at full voltage or sit at zero. That sounds too blunt to control the brightness of an LED or the speed of a fan, both of which seem to need a value somewhere in between. Pulse width modulation is the trick that closes that gap, and it works by flicking the pin on and off faster than your eyes or most motors can follow. This guide explains how the timing works, why it wastes so little energy, and where you will keep running into it once you start building things.
Two states, switched faster than you notice
The idea rests on a simple fact about perception and inertia. If a light flashes quickly enough, you stop seeing individual flashes and perceive an average brightness instead. A motor behaves the same way for a different reason: its physical mass cannot react to each tiny pulse, so it responds to the average push it receives over time. PWM exploits both effects by delivering full-strength pulses and controlling only how much of each moment the signal spends switched on.
Because the pin is always either fully on or fully off, it never lingers in the partly-on state where components turn voltage into wasted heat. That single trait is the source of the efficiency the technique is prized for, and it holds on every microcontroller from the smallest chip to the common maker boards, so the pick between them, weighed in the usual beginner board comparison, rarely turns on PWM at all.
Duty cycle and frequency, the two dials
PWM has two independent settings. Duty cycle is the share of each cycle the signal stays high, written as a percentage. Fifty percent means the pin is on for half of every cycle and off for the other half. Ten percent means it spends most of its time off. Frequency is how many complete on-off cycles happen each second, measured in hertz. The two are separate, so you can change how bright a light looks by adjusting duty cycle while leaving frequency untouched.
Frequency has to be high enough that the flashing dissolves into a steady average. For LEDs, a few hundred hertz usually looks smooth to the eye, though higher rates avoid faint flicker that a camera might catch. For motors, the right range depends on the motor, but too low a frequency produces an audible whine and jerky, stepping motion instead of smooth travel.
| Duty cycle | Signal on-time | Effect on an LED |
|---|---|---|
| 0% | Never on | Off |
| 25% | On a quarter of the time | Dim |
| 50% | On half the time | Medium brightness |
| 100% | Always on | Full brightness |
Turning a percentage into brightness or speed
Dimming an LED comes down to choosing a duty cycle. At 20 percent the diode is dark for four fifths of every cycle, so its average light output drops to roughly a fifth, and your eye reads that as dim. Push the duty cycle to 80 percent and the light looks nearly full. The LED still receives its normal full voltage during each pulse, which keeps its colour consistent rather than shifting as it would if you simply starved it of voltage.
A motor works on the same principle with its own quirks. A higher duty cycle means more energy delivered per second, so the motor turns faster, and because a spinning rotor coasts between pulses, the motion smooths into steady rotation. Controlling motors safely usually means adding a transistor to carry the current, since the controller pin cannot drive a motor directly, a wiring point the article on what those header pins can and cannot do makes clear.
Why switching beats a resistor
The old way to dim a light or slow a motor was to place a resistor in the path, turning surplus energy into heat until only the amount you wanted reached the load. It works, but everything you hold back is lost as warmth, and the resistor has to be sized to survive that heat. On a battery project, that waste shortens the runtime for no benefit at all.
PWM avoids the trade almost entirely. Since the switching element is either fully open or fully closed, very little energy is burned in the act of controlling, and nearly all of it reaches the load. On battery-powered gear the difference is stark, which is why phone screens, drones, and cordless tools lean on switching rather than resistive control to stretch their charge.
Where PWM shows up in real builds
Once you recognise the pattern you see it everywhere. Servo motors read the width of a pulse to decide their angle. Fans expose a PWM wire so a computer can hold a temperature steady without running at full noise. RGB lighting mixes colours by driving three channels at different duty cycles. Even sound can be produced by switching a pin at high frequencies and smoothing the result.
On a hobby board, generating PWM is often a single instruction, and pairing it with input from the physical world is a natural next step, the kind of pairing our coverage of taking readings from the world around a board walks through. A lamp that brightens as a room darkens is really just a sensor value mapped onto a duty cycle.
Putting it to work
The mental model worth keeping is that PWM does not create an in-between voltage at all. It delivers full pulses and lets the eye, a motor, or a smoothing component average them into something that behaves like an in-between value. Change the fraction of time the pulse is present and you change that apparent value, cheaply and with almost no waste heat to shed.
That is why the technique scales from a blinking indicator up to serious motor control without changing its nature. Once you have dimmed one LED by adjusting a number in code, the same skill transfers to fans, servos, and lighting rigs. Adding a screen to show the current level, as the breakdown of fitting a readout to your build describes, turns an invisible duty cycle into a figure you can watch and tune by hand.
Common questions
How does PWM dim an LED without a dimmer resistor?
It switches the LED fully on and off hundreds of times a second. Your eye cannot follow the individual flashes, so it perceives the average brightness instead. Lowering the fraction of time the light stays on lowers that average, dimming the LED. The diode still gets its normal voltage during each pulse, so the colour stays steady and almost no energy becomes heat.
What is duty cycle?
Duty cycle is the percentage of each on-off cycle that the signal spends switched on. At 50 percent the pin is high for half of every cycle; at 10 percent it is high only briefly. It sets the average power delivered to whatever the signal drives, so raising or lowering it directly controls brightness, speed, or position depending on the device attached.
Can PWM control any motor?
It suits many small direct-current motors and fans well, but not every type directly. Large motors need driver circuits rated for their current, and some, such as certain alternating-current motors, need entirely different control methods. For typical hobby motors you still place a transistor or motor driver between the controller and the motor, because the signal pin cannot supply the current a motor demands.
