Build note
PWM: switching too fast to see
A pin is always either high or low. PWM changes only how long it stays high in each cycle. Flick the switch faster than the eye can follow and the room looks half-lit, even though the bulb is fully on or fully off at every instant.
Period, duty, frequency
- Period is the length of one cycle.
- Duty cycle is the fraction of the period spent high.
- Frequency is cycles per second, the inverse of the period.
A 25 percent duty at 5 V delivers the same average power as a steady 1.25 V, but the pin never sits at 1.25 V. For an LED that looks like dimming. For a motor it looks like a slower speed.
Why Timer1
Timer1 is 16-bit, so it counts to 65535. That matters when you want a low frequency with fine resolution. An 8-bit timer gives 256 duty steps, and the frequency drops into visible territory as soon as the period gets long.
The output appears on OC1A, which is the same physical pin as PB1 (chip pin
15, Arduino D9). Two separate things must be true before the waveform leaves the
chip: the timer must be connected to the pin through the COM1A bits, and DDRB
must make the pin an output. Skip either and the timer runs, but nothing happens
outside.
Mode 14 with ICR1 as TOP
TCCR1A = (1 << WGM11) | (1 << COM1A1);
TCCR1B = (1 << WGM13) | (1 << WGM12) | (1 << CS11);
ICR1 = 19999;
OCR1A = 5000;
WGM13:0 = 1110is fast PWM withICR1as the top value.CS11selects a prescaler of 8, so the timer runs at 2 MHz and one tick is 0.5 microseconds.ICR1sets the period: 20,000 ticks at 0.5 microseconds is 10 ms, or 100 Hz.OCR1Asets the high time. 5,000 ticks is 25 percent.
| Register | Page | Job |
|---|---|---|
TCCR1A |
108 | Compare output mode, low waveform bits |
TCCR1B |
110 | High waveform bits, prescaler |
ICR1 |
112 | The top value, used as the period |
OCR1A |
111 | The compare value, used as the duty |
Using ICR1 as the top frees OCR1A to be the PWM output. If you use OCR1A as
the top, you cannot also use it for the waveform, which is a confusing datasheet
sentence until you hit it.
Resolution versus frequency
The trade-off is arithmetic. At a fixed timer clock, a longer period gives more duty steps but a lower frequency.
At 2 MHz and a 100 Hz period there are 20,000 ticks, so the duty can be set in 0.005 percent steps. Push the frequency up and the steps get coarser. Push it down and the LED starts to visibly flicker, especially on a phone camera.
For an LED, 100 Hz is fine to the eye. For a motor, a few kilohertz keeps it quiet. For a servo, the frequency is fixed at 50 Hz by the protocol.
Reading the potentiometer
The last example in the folder ties the ADC from Part 3 to the PWM:
uint16_t adc = adc_read(); // 0..1023
OCR1A = (uint32_t)adc * ICR1 / 1023; // 0..19999
Turn the pot and the LED brightens and dims. The conversion is a straight linear
map, and the uint32_t cast matters: adc * ICR1 exceeds 16 bits, so without it
the result overflows and the brightness jumps around.
This example also uses an ADC prescaler of 128, which is the accurate setting the ADC part recommended.
The failures that cost time
- No
DDRB. The timer generates a perfect waveform that never reaches the pin. - Wrong
COM1Abits. The pin stays disconnected from the timer. - Overflow in the duty calculation. The multiply needs 32 bits.
- Too low a frequency. The LED flickers and the camera sees banding.
How this was made
- Written by: Rupesh, from his own
c-07-pwmnotes and code, with AI drafting and editing. - Numbers: from the ATmega328P datasheet and the timer arithmetic.
- AI-assisted sections: structure and copy editing.