Sub topic 5
Finding the PWM Pins on Your Board
This chapter assumes that you know the basic Arduino sketch structure, pinMode(), digitalWrite(), breadboard wiring, and introductory Ohm’s law. You should also know that an Arduino pin can be configured as an input or output.
PWM, or pulse-width modulation, uses a digital pin that switches quickly between LOW and HIGH. Only some pins have hardware support for this feature. Before using analogWrite(), find the PWM-capable pins for your exact board.
Read the markings on the board
On an Arduino Uno, PWM pins are marked with a tilde symbol, ~, beside the pin number. The PWM pins are:
| Board | Common PWM-capable pins | Marking or note |
|---|---|---|
| Arduino Uno R3 | 3, 5, 6, 9, 10, 11 | Look for `~` beside each number |
| Arduino Nano with ATmega328P | 3, 5, 6, 9, 10, 11 | Similar pin mapping to the Uno |
| Arduino Mega 2560 | 2–13 and 44–46 | Check the board labels and documentation |
| Arduino Leonardo | 3, 5, 6, 9, 10, 11, 13 | Pin mapping differs from the Uno |
The tilde is the most useful clue on an Uno or Nano. A pin without that marking can still perform ordinary digital output, but it usually cannot produce hardware PWM through analogWrite().
Board families can have different pin layouts. A Nano Every, Micro, Mega, and third-party board may not use the same PWM pins as an Uno. Read the pin labels printed on the board, then confirm them in the board’s technical documentation.
Do not confuse PWM with analog input
Pins labeled A0, A1, and similar names are analog input pins. They measure changing voltages with an analog-to-digital converter. They are not automatically PWM output pins.
The word “analog” in analogWrite() is also slightly misleading. On most classic Arduino boards, the function does not create a steady analog voltage. It creates a PWM signal on a supported digital pin. The output changes rapidly between 0 V and the board’s logic voltage.
For example, this call is suitable for an LED connected to Uno pin 9:
analogWrite(9, 128);The first argument is the pin number. The second argument is the PWM level, normally from 0 to 255. A value of 0 keeps the output LOW. A value of 255 keeps it HIGH. A value of 128 produces a signal that is HIGH for about half of each cycle.
Check the pin in a sketch
Use a named constant instead of scattering a number through your program. This makes it easier to move the LED if you later discover that the selected pin is not PWM-capable.
const uint8_t LED_PIN = 9;
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
analogWrite(LED_PIN, 64);
delay(1000);
analogWrite(LED_PIN, 192);
delay(1000);
}On an Uno, the LED should appear dimmer at 64 than at 192. Connect the LED in series with a current-limiting resistor. The resistor protects the LED and the Arduino output pin.
The sketch does not prove that every board supports pin 9. It demonstrates the correct use of a known PWM pin for an Uno. If your board uses another mapping, replace LED_PIN with a PWM-capable pin from its documentation.
Understand timer-related restrictions
PWM pins are controlled by hardware timers inside the microcontroller. A timer repeatedly generates the HIGH and LOW portions of the signal. This lets the processor perform other work instead of switching the pin manually.
A timer may also control another feature, such as millis(), tone(), or a library that generates servo signals. Changing timer settings can alter PWM behavior or interfere with that feature. For simple LED fading, use the board’s normal Arduino functions and avoid changing timer registers until you understand the consequences.
Exercise
You have an Arduino Uno and want to fade an LED with analogWrite(). Which of these pins is suitable: 2, 6, 8, or A0? Explain why.
Show answer
Pin 6 is suitable because it is marked as a PWM pin on the Uno. Pins 2 and 8 are ordinary digital pins, and A0 is an analog input rather than a PWM output.