In this project we are going to use one of the features of ATmega32A to adjust the brightness of 1Watt LED. The method that is used to adjust the speed of LED is PWM (Pulse Width Modulation). The method of PWM is explained here. Consider a simple circuit as shown in figure.
Now if the switch in above figure is closed continuously over a period of time then the bulb will continuously on during that time. If the switch is closed for 8ms and opened for 2ms over a cycle of 10ms, then the bulb will be ON only in the 8ms time. Now the average terminal over across the over a period of 10ms = Turn ON time/ (Turn ON time + Turn OFF time), this is called duty cycle and is of 80% (8/ (8+2)), so the average output voltage will be 80% of the battery voltage.
In the second case, the switch is closed for 5ms and opened for 5ms over a period of 10ms, so the average terminal voltage at the output will be 50% of the battery voltage. Say if the battery voltage is 5V and the duty cycle is 50% and so the average terminal voltage will be 2.5V.
In the third case the duty cycle is 20% and the average terminal voltage is 20% of the battery voltage.
In ATMEGA32A we have four PWM channels, namely OC0, OC1A, OC1B, and OC2. Here we are going to use OC0 PWM channel to vary the brightness of the LED.
Power supply (5v)
Buttons (2 pieces)
100nF (104) capacitor (2 pieces),
100Ω and 1kΩ resistors (2 pieces).
Atmel studio 6.1
Progisp or flash magic
Circuit Diagram and Working Explanation
The above figure shows the circuit diagram of LED dimmer.
In ATmega, for four PWM channels, we have designated four pins. We can only take PWM output on these pins only. Since we are using PWM0 we should take PWM signal at OC0 pin (PORTB 3rd PIN). As shown in figure we are connecting the base of transistor to OC0 pin to drive the power LED. Here another thing is over four PWM channels, two are 8-bit PWM channels. We are going to use a 8-bit PWM channel here.
A capacitor is connected to each of the buttons to avoid bouncing. Whenever a button is pressed there will be some noise at the pin. Although this noise stabilizes in milliseconds. For a controller the sharp peaks before stabilization acts as triggers. This effect can be eliminated either by software or hardware, for the program to be simple. We are using hardware method by adding debouncing capacitor.
The capacitors nullify the effect of bouncing of buttons.
In ATMEGA there are couple of ways to generate PWM, they are:
1. Phase correct PWM
2. Fast PWM
Here we are going to keep everything simple, So we are going to use FAST PWM method to generate the PWM signal.
First to choose the frequency of PWM, This depend on application usually, for an LED any frequency greater than 50Hz would do. For this reason we are choosing the counter clock 1MHZ. So we are choosing no prescalar. A prescalar is a number which is so selected to get a lesser counter clock. For example if the oscillator clock is 8Mhz, we can chose a prescalar of ‘8’ to get a 1MHz clock for counter. The prescalar is selected based on frequency. If we want more time period pulses we have to chose higher prescalar.