Swiss AVR Knife

by matthewpoage in Circuits > Microcontrollers

40255 Views, 117 Favorites, 0 Comments

Swiss AVR Knife

sak-swiss-avr-knife.jpg
sak-swiss-avr-knife-upright.jpg
sak-swiss-avr-knife-insides.jpg
sak-pushbutton-installed.jpg
The Swiss AVR Knife bundles a number of AVR programming projects together in a single convenient Altoids Gum Tin. Because of the flexibility afforded by microcontroller programming, it also provides a starting point for any number of projects based on LEDs and sound output. The SAK can contain as many programs as the 8K of memory permits and maintains eight states for each program. The blue pushbutton causes the SAK to cycle through programs and states -- a quick press causes it to stay in the program but change to the next state (however that is defined) and a long press causes it to advance to the next program. The current program and states for all programs are preserved in EEPROM between uses.

The projects currently implemented in the SAK include the following. These, along with all of the other code and constants (there is a full font table), take up about 4K of the available space. Lots more room!

MiniMenorah -- Evil Mad Scientists
Brain Machine -- Mitch Altman
MiniPOV -- Adafruit Industries
Noise Toy -- Loud Objects
LED Running lights
LED Candle
LED Flashlight

This project would not exist without the considerable generosity of everyone who contributed in one way or another. In addition to the abovementioned, I would like to thank the developers of the software tools used (see in other steps) and anyone that put up a useful website that furthered my understanding of these topics. I can take direct credit for very little of the code used in this project. If you feel that the code is yours, it may well be. Let me know and I will happily give you credit. In any case, thank you for your contribution :-)

Parts

Parts can be obtained from any of a number of electronic suppliers. Because of the space constraint, most of the components are required as indicated. Everything just barely fits; make sure that any substitute parts don't take up additional space. Do not substitute for the ATtiny84 unless you are absolutely certain that the pins correspond. The links following the parts are to DigiKey and All Electronics.

Electronic components
1 x U1 -- ATtiny84 -- ATTINY84-20PU-ND
1 x Ux -- IC socket 14-pin DIP -- A32879-ND
9 x LED -- your choice of color
9 x resistors -- matched to your LEDs

2 x R1, R2 -- 100 ohm 1/4W �1% metal film -- 100XBK-ND
2 x C7, C8 -- 47uF -- P5151-ND

Miscellaneous
Battery Holder 1-AA 6" wire leads (1) 2461K-ND
Phone jack stereo 3.5mm (1) MJW-22
Toggle switch SPDT 1/4" on-on (1) MTS-4
Push button switch (1) 450-1654-ND

Minty Boost
The SAK is powered by a single AA battery boosted by a Maxim MAX756 chip (the essential component of the MintyBoost!). The components below are those needed for this part of the circuit.

1 x U1 -- MAX756CPA DC/DC 3.3/5V DIP -- MAX756CPA+-ND
1 x Ux -- IC socket 8-pin DIP -- A32878-ND
2 x C7, C8 -- 0.1uF -- 399-4151-ND
2 x C3, C5 -- 100uF -- P5152-ND
1 x L1 -- 22uH radial -- M9985-ND
1 x D1 -- 1N5818 Schottky 1A 30V -- 1N5818-E3/1GI-ND

ATtiny84 Microcontroller

sak-attiny84.jpg
Many projects use either the ATtiny2313 20-pin or the ATtiny85 8-pin microcontroller. I found the ATtiny2313 too big (for the enclosure) and the ATtiny85 too small (not enough memory, not enough output pins). The ATtiny84 is just right :-)

The ATtiny84 has 8K of programmable flash memory (enough to hold lots of small programs), 512K of EEPROM (for storing state between uses), up to 12 output pins (for the 9 LEDs, 2 channels of audio output, and a pushbutton switch), and lots of other goodies that aren't used in this project.

If you plan on adding programs, obtain a copy of the ATtiny84 datasheet. There are many instructional guides for learning to program this family of microcontroller on the Internet. For a helpful summary of microcontrollers, see How to Choose a Microcontroller.

