Pulse Width Modulation in AVR Atmega32A Microcontroller

This tutorial is about how to use pulse width modulation in AVR Atmega32A Microcontroller. PWM(Pulse Width Modulation) is an interesting phenomenon which used to generate analog signals from digital device. AVR microcontroller has dedicated hardware for PWM signal generation which reduce the load of generation of PWM signal from software. It is something similar to tell internal hardware of microcontroller to generate PWM signal of specific duty cycle. There are many different applications are associated with PWM for example brightness control of LED, controlling servo or DC motors, sound output, producing approximated analog waveform.

In broader sense the period of PWM is what creates a frequency and it represented as length of time. The period is the place where digital signal count to be high and then goes low and the proportion between is called “duty cycle”. The period is selected at beginning and does not change. Usually it decided as, if period is longer then it represents slower frequency and if period is shorter represent higher frequency (fast). Usually it supposed that frequency of PWM is how many periods can fit within one second. There are three different modes available to general pulse width modulation:

  • Fast PWM
  • Phase Correct PWM
  • Frequency and Phase Correct PWM
pulse width modulation in ATmega32A

Just because lack of time we are not discussing about each one in detail. Maybe in future blog post and article will cover more modes, here in this case we are using fast PWM mode with AVR microcontroller’s 8-bit TIMER 0. We have chosen TIMER 0 because it’s an 8-bit with maximum count 255. This is enough to implement classic example of dimming or brightness control for LED. Other hand TIMER 1 is 16-bit with maximum count 65535 with two registers TCCR1A & TCCR1B which introduce more complexity for beginner. To keep simplicity for beginner to understand concept, I have chosen Timer 0. In this example we are going to connect green LED with current limiting resistor 330Ω to “PB3” pin of ATmega32A.

Circuit Schematic: Pulse Width Modulation In AVR Atmega32A Microcontroller

Click on Image for better picture

Pulse Width Modulation In AVR Atmega32A Microcontroller
LED Dimming with AVR Atmega32A

This video is step-by-step guide with code explanation:

C Program: PWM In AVR Atmega32A

This is it for this tutorial and I hope now you can use PWM in AVR Atmega32A Microcontroller. In next tutorial, we will learn how to interface character LCD with AVR ATmega32A Microcontroller. If you have any questions then feel free to leave a comment.

 Buy AVR ATmega32 Microcontroller Kit

Buy AVR ATmega32 Microcontroller Kit

About Umesh Lokhande

Umesh Lokhande holds a Master degree in Scientific Instrumentation from University of Applied Sciences Jena, Germany. and has previously worked at Orbotech, Alere Technologies etc. Umesh is also a founder and first author of BINARYUPDATES.COM


  1. Vishal G.Meshram

    Hello sir ,
    I am Vishal, working on 4×4 ledcube, and i want to controll the intensity of the cube, thus chose the option to apply fast PWM. but got stuck at a point of giving it to entire port, I am using PORTA and PORTB for anodes, and PORTD for cathode.
    therefore need your help ,
    how should i apply this PWM to entire PORT ,


  2. which are dedicated pwm pin in atmega32a

Leave a Reply

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


Register | Lost your password?