Professional Documents
Culture Documents
Breadboard Tutorial
Breadboard Tutorial
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!
+ -
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!)
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.
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 orbettera 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
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
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.
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 digitpin 12on 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
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