Note The project described here does not actually have the MiniMenorah fully enabled. The MM requires nine output pins, the Brain Machine two, and the button to change state one, for a total of twelve. While the ATtiny84 can be configured to have twelve output pins, it is at the expense of the RESET pin. Disabling the RESET pin and making it I/O makes the ATtiny84 unable to be programmed with the USBtinyISP progammer (who hasn't done that :-) and requires High Voltage Programming. Everything is in place to enable the MM, but a different programmer is required, and I don't have one.

AVR Programming Tools

sak-usbtinyisp.jpg
sak-cradles.jpg
Quite a few components, both hardware and software, are necessary to program AVR microcontrollers. Below are the tools I use. Many, many others exists in the same price range -- free to cheap. Find a set that works for you and stick with them. Better yet, find a friend who has worked out a system and use his/her tools. Nothing is particular hard if all goes as advertised, but getting all of the tools working together can be a real challenge.

Programming cradle
I based mine on the Ghetto Programming Environment. The long pins of the wirewrap chip holder extend down in to a breadboard and makes for a convenient experimental setup.

The only problem I have encountered is that the components from the programming pins cannot be grounded during programming. I have taken two approaches to solve this problem. The first is to have two chip holders, one for programming and one for running (see 8-pin cradle). This is not ideal because it renders a lot of the breadboard unusable and it is quite annoying to move the chip. The second is to install a small switch to disconnect the ground pin from the ground of the breadboard during programming. This works better and leaves more space on the breadboard for components.

Programmer
USBtinyISP kit from Adafruit Industries. With a little modification (remove the 10-pin cable and bend the LEDs) the programmer fits in an Altoids Gum Tin. The 6-pin cable can even be coiled up into the tin for storage.

Software
WinAVR is a collection of open source software development tools for programming AVR microcontrollers on Windows machines. It works well with the USBtinyISP programmer (see the AVR Tutorial).

I recently switched from using the Programmer's Notepad application that comes bundled with WinAVR to using Eclipse with the AVR Eclipse Plugin. Eclipse can use avrdude, so you will have to install WinAVR anyway. Eclipse has better project management, helpful tutorials, and is free. It took just a few minutes to install it, work through a tutorial, and program a chip.

Phone a friend
There are lots of resources on the Internet. Look for them, ask for help. People can be knowledgeable and helpful. That's nice :-) They also can be dismissive. That's not nice :-(

Programming the Microcontroller

sak-code.jpg
C code Don't criticize what I don't understand. I am not a programmer, C is not my native language, and I am holding on by a Java-thin thread and a lot of web searching when working in C. Even though much of the code came from other projects (see credits), I did have to make some additions and modifications. The source code for the Swiss AVR Knife is attached below both as a c source file and a hex file. I would appreciate hearing where the code could be improved.

There are a few changes I anticipate making in the code. Updates are forthcoming. In the meantime, the code does work as advertised.

Fuses Microcontroller fuses are confusing. I have disabled a few microcontrollers both by accidentally setting them to look for an external oscillator and by disabling the RESET pin. They can be recovered, but until then they are just dead bugs. Be careful if you choose to change the fuses.

To calculate the correct fuse values, use an online fuse calculator. Select the target part (ATtiny84) and the appropriate settings -- internal RC oscillator running at 8MHz (default value), DO NOT divide the clock by 8 internally, enable serial program downloading, and disable brownout detection. The result should be the following.

-U lfuse:w:0xe2:m -U hfuse:w:0xdf:m -U efuse:w:0xff:m (low 0xE2 high 0xDF ext 0xFF).

You only need to burn the fuses once (unless you plan to change them). Eclipse makes this easy, as, I am sure, do other IDEs.

Questions that I would like answered

Any ideas on optimizing the code
Why do the blinking lights in the sound and light machine cause an oscillation in tone when enabled in the tin but not on the breadboard?
Why doesn't Eclipse like the lightOn and lightOff functions, even though they seem to work?

Breadboarding the Project

sak-breadboard-01.jpg
sak-breadboard-02.jpg
Because so much of the work of this project is done by the microcontroller, there are very few external parts. After checking that your programmer and tool chain are in order, it would be a good idea to breadboard the circuit and make sure that everything works as advertised.

The pictures below are messed up versions of the actual breadboard I had set up. I used the LEDs in the model tin and pried out the cradle and chip to use in several photographs. The overall wiring basically connects active pins to a few parts and then on to ground.

Note The order of pins and LEDs is not the same on the breadboard and the PCB (although I suppose you could make them the same). In the code, you will see pieces of code that need to either be enabled or commented out depending on whether the target is the breadboard or the PCB.

Preparing the Altoids Gum Tin

Pictures on the way

Flatten the bottom. The bottom of the tin curves up and in. It needs to be flattened so that the battery and circuit board fit and sit evenly. Being careful not to distort the tin, push the bottom out until it is essentially flat.

The tin needs three sets of holes. I use a metal punch to mark the hole locations and brad point bits (for wood) to drill the holes. The brad point bits have a center point and two cutting edges. They won't skate and the edges cut slowly through the metal. Brad point bits are available from Lee Valley (among other places).

The first is a set of nine 5mm holes across the top of the tin for the LEDs. Metric brad point bits are available and they make clean and snug holes for the LEDs. Create a paper template with the holes marked and transfer the marks to the top of the tin. In order to prevent pushing the top of the tin in, support the inner part of the lid on a small block of wood when punching and drilling the top. With the paper and wood in place, I dimple the tin using the punch. When drilling, go slowly at first. The cutting edges of the brad points should make an even circle. Drilling with the bit anything but perpendicular to the surface may result in the bit grabbing and tearing the metal.

The 5mm brad point makes a nice clean hole, but I found that I had to widen it ever so slightly. I did this by drilling out from the inside with a regular 13/64" bit.

The second set consists of two 1/4" holes on the right side of the tin for the switch and audio jack. Because of the tight curvature at the end of the tin, these holes need to be fairly close. Make sure to space them so that the components fit in the tin. Center them vertically on the portion of the side visible when the lid is closed. Mark with punch and drill very carefully. The caution about the bits grabbing the tin applies more strongly with the bigger bits.

The last hole is for the pushbutton switch. Position the hole towards the bottom right in such a way that the pushbutton does not interfere with the other components in the tin.

Designing and Making the PCB

sak-board.jpg
sak-schematic.jpg
sak-places.jpg
There are numerous resources on the Internet that describe the process of creating PCBs. None of the methods is foolproof or easy, but it is important to get comfortable with at least one.

I use the the freeware version of the EAGLE Layout Editor from CadSoft to create the schematic and layout the printed circuit board. My approach to manufacturing the PCB is described in the Making and Preparing the PCB step of the Altoids Tin Speaker instructable.

After transfer, etching, and drilling the board, you are ready to solder everything together.

Note My most recent experience for transfer images to circuit boards is the following. Wash the board well with dish soap and scrub it with a green scrubby. Gently sand any burrs off of the edges of the board so that the transfer paper and iron make good contact with the board. Preheat the iron. Place a piece of paper on the board and the heat the board with the iron. After the board is quite hot, carefully place the prepared transfer paper on the board. It will stick right away (because the board is hot) so make sure that it is correctly positioned. Then iron directly on the shiny back of the transfer paper. This never caused me any trouble, but you are using your own iron. Test first. Let the board cool and then run it under cold water. The transfer paper should pop off and leave the entire image. Use an 8x slide/negative viewer to look over the transfer and fill in any missing pieces. Good luck.

Soldering Parts to PCB

sak-soldering-parts-all.jpg
sak-soldering-parts.jpg
sak-soldering-parts-labeled.jpg
sak-soldering-parts-audio-labeled.jpg
sak-soldering-parts-back.jpg
sak-pushbutton-installed.jpg
There are numerous resources on the Internet that describe the process of soldering electronic components to PCBs. See, for example the soldering tutorial at ladyada.net. The order in which you install components doesn't really matter, although I have found working from smallest to largest the easiest.

The LED/blinkenlight leads are long enough so that you can shape them into a menorah-like pattern in the tin. Carefully fit the LEDs and bend the leads so that the top of each LED is positioned so that it will poke up through its respective hole. This can be challenging but it looks really nice when it finally works out. If the leads are left too long, the LEDs might be squashed down and out of position by the lid of the tin.

Note The rightmost LED is not in the same orientation as the other eight. Make sure you check the polarity of the LEDs against the board layout when you install them. This LED is attached to the RESET pin, so you may choose not to install it.

Note The wires to the audio jack and the resistors share a hole. For convenience, put the resistors in upright in such a way that the body of the resistor is not over the hole with the audio wire. Either prepare and install the audio jack at this point or wait until it is ready to solder in the resistors. It is no fun desoldering the resistors later on.

Blinkenlights

sak-blinkenlights.jpg
The LEDs need to be protected by resistors. Determine the voltage drop and current requirements of your LEDs and calculate the appropriate resistors assuming a 5V source from the chip. There are readily available online calculators to do this.

Make yourself a bunch of blinkenlights. When making them for this project, cut the cathode (negative/short lead of the LED by the flattened side) and solder the resistor very close to the lens of the LED. The LEDs form a menorah shape in the tin. Even with the resistor almost touching the lens, the shortest LED in the middle will be slightly squashed by the lid of the tin.

To prevent shorts from occurring in the tight confines of the tin, cover each resistor with a piece of heatshrink tubing.

Preparing the Battery Holder

sak-battery-holder.jpg
Slide small pieces of heat shrink tubing along the both leads of the battery holder. Push them carefully into the holes of the holder and shrink into place. These provide some degree of protection for the wires. (This instruction is duplicated on the Preparing the Toggle Switch page.) Cut the black wire to length and solder into the appropriate hole on the PCB. The red wire is soldered directly to the toggle switch; see instructions on that page for how to proceed.

In past projects I have cut the retaining tabs off of the battery holder. Having done this on the prototype, I now regret it. The battery does not want to stay tightly in place. Leave the tabs to start and remove them only if you have trouble getting the battery out. Despite saying this, the picture shows a battery holder with the tabs cut off. This is because I scavenged it from another project.

Preparing the Toggle Switch

sak-battery-holder-toggle-switch.jpg
Depending on your switch, you may have to clip off one of the pins. I do this with the switches I use although it may not be entirely necessary.

Slide a small piece of heat shrink tubing along the red lead of the battery holder. Push it carefully into the hole of the holder and shrink into place. It provides some degree of protection for the wire. (This instruction duplicates the instruction in Preparing the Battery Holder.) Slide another small piece of heatshrink tubing onto the red wire. Cut and strip the wire to length and apply some solder to both the pin on the switch and the end of the wire. Solder the red lead from the battery holder directly to the outer pin of the switch. Slide the piece of heatshrink tubing over the joint to protect and strengthen it.

The second wire goes from the middle pin of the switch to the PCB. Solder the wire to the switch as described above. Protect the joint with heatshrink tubing. Solder the other end to the appropriate hole on the PCB.

Preparing the Audio Jack

sak-audio-jack.jpg
sak-soldering-parts-audio-labeled.jpg
sak-soldering-parts-audio.jpg
The wires to the audio jack are all quite short. Apply a bit of solder to the pins on the jack and the wire and then solder them in place. Slide pieces of heatshrink tubing over the joints to protect and strengthen them.

The ground wire can be soldered directly into its hole. The ends of the signal wires each share a hole with one end of a resistor. Prepare the wire and resistor by twisting the ends together and applying a little solder. The hole into which these go should be drilled to 3/64" to accommodate the two wires. Solder into place.

Preparing the Pushbutton Switch

sak-pushbutton-switches.jpg
sak-pushbutton-holder.jpg
sak-pushbutton-installed.jpg
Prepare a short piece of solid wire by forming it into a U-shape the fits snugly over the bottom of the switch. Apply a blob of solder to either side of the hole -- leave room for the switch -- and position the switch in place. Melt the solder and push the wire in place. Let the solder harden and repeat on the other side. This should position and secure the switch in place.

Prepare two pieces of stranded wire by cutting to length and stripping both ends. Make sure that the wires are long enough so that the lid of the tin can fully open. Solder to two appropriate pins on the switch and then slide pieces of heatshrink tubing over the joints to protect and strengthen them. Solder to other to ends into their respective holes on the board.

Carefully thread the wires between the LEDs and make sure they don't sit on top of the batteries. I spread the two pins on the switch so that the rightmost LED slipped between them. The pins on the switch are VERY fragile (the other two snapped off).

Note the pin PA7 PCINT7 6 is set up to listen for a change in state. Pressing the pushbutton switch pulls the pin high and SIGNAL(PCINT0_vect) is executed. Based on the length of the button press, either nothing happens (crude debouncing), the state is advanced (short press), or the program is advanced (long press).

Closing the Lid

sak-swiss-avr-knife.jpg
If all is well at this point, you will want to close up the tin. In doing so, you have to be very careful about the position of the LEDs. I find that I have to nudge them into position with a thin-bladed screwdriver so that they are correctly positioned in their holes. Apply a little downward pressure on the lid as you maneuver the LEDs into location and they will eventually slip into place.

You might have to position the wires so that they fall between and not on components. Also, the pins of the pushbutton switch may have to be bent out of the way.