Projects for geeks

Aquaticus PWM guide

11 Aug 2009
PWM wave


This guide describes principles of PWM for Atmel AVR micro controllers. It is not complete documentation for PWM nor AVR timers but tries to lighten some practical aspects of PWM. Some of the PWM aspects are simplified, some are not mentioned at all. Usability was the main factor when writing this text. Full PWM description can be found in Atmel AVR documentation.

Although the article was written based on ATmega16 and ATmega32 documentation, information is useful for any chip from AVR family.


In the table below you have symbols used in the text. The meaning of symbols is the same in the entire guide.

Symbol Description
F System clock frequency. Typically it is frequency of crystal resonator or internal RC oscillator. Maximum system clock for ATmega32 is 16 MHz (=16,000,000 Hz).
fclock The same as F.
PWM Frequency of PWM wave. This frequency is always lower then system clock frequency.
fPWM The same as PWM.
N Prescaler divider, possible values: 1, 8, 32, 64, 128, 256 or 1024.
TOP Maximum counter value, often equal to ICRx (but it can be OCRx). Controls frequency of the PWM wave in specified PWM modes.
Hz Hertz, basic unit of frequency. 1 Hz is equal to one cycle per second.
kHz kilohertz, 1 kHz is equal to 1,000 Hz or 1 × 103 Hz.
MHz megahertz, 1 MHz is equal to 1,000,000 Hz or 1 × 106 Hz.
ms millisecond, 1 ms = 0.001 s or 1 × 10-3 s. 1 ms = 1000 µs.
µs microsecond, 1 µs = 0.000001 s or 1 × 10-6 s. 1 µs = 0.001 ms.

What is PWM

PWM (Pulse Width Modulation) is a method of generating signal shown above. For more detailed definition look at Wikipedia article.
In short, PWM allows easy control the amount of power provided to external device e.g. motor or LED. When frequency of the signal is changed in time it can be used to generate sound.

Basic definitions

Time of one period is equal to 1/frequency, e.g. for 100kHz you get 1/100,000=0.00001s (=0.01ms=10µs), for 2kHz (2000Hz) period is 1/2000 = 0.0005s or 0.5ms.

There are 3 parameters that describes PWM signal:

  • Amplitude
  • Frequency
  • Duty cycle

Amplitude is constant in time. For AVR controllers (without external components) it is equal to power supply voltage. For typical 5V power supply amplitude is of course 5V, for 1.8V it is 1.8V and so on (AVR operating voltage can vary from 1.8V to 5.5V depending on chip version).

Frequency typically is set once and is not changed during program execution, except cases when variable frequency signal is needed, e.g. when generating sound.
Frequency is equal to 1/period. Having period, frequency can be computed and vice versa.

Duty cycle controls amount of power provided to external component. This parameter is one that is changed many times during program execution. Changing duty cycle does not change frequency. It is often provided in percents, e.g. 60% duty cycle means output is high for 60% of the period. If the period is equal to 10µs (=10kHz), output is high for 6µs.
Sometimes described as pulse width in seconds (or ms or µs).

Most common usage of PWM in amateur AVR applications:

  • motor speed control,
  • brightness control,
  • servo control,
  • simple digital to analog converter,
  • generation of sound.