This article is another step forward in learning more about AVR microcontrollers. We have demonstrated 3 simple LED based projects using ATmega328 microcontroller, which will help you to learn its basic concepts. ATmega328 is an eight bit AVR (Advanced Virtual RISC) based microcontroller. It is a powerful microcontroller with a built-in Â internal memory of around 32Kb. Most Arduino boards consist of an Atmel 8-bit AVR microcontroller with varying amounts of flash memory, pins, and features. Arduino Uno is a microcontroller board based on the ATmega328.
AVR microcontrollers are very easy to use. All AVR microcontrollers require Integrated Development Environment(IDE) such as Atmel Studio. Using this IDE, we can create, compile and debug program on all AVR microcontrollers.
Letâ€™s develop simple LED Blinking programs for ATmega328 using Atmel Studio 7.
- Blinking Two LEDâ€™s using ATmega328
- Control Two LED’s using a Push button switch
- Toggle Two LEDâ€™s using a Push button switch
#1.Â Blinking Two LEDâ€™s using ATmega328
Â In this section, we will learn How to blink two LEDs with AVR ATmega328 microcontroller. First, we will connect the 2 LED’s with PB2 and PB3 ofÂ PORTB of the ATmega328 microcontroller. Then, we will make the 2 LED’s to blink with an interval of 1 second. It means, initially the 1st LED alone will glow and on the next second, it will turn off and the 2nd Â one will glow. This process continues forever and in this way LEDs blinks continuously.
Assemble the circuit as shown in diagram. A photograph of the assembled circuit is shown below. A video demonstration of the project is shown below.
Blinking Two LEDâ€™s Using ATmega328 -Â Download Program
Â At the beginning of the program a pre-processor named â€œF_CPUâ€ is defined. It is simply your way to tell some of the library code how many CPU cycles per second the processor is executing. Here we defined the F_CPU as 1 MHz. â€œ#include <avr/io.h>â€ is a header files which provides you with various Â i/o operations like DDRx, PINx, PORTx, etc. â€œ#include <util/delay.h>â€ is a header file which provides you with inbuilt delay functions like _delay_ms(), _delay_us(), etc. â€œ_delay_ms(1000)â€ provides a delay of 1000 milliseconds (i.e., equivalent to 1 second).
DDRx â€“ Data Direction Register configures data direction of the port(Input/Output). The instruction â€œ DDRB |= (1<<DDB2)â€Â makes corresponding port pin as output.
PORTx â€“ Port register is for assigning appropriate values for the port pins.
Writing to PORTx.n will immediately change state of the port pins according to given value. â€œPORTB |=(1<<PORTB2)â€ will generate a high signal at PB2. And â€œPORTB&=~(1<<PORTB3)â€ is for generating a low signal at PB3.
#2. Control Two LED’s using a Push button switch
Â Here we are going learn how to control the working of two LEDâ€™s using a push button switch. Similar to the earlier circuit, first we will connect the 2 LEDs with PB2 and PB3 ofÂ PORTB of the microcontroller. A push button switch is then attached to PB0 pin and pulled-up using a 10K resistor. The remaining terminal of the switch is grounded. The function of a pull-up resistor is to insure that while leaving the switch as not pressed, the status of the PB0 pin should remain high. There are 20K pull-up resistors built into the ATmega chip that can be accessed from software also. But hereÂ we are using an external pull-up circuit. When the switch is pressed, the two LEDâ€™s will glow and will turn off while we release the switch. This is how the circuit will work.
Assemble the circuit as shown in diagram. A photograph of the assembled circuit is also shown above. A video demonstration of the project is shown below.
Control 2 LED’s Using Push Button Switch – Download Program
Â We have already discussed about the pre-processors and libraries at the earlier section. The only thing that is newly included in this code is the assigning of PB0 pin as input port and the â€œifâ€ loop associated with that pin.
â€œDDRB &= ~(1 << DDB0)â€ is the code used for assigning PB0 as input port. An â€œifâ€ loopÂ is included in the â€œwhileâ€ loop, which will continuously monitor the status of the PB0 pin and alter the status of the two LEDâ€™s accordingly.
PIN register is used to read data from port pins, when port is configured as input. â€œ!(PINB&(1<<PINB0))â€ is the condition inside the â€œifâ€ loop,Â which will decide whetherÂ the status of PB0 pin is high or low. When the status reads as low, the controller will turn the LED ON. The LEDâ€™s will turn off when the controller reads a low value at the PB0 pin.
Â #3.Â Toggle Two LEDâ€™s using a Push button switch
Â In this section, we will toggle the status of the two LED’s according to the input from a button switch. The circuit needed for this is similar to that of the above section. The only difference is in the programming part. And the working of the circuit is also slightly different. Every time when the controller receives input from the switch,Â it will toggle the current status of the two LEDâ€™s.
Assemble the circuit as shown in diagram. AÂ video demonstration of the project is shown below.
<iframe width=”560″ height=”315″ src=”https://www.youtube.com/embed/A5xJcSJrl9s” frameborder=”0″ allowfullscreen></iframe>
Toggle 2 LED’s Using Push Button Switch – Download Program
Similar to the above sections, here also we included the pre-processors and libraries. And the necessary pins are configured as input and outputÂ using the DDRx (Data Direction Register). An â€œifâ€ loop is then included at the main program with a condition â€œ!(PINB&(1<<PINB0))â€, which will continuously monitor the status of the PB0 pin.
â€œPORTB ^= (1<<PINB2)^(1<<PINB3)â€ is the instruction for toggling the current status of the PB2 and PB3 pins. A 300 ms delay is also included at the end of the program in order to avoid debounce of the push button switch.