PWM Explained: How analogWrite Dims an LED on Arduino (Duty Cycle)
What PWM is, how duty cycle sets the average voltage, and how Arduino analogWrite (0–255) dims LEDs and controls motors. PWM pins, frequency, simulator.
ON THIS PAGE
An Arduino pin can only be fully ON (5 V) or fully OFF (0 V). So how does analogWrite() make an LED glow at half brightness, or a motor spin slowly? The trick is called PWM — pulse width modulation: switching the pin on and off so fast that your eye (or the motor) only feels the average.
Free PWM simulator
Change the duty cycle and the frequency. You see the square wave, the average voltage and the LED dimming:
⚡ LOADING THE CALCULATOR…
Free, no sign-up. Prefer a bigger screen? Open the PWM dimmer calculator in the Lab →
Duty cycle in one picture
Imagine a light switch flipped on and off 500 times a second:
| Duty cycle | ON time | Average on a 5 V pin | analogWrite value |
|---|---|---|---|
| 0 % | never | 0 V | 0 |
| 10 % | 1 part in 10 | 0.5 V | ≈ 25 |
| 25 % | a quarter | 1.25 V | ≈ 64 |
| 50 % | half | 2.5 V | ≈ 127 |
| 75 % | three quarters | 3.75 V | ≈ 191 |
| 100 % | always | 5 V | 255 |
Why doesn't the LED flicker?
Above about 50–100 flashes a second, your eye can't follow them and blends them into a steady, dimmer light. The Arduino Uno's PWM runs at about 490 Hz (pins 5 and 6 at about 980 Hz) — far too fast to see.
Arduino PWM code
const int LED = 9; // a PWM pin (marked ~)
void setup() {
pinMode(LED, OUTPUT);
}
void loop() {
for (int v = 0; v <= 255; v++) { // fade up
analogWrite(LED, v);
delay(5);
}
for (int v = 255; v >= 0; v--) { // fade down
analogWrite(LED, v);
delay(5);
}
}
Use a 220 Ω resistor in series with the LED, as always (see the LED resistor guide).
Which pins can do PWM?
| Board | PWM pins | Notes |
|---|---|---|
| Arduino Uno / Nano | 3, 5, 6, 9, 10, 11 (marked ~) | 8-bit: analogWrite 0–255 |
| Arduino Mega | 2–13, 44–46 | 8-bit |
| ESP32 | almost any output pin | ledcWrite() / analogWrite() in core 3.x, adjustable resolution and frequency |
| Raspberry Pi Pico | every GPIO | 16 PWM channels |
PWM for motors
A motor responds to the average too, so PWM controls its speed. But never drive a motor straight from a pin — a pin gives only about 20 mA. Use a transistor, MOSFET or a motor driver (like the L298N or TB6612) and send the PWM to its input. The motor's inertia smooths the pulses.
Is PWM real analog?
No — the pin is still only 0 or 5 V; the output just behaves like an analog level for slow things (eyes, motors, heaters). If you need a real steady voltage, add an RC low-pass filter (see RC time constant) or use a DAC.
Frequently asked questions
What does PWM stand for?
Pulse width modulation: changing the width (ON time) of fast pulses to control the average power.
What is duty cycle?
The percentage of each cycle that the signal is ON. 50 % duty means ON half the time and OFF half the time.
What is the PWM frequency of Arduino Uno?
About 490 Hz on pins 3, 9, 10 and 11, and about 980 Hz on pins 5 and 6.
What is the range of analogWrite?
0 to 255 on Arduino Uno: 0 is always off, 255 is always on, 127 is about 50 % duty.
Can I use PWM on any Arduino pin?
On the Uno, only pins 3, 5, 6, 9, 10 and 11, marked with a ~ on the board.





