A pin only has two settings
Inside a microcontroller, most pins are digital. A digital pin is either HIGH (full voltage, like 5 volts) or LOW (0 volts). There is no setting called “a little bit.”
But your phone screen dims. A robot slows down as it gets close to a wall. A desk fan has a low setting. How can an on-or-off pin do any of that?
Flick the switch very, very fast
Imagine you flick a light switch on and off, over and over, super fast. If the light is on half the time, the room looks half as bright. If it is on only a quarter of the time, the room looks even dimmer. You never used a dimmer knob. You only used on and off.
Another way to see it is a flip book. Each page is one still picture, yet flip fast and you see smooth movement. Your eyes blend the pages together. They blend fast light blinks together, too.
That trick has a name: PWM. It stands for pulse width modulation. A pulse is a short burst of ON. Width is how long the burst lasts. Modulation just means “changing.” So PWM means changing how wide the ON bursts are.
The four big ideas
Every PWM signal is a repeating square wave. Four words describe it. You will use all four in the lab below.
Cycle and period
One cycle is one ON burst plus one OFF rest. The period is how long that takes.
period = 1 ÷ frequencyFrequency
How many cycles happen each second. It is measured in hertz (Hz). 100 Hz means 100 cycles every second.
500 Hz → 2 ms per cycleDuty cycle
The percent of each cycle that the signal is ON. More ON time means a higher duty cycle.
duty = on-time ÷ periodAverage voltage
The pin is either full voltage or zero, but the average sits in between. Lights and motors respond to the average.
avg = voltage × dutyExample: a 5 V pin that is ON for 1 ms out of every 4 ms has a duty cycle of 1 ÷ 4 = 25%. Its average is 5 V × 0.25 = 1.25 V.
Frequency and duty cycle are different. Frequency is how often the switch cycles. Duty cycle is how long it stays on each time. You can change one without the other.
The PWM scope lab
This virtual oscilloscope draws the voltage on one pin over time. Drag the sliders. Watch the square wave, the average line, and what an LED and a motor would do.
How long the pin stays ON in each cycle.
How many cycles per second. 490 Hz is the Arduino Uno default.
LED view: what your eye sees
Your eye blends the blinks into one steady glow.
Slow motion is off. Press play to watch the LED really blink.
Motor view: a fan that can’t keep up
The motor is a heavy spinner. It reacts to the average voltage, not each blink.
This fan model needs about 0.6 V to start and is built for 5 V. It spins up and slows down gradually, like a real motor.
Photosensitivity note: nothing on this page flashes faster than 3 times per second. Normal mode shows steady light only. Slow motion is off until you press play, and it never goes above 2.5 blinks per second. If blinking bothers you, leave it off. The scope trace tells the same story.
Lab goals (checked automatically)
- Make the average voltage between 1.2 V and 1.3 V.not yet
- Dim the LED to about half brightness (45% to 55%) with no flicker (250 Hz or faster).not yet
- Make the fan run at about half speed (45% to 55% of top speed).not yet
Why your eye and a motor can’t keep up
PWM only works because the thing on the other end is slow compared with the switch. A pin can flip thousands of times a second. Most things cannot react that fast.
Your eye blends fast blinks
Your eye holds on to each flash of light for a tiny moment. This is called persistence of vision. If blinks come fast enough, the moments overlap. You see a steady glow whose brightness matches the average. That is the flip book at work.
A motor is heavy
A motor has inertia. That means a spinning thing keeps spinning, and a still thing needs time to start. Each ON pulse gives the motor a quick push. The next push comes before it slows down. So it settles at a speed set by the average push.
When switching is too slow, you see it
Try the lab with frequency near 1 Hz or 5 Hz. The wave is the same shape, but now each cycle takes so long that your eye can see the pieces. The LED seems to flicker. Most people see a steady light above roughly 100 to 200 Hz. Cameras can still catch flicker as dark bands, because a camera takes snapshots in a different way than your eye.
Faster is not always free. Each switch costs a tiny bit of energy. Very fast PWM can make parts warm. Very slow PWM makes motors whine or jerk, and a motor can make an audible hum at frequencies we can hear. Real designs pick a frequency that is fast enough to look smooth but not wasteful. An Arduino Uno uses about 490 Hz on most PWM pins and about 980 Hz on pins 5 and 6.
Why not just use a resistor?
A resistor can also dim a light, but it burns the extra power as heat. A switch that is fully on or fully off wastes almost nothing. That is why PWM is used in battery-powered things like robots, drones, and phones.
Servos: when the width is the message
A servo is a small motor that turns to a chosen angle and holds it. It is used in robot arms, RC planes, and camera mounts. Robots use them a lot.
A servo uses PWM in a different way. It does not care about the average. It reads the pulse width as a message. A pulse arrives about every 20 ms (that is 50 Hz). The width of that pulse says where to point:
- 1.0 ms pulse means 0 degrees (one end).
- 1.5 ms pulse means 90 degrees (the middle).
- 2.0 ms pulse means 180 degrees (the other end).
Notice that the duty cycle is only 5% to 10%. Servo pulses are short on purpose. Different servos can vary a little, so real projects often test their own.
Real servos stop at their ends. Below 1.0 ms or above 2.0 ms the arm just stays at 0 or 180 degrees.
Servo challenge: aim at the flag
Goal 1 of 4: point the arm at 90 degrees. Find the pulse width that does it.
Hint: the angle is (pulse − 1.0) × 180. So 135 degrees needs a pulse of 1.0 + 135 ÷ 180 = 1.75 ms.
analogWrite: turn 0 to 255 into a duty cycle
On Arduino-style boards, one function does all the PWM work: analogWrite(pin, value). The value is a number from 0 to 255. That is 256 steps, because the timer inside the chip counts in 8 bits.
- 0 means 0% duty. Always OFF.
- 255 means 100% duty. Always ON.
- Anything between means value ÷ 255. So 128 is about 50%.
The name is a little tricky. The pin does not make true analog voltage. It makes PWM. Try it:
| Value | Duty | On a 5 V pin |
|---|
Now program the LED on pin 9
Free play. Change the numbers and press Run.
Press Run to send your code to the virtual board.
Virtual board · LED on pin 9
The screen LED blends changes over about a third of a second, like your eye does, so it never flashes quickly.
Challenge progress
- Set the LED to 25% brightness.not yet
- Make a loop that fades the LED up from 0 to 255, then back down.not yet
This mini editor understands a small piece of Arduino C: analogWrite, delay, pinMode, int variables, and for loops. Like real Arduino code, dividing two whole numbers drops the decimal, so 255 / 4 is 63. On an Uno only pins 3, 5, 6, 9, 10, and 11 can do PWM.
Where PWM shows up
- Screen and lamp dimmers. Many LED lights and some phone screens dim by switching fast.
- Fans and robot wheels. Motor speed is set by duty cycle. Reversing a wheel needs an extra part called an H-bridge.
- Servos. Robot arms, RC planes, and camera mounts read pulse width as an angle.
- Power supplies. Phone chargers use fast switching to change voltage with little wasted heat.
- Sound. Cheap buzzers and some simple audio use PWM at the speed of the note.
Can PWM ever be smooth analog?
Yes, with help. A small circuit called a filter (a resistor and a capacitor) can smooth the pulses into a steady in-between voltage. Parts that can make true analog voltage by themselves are called DACs. Many small boards skip the DAC and use PWM instead because it is cheap and simple. To see how voltage and resistance work in circuits like these, try Ohm’s Law and Simple Circuits.
Quick answers
What does PWM stand for?
PWM stands for pulse width modulation. A pulse is a short burst of ON. Width is how long the burst lasts. Modulation means changing it. PWM changes how wide the ON pulses are while the pin keeps switching on and off very fast.
What is duty cycle?
Duty cycle is the percent of each cycle that a signal is ON. A signal that is on for 1 millisecond out of every 4 milliseconds has a 25 percent duty cycle. A 5 volt pin at 25 percent duty has an average of 1.25 volts.
Is PWM real analog output?
No. The pin only ever outputs full voltage or zero volts. Things like LEDs, motors, and your eyes blend the fast switching into an average, so it acts like an in-between voltage. Some chips have a true analog output called a DAC, but many small microcontrollers do not.
How fast does PWM need to be to stop LED flicker?
Most people see a steady light above roughly 100 to 200 hertz. An Arduino Uno uses about 490 hertz on most PWM pins and about 980 hertz on pins 5 and 6, which is fast enough for the eye. Cameras can still catch flicker, and motors may whine at frequencies you can hear.
How does PWM control a servo?
A hobby servo reads the width of a pulse as a message, not as power. The pulse repeats about every 20 milliseconds. A pulse of about 1 millisecond points the arm to one end, 1.5 milliseconds to the middle, and 2 milliseconds to the other end.
Why use PWM instead of a resistor to dim a light?
A resistor turns the extra power into heat. A switch that is fully on or fully off wastes almost no power, so PWM is much more efficient. That is why PWM is used in phone chargers, fans, and battery-powered robots.
Which Arduino pins can do PWM?
On an Arduino Uno, pins 3, 5, 6, 9, 10, and 11 can do PWM. They are marked with a tilde (~) on the board. Other pins can only be fully on or fully off.
Quick quiz
A 5 V pin is ON for 2 ms and OFF for 2 ms, over and over. What is its average voltage?
Why does a PWM LED look dim instead of blinking?
A servo gets a 1.5 ms pulse every 20 ms. Where does the arm point?
Keep learning about physical computing
You now know how an on-or-off pin can fake an in-between value. Next, go back to the chip that makes the pulses, learn the circuit rules behind them, or see how robots put it all together.