Toggling an LED
Welcome back again.
Up until now, In all my previous posts I have been talking about different setups (both hardware and software) needed to start working with AVR microcontrollers. Having done that we are now ready to start building our first circuit and program it. So in this post, we are going to connect an LED with the microcontroller and learn to toggle it as per our need.
LED (Light Emitting Diodes) are the cheapest way of generating light energy. They are used everywhere in our day to day life so it is very important for any electronics or electrical engineer to learn about their working mechanism. In short, a light-emitting diode (LED) is a two-lead semiconductor light source. It is a p–n junction diode that emits light when activated. When a suitable voltage is applied to the leads, electrons are able to recombine with electron holes within the device, releasing energy in the form of photons. This effect is called electroluminescence, and the color of the light (corresponding to the energy of the photon) is determined by the energy band gap of the semiconductor. LEDs are typically small (less than 1 mm²) and integrated optical components may be used to shape the radiation pattern.
We will not go into details of their working mechanism as that is an entire vast topic in itself. I would suggest you do a little bit of research on your own if you are unaware of it. We, on the other hand, will focus on their application especially their toggling.
Before we do that, let us first take a look at the Pin Diagram of the ATmega 16 microcontroller in its P-Dip Package. We see in the diagram that this type of package divides the controller into two rails with twenty pins on each side. Therefore in total, we have forty pins in ATmega 16 controller.
Now out of these forty pins, we have thirty-two(32) among them as GPIO(General Purpose Input Output) Pins. These 32 pins can be used both for input as well as output operations. Further, these pins have been divided into Ports by combining 8 pins to form one Port. As a result, we get 4 Ports in ATmega 16 controller. They are PORTA, PORTB, PORTC, and PORTD. The same has been shown in the figure to the left. We can either use each individual pins or a collection of pins(PORT) depending upon our need.
Now that we know about the Pin-Out of the controller, we need to create the circuit for toggling an LED. To do that I will fix the controller on a breadboard and then connect the circuit as per the circuit diagram, given below.
I will now ask you to please go through the video below where I explain everything about the circuit and the code in details. This will help you to clearly understand everything I have been talking about in this post.
Now, you should be having a clear idea of what we are doing in the circuit and in the code. I have also given the code below for your convenience. Feel free to modify it as you see fit. Download the Makefile needed for compilation and copy it into the folder where you have saved the source file.
Title: LED Toggling
Author: Samiran Patra
Target: ATmega16 Microcontroller
MCU_SPEED: 1000000 Hz
Note: Use makefile for compilation.
//DDR Data Direction Registor
DDRA = 0b11111111; //output is always 1 and input is always 0
PORTA = 0b11111111;
PORTA = 0b00000000;
If you want to check out a cool application of this project, click here.
Shop Smart with us for Best Prices:
If you want to continue on with this series I would ask you to visit our YouTube channel and subscribe to receive continuous notifications. You can also subscribe to our Newsletter below to get email notification of each post as and when they are posted.
Thanks a lot for reading through this post. Feel free to follow through this series and comment your queries in the comment box below. Also feel free to contact us regarding any out of stock product and we will try to get back to you as soon as possible.