AVR Introduction to PWM Part I

Hi friends, today we are going to start a very important topic PWM, this tutorial is going to be of two parts. In first part we are going to discuss about basics and principles behind PWM. In second part we are going to write code for Atmega16 controller.

A Pulse Width Modulation (PWM) Signal is one way to represent an analog signal in the digital or in other words a modulation technique that uses a digital circuit to create a variable analog signal.


For the digital signal, the frequency is set and the duty cycle varies according to the analog signal’s amplitude. For example, if it is known that an analog signal has a range from 0 to 12V then a duty cycle range of 0% to 100% could be associated with this signal. When the signal is at 2.4V, the corresponding digital signal’s duty cycle is 20%. When the analog signal is at 7.2V, the digital signal’s duty cycle is at 60%. The relation between analog amplitude and digital pulse widths will vary according to different applications.

Following  parameters describe about 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.

Frequency typically is set once and is not changed during program execution, except cases when variable frequency signal is needed, e.g. while 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.

Most common usage of PWM in amateur AVR applications:

  • Motor speed control,
  • Brightness control,
  • Servo control,
  • Simple digital to analog converter,
  • Generation of sound.
  • UPS
  • Inverters
  • Flash Light

Hardware controllers

Many microcontrollers include PWM controllers. For example, AVR ATmega16 includes four PWM channels. The duty cycle is the ratio of the on-time to the period; the modulating frequency is the inverse of the period. To start PWM operation, the data sheet suggests the software should:

  • Set the period in the on-chip timer/counter that provides the modulating square wave.
  • Set the on-time in the PWM control register.
  • Set the direction of the PWM output, which is one of the general-purpose I/O pins.
  • Start the timer.
  • Enable the PWM controller.

Although specific PWM controllers do vary in their programmatic details, the basic idea is generally the same.


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

Friend’s if you like this article, please click like button on facebook…subscribe to this blog and receive notifications of new posts by email. ..Thanks!


Related posts:

About author

This article was written by admin

Admin has over twenty years experience in the electronics industry, largely dedicated to embedded software. A frequent presenter at conferences and seminars and author of numerous technical articles. Working presently as Development Manager in India. A firm Believer in Knowledge grows when it shared.


Comments (4)
  1. sanjay suri says - Posted: December 28, 2012

    This is one of the best tutorial i have ever came across…keep it UP!

  2. subrahmanyam says - Posted: February 5, 2013

    waitng for the second part….!

  3. Nashid zai says - Posted: September 28, 2015

    yes waiting for 2nd part

  4. Leolol204 says - Posted: December 30, 2016

    again waiting for 2nd part!!… 30/12/2016

Leave your comment

Your email address will not be published. Required fields are marked *