Skip to content

Tutorial

Arduino PWM Tutorial: analogWrite, LED Dimming, Motor Speed and Frequency

Understand what PWM actually is on Arduino, dim an LED smoothly, and safely control a DC motor's speed with a driver chip -- plus the mistakes (like wiring a motor straight to a pin) that damage boards.

  • 6 min read
  • Updated November 7, 2026
Arduino PWM Tutorial: analogWrite, LED Dimming, Motor Speed and Frequency

Most Arduino pins can't output a real in-between voltage. They're either 0 V or 5 V. PWM (pulse width modulation) gets around that by switching a pin on and off hundreds of times a second: an LED or a motor responds to the average, so changing how long the pin stays on each cycle changes the brightness or the speed.

Quick answer: on an Uno or Nano, use analogWrite(pin, value) on a ~ pin (3, 5, 6, 9, 10, 11) with a value from 0 (off) to 255 (fully on); 128 is about half. LEDs connect through a resistor. Motors never connect to a pin directly: use a MOSFET for one direction or a driver such as the L298N, TB6612FNG or L293D for two. The default frequency is about 490 Hz (980 Hz on pins 5 and 6); raise it to about 31 kHz on pins 9/10 to stop motor whine.

This guide covers how PWM works, PWM pins on every common Arduino, LED dimming that looks smooth, motor control with a MOSFET and with an H-bridge, changing the frequency safely, and the timer conflicts with Servo and tone() that catch people out.

PWM at 25 %, 50 % and 75 % duty cycle: the frequency stays the same, the share of time HIGH sets the average voltage

What PWM Actually Is

