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:
- 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.
- Flash Light
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!