Introduction to AVR Microcontroller Pulse Width Modulation (PWM) Using Atmega

Schematic Introduction to AVR

PWM is used in many industrial mostly for controlling the motor speed. The PWM is used because it’s the most efficient method comparing to the analog one. That’s why most of the modern microcontrollers today have this features build in. How does this PWM works will be described on the following circuits:

 Schematic Introduction to AVR

On the figure 1A the motor is controlled by applying continues current through transistor base lead and this will cause the continues current flow through transistor’s collector lead by the factor of transistor’s gain (Ic = gain x Ib); the more current we put on the transistor’s base lead the more current Ic will flow through the transistor’s collector lead; this also mean the motor speed is depend on the current we put on transistor’s base lead. Using this kind of method, make the transistor always in the working stage and as a result the power dissipation (power lost as heat) on the transistor become very high.
On the other circuit figure 1B the motor is controlled by applying pulse current through the transistor’s base lead and this will cause the Ic pulse current flow through transistor’s collector lead. By varying the pulse width and using the right frequency, we could control the motor speed. Using the pulse current make the transistor on and off very quickly, this will reduce much of the power dissipation on the transistor. This kind of method is known as pulse width modulation or PWM for short.
The disadvantage of using the PWM method is the electronics circuit to produce this signal tend to be complex, but thanks to today’s technology; even the most cheapest and tiny microcontroller such as AVR Tiny13 (only have 8 pins) could produce this PWM easily. For the purpose of this tutorial I will use Atmel AVR Tiny2313 microcontroller for playing with the PWM, but you could apply the principal to almost all AVR microcontroller family.
The following is the list of hardware and software I use in this tutorial:

  1. AVRJazz Tiny2313 learning board from ermicro
  2. JazzMate AVR-ISP STK200/300 programmer board from ermicro
  3. WinAVR for the GNU’s C compiler
  4. Atmel AVR Studio 4 for the coding, compiling and debugging environment
  5. Kanda AVRISP STK200/300 for the programmer software; nicely plug in to the Atmel AVR Studio 4.14
 Introduction to AVR Microcontroller

The AVRJazz Tiny2313 has four LEDs attached to the PORT B0 through PORT B3 and we will use the LED on PORT B3 for outputting the PWM signal. Ok let’s start the coding now !

The first statement is to instruct the microcontroller to enable the output for PORTB, this can be achieved by setting all the port B data direction register DDRB to 0xFF (0b11111111). Now the next two statements is to enable the AVR ATTiny2313 microcontroller for 8 bit Fast PWM mode; the following diagram is a simple version of how the PWM is generated in AVR ATTiny2313 microcontroller (for the complete PWM diagram version please refer to the datasheet):

Read more: Introduction to AVR Microcontroller Pulse Width Modulation (PWM)

Leave a Comment

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