Introduction: Beginning Microcontrollers Part 5: Testing the Programmer and Building the First Circuit
So far, you should have all the software installed and constructed the interface that will provide a convenient connection from the programmer to the microcontroller (MCU). For the next step, you will need a breadboard (the ones with numbers are very helpful), an LED, and a resistor of a size appropriate for the chosen LED. You will learn in this section how to test the programmer to determine if the software and drivers were installed correctly. You will also learn a little bit about LED lights, Ohm's law, and the resistance value required for the chosen LED.
To check to see if the drivers and the development software installed correctly, we will test the programmer with a program called AVRdude. AVRdude is a program that is installed with the latest WinAVR installation, and is responsible for the actual transfer of the file going into the microcontroller. This is the .hex file, which is basically the binary code that the microcontroller can understand and execute. If the test is not successful, the programmer will not be able to transfer the file--therefore this step is crucial to the whole process. To test the programmer, follow these steps:
- Go to a DOS prompt by clicking the start menu and typing cmd.exe in the search box. By the way, if you didn't know, DOS stands for Disk Operating System. This was a prompt created so computer users would be able to organize their files on floppy disks, thus making it easy to execute (run) programs from the DOS Prompt. Prompt means the place where the cursor is located and you can start typing. The prompt is labeled with the drive letter along with the folder name separated with backslashes "\". (We called these directories in the past.)
- To execute the avrdude program that was installed with WinAVR, simply type avrdude -c usbtiny -p m32 at the DOS prompt, and the DOS shell output will report the success of the connection. The "-c" is a flag that is followed by the parameter used to specify the programmer (usbtiny), and the parameter following the "-p" flag is used to specify the microcontroller ("m32" for the Atmega32). If you are using a different microcontroller, you will need to use the appropriate specification, as shown in the video for this tutorial.
- To exit out of the DOS window, you can type "exit" at the DOS prompt and the DOS window will disappear... just like my ephemeral kids in Disneyland!
So now you're probably wondering, why can't we program yet! Well, we still need to create a circuit that the program will control. It would be pointless to simply load a program into a microcontroller and run it without any devices connected to it. We wouldn't have much to look at! In fact you will soon see that many electronic components can be controlled by microcontrollers, however one of the easiest devices to control is an LED.
"LED" stands for Light Emitting Diode, and this component generally has two leads. Leads are the metal legs (wires) hanging off of the actual LED itself. These leads are the polar connections that allow current to flow into the LED from one lead (called the anode) and then out of the LED from the other lead (called the cathode). One very important caution about powering and using LEDs: The current running through the LED must be limited so it does not burn out. LEDs have both a current rating, and a voltage rating. The current rating is the maximum current limit that the LED can handle--any higher current and the LED life will be shortened; but less current will result in a dimmer LED that does not emit light as brightly. So, we are faced with having to compute the optimum value for the resistor we will choose. Note that if you don't want to do this calculation for some reason, it is generally safe to use a 1k--but the light will be pretty dim.
So to calculate the resistance needed, we will use Ohm's law which states that resistance (in Ohms) is found by dividing the voltage by the current. The formula is:
Resistance = volts/current
This is typically written as R = V / I. But, how do we find the voltage and current vales for the LED? Typically, LEDs are rated at either 2 or 4 volts, and have either a 10 milliamp (mA) or 20mA current rating. My green LED has a 2 volt rating. In the website linked in the previous sentence, white and blue LEDs have a voltage rating of 4 volts. I used the 10mA rating since it is safer to use this value as it results in the smallest denominator in the Ohm's law formula, and thus represents the largest resistance value for a given voltage. Therefore it can be considered the "worst case scenario," in terms of choosing a resistor to insert into the circuit. Another aspect we will need to consider is the difference between the supply voltage (the voltage we are feeding into the system), and the rated voltage of the LED. So therefore the new formula becomes
R = (Supply Voltage - LED Voltage) / I
Therefore in the case of our green LED, R = (5v - 2v) / .01A = 300 ohms. Oh yeah, you need to convert the current to amps. Therefore we need to divide the 10mA number by 1000, as there are 1000 milliamps in one ampere. So now, what is this formula telling us? It's telling us that the resistance is equal to the remaining voltage after the LED is considered (i.e.; the voltage drop across the LED), divided by the desired current through the LED.
What is that you say? We got a value of 300 out of that formula, but when I went to the store I couldn't find that number! I hate to tell you this, but the money spent on gas for that trip could have bought you 40 of the correct resistors! 300 Ohms is a resistance that is probably not available, but don't lose hope--you can always use a resistor of the next highest value. I have found this to be 330 Ohms in the mix of resistors I've collected from various thrown away appliances and electronics.
Now we'll create the circuit, which is pretty simple in this tutorial. The hard part is behind us (the stuff above), so you can wipe your brow now! Let's use Pin 0 of PORT "B" on the MCU (microcontroller) in this circuit, and that just so happens to correspond to the number 1 pin on the Atmega32 microcontroller I am using. So now we are going to programmatically turn on that pin to light up the LED.
The steps to create the circuit go like this: Connect the resistor to the pin number 1 (note that saying "PORTB0" is one way of referring to pin B0 of PORT B, but you'll learn other ways as well). Now connect the other end of the resistor to the positive side of the LED (i.e.; the anode side, or the lead that is the longest, or the lead opposite to the flat of the LED). Then we will connect the cathode side to the ground (GND) pin. The programmer will be connected too of course; which will allow the program to get transferred into the chip, and also provide power to the microcontroller. Finally, we can now apply the circuit to the breadboard. The videos demonstrate all the steps needed in this process. From the accompanying image, you can see that this is a very simple circuit.
Now apply the circuit to the breadboard. The videos demonstrate this with every step needed. From the image, you can see that this is a very simple circuit.
Check out the close-up of the resistor and the LED. Can you see how the wire is connected to the resistor and how the resistor is connected to the LED? After this circuit is completed on the breadboard, we can start to program and make the LED light up. Excited? I am!