A PWM signal is a square wave with a fixed frequency and a variable duty cycle: the percentage of each cycle spent HIGH. On a 5 V board, 25 % duty averages 1.25 V, 50 % averages 2.5 V and 75 % averages 3.75 V. The pin itself is never at those voltages, but anything slow compared with the switching (an LED seen by your eye, a motor's spinning mass, a capacitor) responds to the average.

analogWrite() maps 0–255 onto that duty cycle:

analogWrite valueDuty cycleAverage on a 5 V Uno
00 % (always off)0 V
6425 %1.25 V
12850 %2.5 V
19175 %3.75 V
255100 % (always on)5 V

To convert a percentage, use value = percent * 255 / 100. There's no need to call pinMode() first; analogWrite() sets the pin as an output itself.

Which Arduino Pins Support PWM

On the Uno R3 and Nano (ATmega328P), the PWM pins carry a ~ on the silkscreen. Each pair is driven by one of the chip's three hardware timers:

PinsTimerDefault frequencyAlso used by
5, 6Timer0~980 Hzmillis(), delay()
9, 10Timer1~490 HzServo library
3, 11Timer2~490 Hztone()

Pins 5 and 6 run faster because Timer0 is set to fast PWM mode for timekeeping, while the others use phase-correct mode, which halves the frequency.

Other boards:

BoardPWM pinsNotes
Uno R3, Nano, Pro Mini3, 5, 6, 9, 10, 118-bit (0–255)
Mega 25602–13, 44–4615 pins, 5 timers
Uno R4 Minima / WiFi3, 5, 6, 9, 10, 11analogWriteResolution() up to 12 bits; A0 is a real DAC
ESP32 (Arduino core 3.x)Almost any output pinanalogWrite() works; ledcAttach() sets frequency and resolution
Raspberry Pi PicoAny GPIOanalogWriteFreq(), analogWriteRange()

On a non-PWM pin, analogWrite() doesn't fail. On AVR boards it falls back to a digital write: values below 128 give LOW, 128 and above give HIGH. That's why an LED on pin 7 snaps on halfway through a fade instead of dimming.

Dimming an LED Smoothly

Wire the LED's long leg (anode) to pin 9 through a 220–330 Ω resistor, and the short leg to GND. Then:

sketch.ino
const int LED_PIN = 9;   // a ~ pin

void setup() {}

void loop() {
  for (int b = 0; b <= 255; b++) { analogWrite(LED_PIN, b); delay(8); }
  for (int b = 255; b >= 0; b--) { analogWrite(LED_PIN, b); delay(8); }
}

You'll notice the LED seems to brighten quickly at the start and barely change near the top. That's your eye, not the code: perceived brightness is roughly logarithmic. A gamma curve makes the fade look even:

sketch.ino
const int LED_PIN = 9;

void setup() {}

void loop() {
  for (int i = 0; i <= 255; i++) {
    float x = i / 255.0;
    analogWrite(LED_PIN, (int)(pow(x, 2.2) * 255 + 0.5));   // gamma 2.2
    delay(8);
  }
  delay(300);
}

To control brightness with a knob, read a potentiometer and divide by 4, because analogRead() returns 0–1023 and analogWrite() takes 0–255: analogWrite(9, analogRead(A0) / 4);

At 490 Hz the flicker is invisible to your eye, but a phone camera can show it as rolling bands. For camera-facing lighting, raise the frequency (see below).

You can try both sketches in the free SolderHub Simulator, which runs Uno sketches in the browser.

Controlling a DC Motor with PWM

Never wire a motor straight to an Arduino pin. A pin can supply about 20 mA continuously (40 mA absolute maximum), while a small hobby motor draws a few hundred milliamps and several times that when starting or stalled. The pin, or the whole chip, will be damaged, and the motor's voltage spikes reset the board even before that.

Instead, the PWM pin switches a driver, and the driver switches the motor's own supply.

One direction: a logic-level MOSFET

For a fan, pump or LED strip that only ever runs one way, a single N-channel logic-level MOSFET (fully on at 5 V on the gate, such as the IRLZ44N or IRL540N; not the plain IRF540N) is cheap and efficient:

  • Arduino pin 9 → 220 Ω → MOSFET gate
  • 10 kΩ from gate to GND, so the motor stays off while the Arduino boots
  • Motor between the motor supply (+) and the MOSFET drain; source to GND
  • Flyback diode across the motor (1N5819 or 1N4007), stripe towards +
  • Arduino GND and motor supply GND joined
sketch.ino
const int MOTOR_PIN = 9;

void setup() {}

void loop() {
  analogWrite(MOTOR_PIN, 255);  // kick-start at full power
  delay(100);
  analogWrite(MOTOR_PIN, 120);  // then settle to ~47 %
  delay(3000);
  analogWrite(MOTOR_PIN, 0);
  delay(2000);
}

Most small motors won't start below about 30–40 % duty because of friction, which is why the sketch gives a short burst at full power first.

Both directions: an H-bridge driver

To reverse a motor you need an H-bridge. The common choices:

DriverMotorsCurrent per channelNotes
L298N module2~2 ACheap and tough, but loses about 2 V and runs hot
TB6612FNG2~1.2 A (3 A peak)Small, efficient, little voltage loss; the better choice for small robots
L293D2~0.6 ADIP chip with built-in flyback diodes; for small motors only
BTS7960 module1Tens of ampsFor large motors

With an L298N, two plain digital pins set the direction and the enable pin takes the PWM speed. Remove the ENA jumper first, otherwise ENA is tied permanently HIGH and speed control does nothing.

sketch.ino
const int ENA = 9;   // PWM speed (jumper removed)
const int IN1 = 7;
const int IN2 = 8;

void motor(int speed) {           // -255 (full reverse) to 255 (full forward)
  digitalWrite(IN1, speed > 0);
  digitalWrite(IN2, speed < 0);
  analogWrite(ENA, abs(speed));
}

void setup() {
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
}

void loop() {
  for (int s = 0; s <= 255; s += 5) { motor(s); delay(40); }    // speed up forward
  motor(0); delay(500);
  for (int s = 0; s >= -255; s -= 5) { motor(s); delay(40); }  // speed up in reverse
  motor(0); delay(500);
}

Power the motors from their own supply (a battery pack or a 6–12 V adapter, matched to the motor), connect its ground to the Arduino's GND, and don't feed the motor supply through the Arduino's 5 V pin.

Changing the PWM Frequency

At 490 Hz, many motors whine audibly, and LED lighting can flicker on camera. Raising the frequency above about 20 kHz fixes both. On an Uno, you do it by changing a timer's prescaler in setup():

sketch.ino
void setup() {
  // Pins 9 and 10 (Timer1): prescaler 1 -> ~31.4 kHz
  TCCR1B = (TCCR1B & 0b11111000) | 0x01;

  // Pins 3 and 11 (Timer2): prescaler 1 -> ~31.4 kHz
  TCCR2B = (TCCR2B & 0b11111000) | 0x01;
}
Timer1 / Timer2 settingFrequency (pins 9, 10 or 3, 11)
0x01 (prescaler 1)~31.4 kHz
0x02 (prescaler 8)~3.9 kHz
0x03 (prescaler 64, Timer1 default)~490 Hz

(Timer2 uses different codes for its higher prescalers, but 0x01 and 0x02 behave the same as above.)

Leave Timer0 (pins 5 and 6) alone. It runs millis(), delay() and micros(), so changing it makes every delay in your sketch, and every library that uses timing, run at the wrong speed.

Higher frequencies aren't free: MOSFETs and drivers switch more often and run warmer, and the L298N in particular loses efficiency well before 31 kHz. For it, 1–4 kHz is a reasonable compromise.

Timer Conflicts: Servo and tone()

Because each library borrows a timer, some pins stop working with analogWrite() once a library starts:

  • Servo library (Uno): uses Timer1, so analogWrite() on pins 9 and 10 stops working, even if the servo is on a different pin. Put PWM outputs on 3, 5, 6 or 11.
  • tone(): uses Timer2, so PWM on pins 3 and 11 is disturbed while a tone plays.
  • IRremote and some other libraries also take Timer2.

A servo isn't driven with analogWrite() at all: it needs a 50 Hz pulse of 1–2 ms, which the Servo library generates on any pin.

Turning PWM into a Real Voltage

Some circuits, like a voltage reference or an analog control input on another device, need a steady voltage rather than pulses. A simple RC low-pass filter smooths PWM into DC: a 10 kΩ resistor in series and a 10 µF capacitor to GND gives a steady average with a little ripple. Its time constant is 0.1 s, so it settles in about half a second after the duty cycle changes. Raise the PWM frequency for less ripple and a faster response. Load the output lightly, or buffer it with an op-amp. (The Uno R4 has a true DAC on A0 if you need a real analog output.)

Common Mistakes

MistakeWhat you seeFix
Motor wired straight to a pinBoard resets, pin diesUse a MOSFET or motor driver
analogWrite() on a non-~ pinLED snaps on at 128 instead of fadingMove to 3, 5, 6, 9, 10 or 11
L298N ENA jumper still fittedSpeed control does nothingRemove the jumper, wire ENA to a ~ pin
No common groundMotor ignores the signal or behaves randomlyJoin the motor supply GND to Arduino GND
No flyback diode on a bare MOSFETMOSFET fails, random resetsDiode across the motor, stripe to +
Servo library plus PWM on 9/10PWM on 9/10 stopsUse pins 3, 5, 6 or 11
Changed Timer0 registersdelay() and millis() wrongOnly change Timer1 or Timer2
Motor hums but won't turnDuty too low to overcome frictionKick-start at 255, or raise the minimum duty
Standard MOSFET (IRF540N) gets hotNot fully on at 5 V gateUse a logic-level MOSFET

FAQ

What is the range of analogWrite()? 0 to 255 on the Uno, Nano and Mega (8-bit). On the Uno R4, ESP32 and Pico you can set a higher resolution.

Is PWM a real analog output? No. It's a fast on-off signal whose average acts like a voltage. Add an RC filter for a steady DC level.

Which pins are PWM on an Arduino Uno? 3, 5, 6, 9, 10 and 11, marked with ~.

What is the default PWM frequency? About 490 Hz on pins 3, 9, 10, 11 and about 980 Hz on pins 5 and 6.

Can I control a servo with analogWrite()? No. Use the Servo library, which produces the 50 Hz pulses a servo expects.

Why does my motor whine? The PWM frequency is in the audible range. Raise it to about 31 kHz on pins 9 and 10 with the Timer1 code above.

Where to Go Next

PWM is what turns a blinking-LED project into something physical: a fan that ramps up, a lamp that fades, a robot that drives. Good next steps:

Continue learning

Related articles

All articles

Signal In

Built from what makers search for

The lookups piling up in SolderHub's search bar every day shape what we publish next — new component pages, board guides, and firmware notes, sent out as they go live.

By subscribing you agree to our Privacy Policy. Unsubscribe anytime.