Sub topic 11
PWM Frequency and the Timers Behind It
Assume you know Arduino sketches, pinMode(), digitalWrite(), and basic PWM duty cycle. This chapter focuses on the Arduino Uno’s hardware timers: the circuits that create the repeated PWM waveform.
Frequency is separate from duty cycle
PWM has two independent properties:
- Frequency is how many complete on-and-off cycles occur each second. It is measured in hertz (Hz).
- Duty cycle is the percentage of each cycle that the signal stays HIGH.
For example, a 50% duty cycle at 500 Hz and a 50% duty cycle at 5 kHz deliver the same ideal average power. Their switching rates differ by a factor of ten.
The Arduino Uno runs its ATmega328P microcontroller at 16 MHz. Its timer peripherals count this clock and change output pins automatically. The processor does not need to execute a line of code for every HIGH or LOW transition.
The Uno's PWM timers
The Uno has three timers. Each timer controls specific output pins and has its own behavior.
| Timer | PWM pins | Typical `analogWrite()` frequency | Other common uses |
|---|---|---|---|
| Timer0 | 5 and 6 | About 980 Hz | `millis()`, `micros()`, and `delay()` timing |
| Timer1 | 9 and 10 | About 490 Hz | Servo libraries and 16-bit timing |
| Timer2 | 3 and 11 | About 490 Hz | `tone()` and other timing tasks |
The frequency values are approximate because the timer configuration and PWM mode matter. On a standard Uno Arduino core, pins 5 and 6 use fast PWM. Pins 3, 9, 10, and 11 normally use phase-correct PWM.
This difference explains why pins 5 and 6 usually switch about twice as fast as the other PWM pins.
How a timer creates PWM
A timer repeatedly counts from zero to a maximum value. With 8-bit PWM, that maximum is usually 255. The timer compares its current count with the duty value supplied by analogWrite().
For a duty value of 64:
- The timer starts a new cycle.
- The output is HIGH while the counter is below 64.
- The output becomes LOW for the rest of the cycle.
- The counter wraps around and repeats.
The timer clock is divided by a prescaler. A prescaler is a hardware divider that makes the timer count more slowly than the 16 MHz processor clock.
For fast 8-bit PWM, the approximate frequency is:
frequency = clock frequency / (prescaler × 256)
For Timer0, the standard prescaler is 64:
16,000,000 / (64 × 256) ≈ 976 Hz
Phase-correct PWM counts up and then down. That takes twice as many timer counts, so its frequency is approximately half as high:
16,000,000 / (2 × 64 × 255) ≈ 490 Hz
Why timer conflicts matter
A timer is a shared resource. If a library changes a timer’s mode, prescaler, or compare registers, it can change the PWM signal on that timer’s pins.
Common examples include:
millis(),micros(), anddelay()depend on Timer0. Changing Timer0 can break their timing.- The Servo library normally uses Timer1. PWM on pins 9 and 10 can stop working or behave differently while that library is active.
tone()normally uses Timer2. PWM on pins 3 and 11 can be affected while the tone is active.
The conflict is not caused by calling analogWrite() with a different brightness. The duty cycle changes, but the timer’s normal frequency remains the same. The conflict happens when another program or library reconfigures the timer itself.
Choosing a PWM frequency
For an indicator LED, the Uno’s default frequencies are usually suitable. The LED switches too quickly for most people to see individual pulses.
Choose a different frequency only when the load or application requires it:
- A higher frequency can reduce visible flicker and camera banding.
- A motor may make an audible whine at some PWM frequencies.
- Higher switching rates can increase electrical losses and electromagnetic interference.
- Changing the frequency can reduce timer resolution or interfere with Arduino timing functions.
For simple LED fading, keeping the default timer settings is usually the safest choice. Select a PWM-capable pin that does not share a timer with a library your project needs.
const uint8_t LED_PIN = 9; // Timer1 PWM on an Uno
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
for (int level = 0; level <= 255; level++) {
analogWrite(LED_PIN, level);
delay(5);
}
for (int level = 255; level >= 0; level--) {
analogWrite(LED_PIN, level);
delay(5);
}
}This sketch changes the duty cycle on Timer1. It does not change the approximately 490 Hz PWM frequency. If you add a Servo library, test this pin again because Timer1 may no longer be available for normal PWM.
Exercise
On a standard Arduino Uno, which timer controls pin 9, and what can happen if a Servo library uses that timer?
Show answer
Timer1 controls pin 9. A Servo library can reconfigure Timer1, causing PWM on pin 9 and pin 10 to stop or behave differently.