Safe, legal fun with breadboards introduction Breadboards are a simple and useful way to quickly assemble and play around with different circuits. Today we'll be using breadboards to put together some simple circuits that closely relate to the final boards the University wants you to assemble. This is going to be super educational! Building circuits with a breadboard is as simple as sticking wires and components into the holes. The holes themselves are connected according to the diagram at left(thanks, Wikipedia!). Getting started Since we want to run an attiny2313 microcontroller (more on that later) on this board, the first thing we need is a power supply. We want the rails on either side to be 5 volts (+) and ground (-). The first thing I've done here is connected the two sets of rails together. Notice that I've used a red wire for the +5V line and a black wire for the ground line. I'm told that this is a good practice. Circuit symbols A few circuit element symbols: Hopefully this list isn't really new information for anybody. We'll be looking at a lot of circuit layouts, so knowing the symbols is super essential. Of course, access to a complete list of circuit elements is as close as google, so I wouldn't worry too much. The power supply Below is a schematic of the power supply we'll be making. This presentation has photos of the assembly process but you'll want this diagram to be your authoritative source on what connects to where; it has the advantage of being unambiguous). + - Important caveat! Do not put the capacitors in backwards! You will notice that the capacitors have a long leg and a short leg. The long leg is always the anode, or positive lead. Make sure to put the anode on the positive line. If the capacitor is inserted incorrectly, when a current is applied it will be ruined and will possibly even EXPLODE. (science is cool!) You don't need to worry about which direction resistors face because they're symmetrical. If you put the diode in backwards, the circuit won't work. If you put the capacitors in backwards, bad things will happen. Assembly step 1 This is the beginnings of the power supply. The two wires running off to the left will be connected to a nine volt battery. If you look closely, you will also note that I've inserted to diode backwards. Ha ha! Ouch for me! We want the current to flow through the diode from left to right, so the little silver stripe should be on the right-hand side. Don't make the same confusing mistake that I did. Assembly 2 I've added both capacitors and the 7805 voltage regulator. It's hard to see exactly how things fit together from a photograph so use the diagram as your guide. Note once again the use of red wire for +5V and black wire for ground. Indicator light The power supply was already complete, but I've added an LED, connected to both power and ground. Notice that I used a resistor to connect the led to ground. Leds have little intrinsic resistance, so if you connect them to a circuit without adding resistors then they'll pull a lot of current, and may damage themselves. The reason I even put the led in is to make sure the power supply is working. If the led lights up, we have power. Now is a good time to attach the battery and make sure everything works. Use eye protection in case a capacitor explodes. Seriously. Radical! IEEE Approved! (i think) Does math! Now we can add the attiny2313 microcontroller. These guys are just about the coolest things ever. They're extremely versatile, fast(potentially), and you can program them fairly easily, using standard C code. You'll soon be learning how. There's a notch at one end of the 2313. Make sure that end points toward the top of the board. Also, the picture shows the 2313 with its first pin on row five, but I'm actually going to recommend you put it on row 10. The pin on the upper right (pin 20), and the pin on the lower left (pin 10), are connected to +5V and ground respectively. In which a riddle is proposed I'm reasonably sure that the attinys we're giving you are preprogrammed with a simple code that will make an led blink. So let's go ahead and build the apparatus for that. It's super easy; pretty much just an led with a resistor like we used for the power indicator. All the pins on the right-hand side of the chip except the top and bottom one collectively constitute Port B. You can wire the led to any one of them and it will light up. Different pins will cause the led to blink at different speeds. Can you think of a simple reason why this might be? (Hint: computers use binary code and they loooove to count things.) I think this might actually be a pretty hard riddle, so feel free to ask for the answer. This is where it starts getting tricky. This is the diagram for the circuit that will actually program the 2313. The tricky part is that it doesn't show exactly what we're doing. We won't be using an ICSP port; we're just going to connect the wires directly to the 2313. We'll be handing out copies of this diagram; you'll need 'em. Make sure you remove the battery before messing with your breadboard. The final product This is what the programmer will look like when done. I built it right next to the power supply to make room for future features. The wires going off to the right connect to (from top to bottom) pin 2, pin 19, pin 17, pin 18. See next slide. Here is the same view with the red wires removed. Wire A connects the left pin of the transistor to wire B. Resistor A connects the middle pin of the transistor to wire B. The band on the diode faces the transistor. The flat side of the transistor faces down. With this information, you should be able to replicate the design exactly. Making a cable The next step is to make a cable to connect the breadboard to your computer's serial port. This involves soldiering, which I'm not even going to try to explain in powerpoint. Someone will teach you. Soldier wires onto pins 3, 4, 5, and 8. Soldier the same wire to pins 6 and 7(As shown in the diagram on slide 12). You can connect the two pins with a blob of soldier or—better—a fragment of wire. The wires should be long enough to reach from the breadboard to the computer's serial port. Crimp /////s onto the other ends of the wires. They can now be placed onto the xxxxxs. Attaching the pins Now you can plug the wires you've made onto the pins on the breadboard. You can ignore the stuff behind the wires in the photo; we're not there yet. Attach the wires as follows: the top breadboard pin (on the right in the picture) attaches to the wire that runs to pin 4 in the DB9. The next one attaches to pins 6 and 7. The middle one attaches to pin 3. The fourth one attaches to pin 5. The last one(on the left in the picture) attaches to pin 8. Now plug the DB9 head into your computer's serial port and you can program the attiny 2313. rgb leds Now we can write a program that will cycle through the colors of an rgb led. Take out the apparatus for the singlecolor led (if you haven't already done so) and build the configuration seen at left. Note that the long pin of the led should be the one not connected to a resistor. In the picture, the wires for power and the programmer cannot be seen—this is just for clarity, so you can see exactly what to add. Do not remove the power or programming wires from your chip. Make sure the board is powered on and plugged into the computer. It's time to program it. programming The software tools we'll be using to program the attiny 2313 chips are WinAVR and PonyProg2000. They should hopefully be installed on your computers already. Now that we have the programmer assembled and the led in place, the first thing we're going to do is use ponyprog to download a new program to the attiny2313. First, fire up PonyProg2000. Disable the startup noise (it's super annoying) and close the launch window. The main function of ponyprog is to read and write data (in the intel hex format) to and from the built-in program memory on the attiny. The first thing we need to do is make sure the device is communicating. Click on the setup menu, and select “interface setup...”. This will bring up a new window. Make it look like this, then click Probe. It should pop up a little window that says “test OK”. If not, there is a problem. Just raise your hand, probably. The PonyProg2000 interface The most useful buttons in the interface are defined above. Make sure that AVR micro and Attiny2313 are selected in the drop down menus to the right. Then go to the setup menu, click on calibration, and select yes in the window that pops up. It should tell you “Calibration OK”. Now you're ready to program. Test that things are working by pushing the “read device” button. It should pull up the program already on the chip and put in in the main window. The programs are in hex format, so don't worry if they're totally incomprehensible. Programming continued Time to write a new program to the chip. Press the “open program file” button, and select the file “main.hex” in the folder “tiny2313_led0”. Then just push the “write device” button. The program is written to the device and the light comes on! I think we can all agree that this is pretty cool. So now let's look at the source code. The application to do that with is programmer's notepad. So fire that up. Before we look at the RGB code we just installed, let's check out the simple monochrome blinking that was on the chip to begin with. It's the file “main.c” in the folder “tinyblink”. I sure hope everyone understands basic c syntax; if not, the next slide contains most of the terms used in the code. The code itself is pretty well commented; you should be able to figure out what's going on. The main idea is that the pins making up port b collectively express a numerical value in binary code. This value, called “countval” is counted repeatedly from 0 to 255. Suppose countval was 155. In binary that's 10011011. So on the physical chip, pins 12, 13, 15,16, and 19 would be pulled high while pins 14, 17 and 18 would not. You can see how, as countval changes, an led attached to a single pin will blink on and off. C syntax (or, what's going on) A complete explanation of the c programming language would be outside the scope of this lesson, but I will try to explain the basic terms used in the tinyblink program. Really though, if you've never used C, you're going to need more information than it makes sense to put into this presentation. You can find a good introduction to C programming at http://www.cprogramming.com/tutorial.html#ctutorial So here are a few lines from the code, with explanations: uint8_t countval; //the keyword uint8_t declares that the next term will be the name of a new unsigned 8-bit integer. All variables must be declared in this manner before they can be used. int main(void) {...} //This defines a function called main which consists of the contents of the curly brackets. The main function is what is executed when the program runs. Read the turtorials linked above. DDRB = 0xff; //The commands DDRB and PORTB aren't part of standard C; they're device-specific functions that the microcontroller-focused compiler we're using understands. These functions allow us to write data directly to specific locations in the chip's memory. What is being written is a numerical value in hex digits. The 0x at the front of the number only means that the following two symbols are in hexadecimal. See http://en.wikipedia.org/wiki/Hexadecimal Further discussion of the code While(1) {...} //While commands are one way to do loops in C. When the program reaches the while command, it decides whether the expression in the parentheses is logically true. If it is, the code in the curly brackets is executed. When the end of the curly-bracketed code is reached, the computer again evaluates whether the expression in the parentheses is true. If so, the bracketed code is executed again at infinitum. Because 1 is considered to be a true statement always, the program will loop forever. if(countval>=255) countval = 0; // If statements cause the command following them to be executed only if the expression in the parenthesis is logically true. In this case, the variable is being reset to zero when it reaches 255. Talkin' bout the colors now So now let's turn our attention to the RGB program. Open it up and take a look at the code. The overall structure is pretty much the same; the difference is in what values are being broadcast over port B. I'll try to explain the logic behind them. You can physically see that the cathodes controlling the color of the led are connected to pins 14, 15, and 16, or in other words, the fours, eights, and sixteens place of port B. We want the program to cycle through every possible configuration of those three pins. Hence a variable is run from 0 to 7 and multiplied by 4. (The multiplying by 4 shifts the digits two spaces up, just like multiplying by 100 would in base 10. Chew on that for a minute.) The other commands use boolean logic to turn the ones digit—pin 12—on and off respectively. Pin 12 provides power to the anode. Compiling Just for the fun of it, let's change the program a little. Change the value that the variable waitcounter is initialized to from ten million to five million. This will make the colors cycle twice as fast. After the source code has been changed, we still need to compile it into a format the attiny understands and then download it to the chip. Here's how. Start by going to the tools menu and clicking make clean, then make all. This will cause WinAVR to clear the old output and then compile the new code. It does so according to instructions contained in the makefile, which we'll return to shortly. One of the files you just created was a new main.hex file. Open it up in PonyProg2000 and write it to the device. You should notice the change in led behavior. Outstanding! Makefiles All of the demo program folders we've given you contain a file called makefile. The makefile is a plain text file that the compiler refers to when it compiles the program. The makefile contains information such as what compiler version to use, what file should be used as the source code, what the output files should be named, and what kind of device the code will be written to. You can open up the makefile with notepad and take a look at what's in there. The only lines you really need to make sure you understand are the first few. (Fortunately) 9 led RGB array Actually, it's not really an array since they're all in a line. But it's still going to be pretty cool. Start by putting a column of nine rgb leds on the furthest-left column on the board. Make sure that the anode (again, the longest pin) of the led is the second from the bottom in each case; if all the leds aren't facing in the same direction then you'll have problems. The three cathodes on the leds each control one color. We want every led to have its cathodes connected to the corresponding cathodes of every other led. That was really confusing, sorry; just look at the picture. Pin one connects to pin 1, pin 3 connects to pin 3, and pin 4 connects to pin 4. The pin 2s are anodes, and we'll deal with them later. My condolences Continue making the same braid pattern established in the last slide until every led is connected. Make sure that you use three different colors, and use them consistently. This part takes awhile and is sort of mind-numbing; be strong. Nearly there This picture really isn't the greatest but it should be pretty clear what I've added. The led cathodes have been connected to the chip via resistors. Notice that I've used the same color wire on both sides of the resistors(you can't really see the black one). The wires connect to pins 14-16. The magnitudes of the resistors, starting with the one closest to the chip, are 500, 620, and 240 Ohms. This is actually the same resistor and pin configuration as was used for the single RGB led, so hopefully you already have it in place. The big finish The last step is just to connect all the anodes to various pins on the chip. The circuit diagram we handed out will tell you which led to connect to each pin. Once you're done, the circuit is ready to be programmed. The 9 LED program The code for this configuration is found in the folder tiny2313_led1. I think you know by now how to put it on the chip, so I'll just talk briefly about what it does. It's actually pretty much based on the same ideas as the last program was. All of the leds are designated one after the other according to pretty much the same rule as before. Notice that they're all different colors, so the color defining variable is offset by one in each successive led. The on and off commands correspond to the various pins we attached anodes to. I know the bitwise operators that turn the anodes off and on are sort of confusing. The following reference explains how they work really well. http://www.gamedev.net/reference/articles/article1563.asp