Sub topic 5
Finding the PWM pins on your board
This chapter assumes you can write a basic sketch with setup() and loop(), wire an LED with a resistor on a breadboard, and read an Arduino pinout diagram. The earlier chapters covered duty cycle and analogWrite(). Here you will learn which pins can produce PWM, how to confirm that in software, and what goes wrong when you pick a pin that can't.
Why only some pins do PWM
analogWrite() only works as PWM on pins connected to a hardware timer. Inside the microcontroller, a timer is a counter that runs on its own, driven by the clock. It compares its count to a stored number and flips an output pin when they match. This happens in hardware, so the CPU does no work while the signal runs. Only a few pins are wired to a timer's output comparator, which is why only a few pins can do PWM.
On the Arduino Uno, the ATmega328P chip has three timers, and each can drive two pins:
| Timer | Width | Uno pins | Default PWM frequency |
|---|---|---|---|
| Timer0 | 8-bit | 5, 6 | about 980 Hz |
| Timer1 | 16-bit | 9, 10 | about 490 Hz |
| Timer2 | 8-bit | 3, 11 | about 490 Hz |
That gives six PWM pins: 3, 5, 6, 9, 10 and 11. Pins 5 and 6 run at roughly twice the frequency of the others because Timer0 counts in a different mode. The Arduino documentation rounds the Timer0 figure to 980 Hz. The exact value is 976.56 Hz.
A later chapter covers frequency and timers in detail. For now, remember that each PWM pin belongs to a specific timer, and that matters when two features want the same timer.
Reading the board
Most Arduino boards mark PWM-capable pins with a tilde (~) printed next to the pin number. On an Uno, look along the digital header for ~3, ~5, ~6, ~9, ~10 and ~11. Pins 2, 4, 7, 8, 12 and 13 have no tilde.
The analog pins A0 to A5 are not PWM pins. They feed the ADC (analog-to-digital converter) and can also work as plain digital pins, but they have no timer output.
Pin counts and positions differ between boards, so check yours before wiring:
| Board | PWM pins |
|---|---|
| Uno (R3) | 3, 5, 6, 9, 10, 11 |
| Nano | 3, 5, 6, 9, 10, 11 |
| Leonardo | 3, 5, 6, 9, 10, 11, 13 |
| Mega 2560 | 2 to 13, and 44 to 46 |
The Nano uses the same chip as the Uno, so its PWM pins match. Boards based on other chips, such as the ESP32 or the Uno R4, can differ in the number of PWM pins, the available resolution and the frequency. Read the pinout diagram for your exact board. If you port a sketch between boards, check the pin choice first.
Checking in software
The tilde is easy to misread on a small board. The Arduino core can also tell you. Each board definition provides a macro called digitalPinHasPWM(pin). It is true when the pin supports PWM. The sketch below lists every PWM-capable pin on whatever board you compile it for.
void setup() {
Serial.begin(9600);
Serial.print(F("PWM-capable digital pins:"));
for (uint8_t pin = 0; pin < NUM_DIGITAL_PINS; pin++) {
if (digitalPinHasPWM(pin)) {
Serial.print(' ');
Serial.print(pin);
}
}
Serial.println();
}
void loop() {
// Nothing to do after the list is printed
}Open the Serial Monitor at 9600 baud. On an Uno, the output is:
PWM-capable digital pins: 3 5 6 9 10 11NUM_DIGITAL_PINS is the number of pins the core numbers as digital pins. On the Uno that is 20, because A0 to A5 count as digital pins 14 to 19. None of those six are PWM pins, so they never appear in the list.
On some boards, digitalPinHasPWM may not be defined by the board package. If the sketch fails to compile with an "not declared" error, use the pinout diagram instead.
What happens on a non-PWM pin
The most common mistake is calling analogWrite() on a pin with no timer. The call does not raise an error and the sketch compiles. On AVR boards such as the Uno, the core falls back to digital behavior:
- A value below 128 sets the pin LOW.
- A value of 128 or more sets the pin HIGH.
So an LED on pin 8 fed a rising value from 0 to 255 stays dark, then jumps to full brightness halfway through. There is no fade. The sketch below makes the difference visible. Wire an LED and a 220 Ω resistor from the chosen pin to GND, then switch LED_PIN between 9 and 8 and compare.
const uint8_t LED_PIN = 9; // change to 8 to see the non-PWM behavior
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
for (int value = 0; value <= 255; value++) {
analogWrite(LED_PIN, value);
delay(10);
}
for (int value = 255; value >= 0; value--) {
analogWrite(LED_PIN, value);
delay(10);
}
}On pin 9 the LED fades up and down smoothly. On pin 8 it switches on at 128 on the way up and off at 127 on the way down.
The special values 0 and 255 are handled the same way on PWM pins. On AVR boards, analogWrite(pin, 0) holds the pin constantly LOW and analogWrite(pin, 255) holds it constantly HIGH. The core does this so the output can be a true steady level, not a very narrow pulse train.
Pins that do double duty
Some PWM pins are also used by built-in features. Before you choose a pin, check that nothing else in your project needs it.
| Pin | Other role on the Uno |
|---|---|
| 3 | External interrupt INT1 |
| 5, 6 | None, but Timer0 also keeps time for `millis()` and `delay()` |
| 9, 10 | Pin 10 is the SPI chip-select (SS) line |
| 11 | SPI data out (MOSI) |
Pins 0 and 1 are the serial port, and they are not PWM pins. Pin 13 has the on-board LED and is the SPI clock (SCK). It is not a PWM pin on the Uno, though it is on the Leonardo.
If your project uses an SPI device such as an SD card reader, a display or a radio module, avoid pin 11 for PWM and be careful with pin 10. Choose from 3, 5, 6 or 9 instead.
Timer conflicts
Because PWM pins share timers, a library that takes over a timer can silently disable analogWrite() on the pins attached to it. Two cases come up often on the Uno:
- The
Servolibrary uses Timer1, soanalogWrite()stops working on pins 9 and 10 while it is active. - The
tone()function uses Timer2, so it interferes with PWM on pins 3 and 11.
If your LED fades correctly until you add a servo or a buzzer, a timer conflict is a likely cause. Move the LED to a pin on a different timer. With a Servo in use, pins 3, 5, 6 and 11 are still free. With tone() in use, pins 5, 6, 9 and 10 are free.
A pin-selection checklist
When you pick a pin for a fading LED, work through these steps in order:
- List the PWM pins for your board, from the tilde marks or the pin-listing sketch.
- Remove any pins your other hardware needs: SPI, I2C, serial, interrupts.
- Remove pins whose timer a library you use will take over.
- From what is left, prefer one on a timer you will not reconfigure.
- Write the pin number once, as a named constant, so a later move to another pin is a one-line change.
The last step is why every sketch in this tutorial starts with const uint8_t LED_PIN = 9;. Pin 9 is a sensible default on the Uno. It is PWM-capable, it is not part of SPI, and its timer is not the one that keeps time.
Exercise
You wire an LED to pin 7 of an Uno and run a fade loop that calls analogWrite(7, brightness) while brightness rises from 0 to 255. What do you see, and what is the fix?
Show answer
Pin 7 has no timer output, so the core treats the call as a digital write. The LED stays off while the value is below 128 and turns fully on at 128 or more, with no gradual fade. Move the LED to a PWM pin such as 9 (and update the pin constant), or choose another pin marked with a tilde.
Exercise
Your Uno project fades an LED on pin 10 and also drives a hobby servo with the Servo library. The fade stops working once the servo is attached. Why, and which pins could you use instead?
Show answer
The Servo library uses Timer1, which also generates PWM for pins 9 and 10, so analogWrite() no longer works on them. Move the LED to a pin on another timer: 3, 5, 6 or 11. If you also use SPI, prefer 3, 5 or 6 because pin 11 is MOSI.