Intro: Pimp Your Chocolates With Arduino IDE and ATtiny13
Step 1: Get Parts and Tools Together
Here are the parts:
- 1 x Atmel ATtiny13 (or ATTiny13A) 8-bit AVR MCU;
- 10 x high intensity red LEDs. I imagine yellow would work, too. If you decrease (or remove) the 120 Ohm current limiting resistors, white or even blue might also work. Experiment!
- About 20 ft (6 m) of 30AWG hookup wire. Red color is recommended because the box is transparent and it's much easier to hide red wires inside the red plastic insert.
- 4 x 120Ohm current limiting resistors. Could be less than 120 for brighter LEDs (though shorter battery time)
- 1 x 10K resistor for the ball tilts switch part of the circuit
- 3 x 1.55V tablet batteries. Could be any type as long as you have a holder for them. I didn't at first, so I had to devise a one-time battery pack out of a piece of heat shrink tubing (center of photo) and two strips of brass. For an improvised pack the slightly larger 357-type (LR44) batteries would work best. Later on my dog helped me by destroying one of those electronic squeaking pet toys that had a small battery box for three LR1130 batteries. They have smaller capacity but the box lets me replace them easily
- A small slider switch in case you want to be able to switch the blinker off. The program makes the ATtiny13 go to sleep when there was not change in the tilt switch position for awhile and that helps with battery time. Mine worked for about a month from the three 357 batteries. But if you know that no one will use it for a long time, it would make sense to add a switch.
- Soldering iron and rosin core solder
- Cutters (snips)
- Needle nose pliers
- Hot glue gun
Step 2: Program the ATtiny13
If you haven't upgraded your Arduino IDE install to the current version 1.0, now is the best time to do it! Although it's possible to program an MCU using a programmer with earlier versions, 1.0 makes it especially easy with convenient menu options. I definitely recommend upgrading before going further, if you haven't yet done so.
The Arduino IDE comes with a set of C libraries that are optimized for running on various hardware platforms and types of MCUs. By default, ATtiny13 libraries are missing. It's a very small MCU and not many people program for it, I guess. Fortunately, there are at least two sets of Arduino libraries that either include support for or can be modified to support ATtiny13: http://elabz.com/arduino-shrunk-how-to-use-attiny13-with-arduino-ide/ The installation is not a difficult process and basically involves simply placing the library files into the right directory under the Arduino IDE hierarchy and editing the boards.txt file to tell Arduino IDE that you have just added support for another hardware platform.
Once you've installed the needed library, you will see ATtiny13 in the list of your boards in Arduino IDE -> Tools. It's kind of hard to see on this picture but the highlighted line describes the board that the Arduino IDE will compile for as ATtiny13 @4.8MHz (internal 4.8MHz clock)
Now go ahead and load the Arduino sketch for the Pimp Your Chocolates project from the ZIP file attached here (I've no idea how to point you to the right file that I have just uploaded to Instructables. If you can't see it, load it from my site here)
The next step is too hook the ATtiny13 up to your ICSP programmer (Arduino as ICSP or a dedicated programmer, see previous step) according to the schematics in the second picture attached to this step.
Select the programmer type from Tools->Programmer menu
Then select Tools -> Burn Bootloader. This is a rather confusingly named feature, at least for ATtiny13 because ATtiny13 uses no bootloader. However , the same feature also burns the proper control registers inside the chip that make it work on the right clock frequency, use internal clock frequency dividers or nor and otherwise sets the physical chip up to operate just the way the compiled code would expect of it. It's an important step and many people miss it, so be careful.
Burn the code into the ATtiny13 switch by going File->Upload Using Programmer.
Hopefully there will be no error messages there and, once the upload is complete, the MCU is ready for our project,
Step 3: Prep the LEDs
There are a lot of wires in this project, that's why it is important to use the thinnest wire possible that you're still comfortable working with. I am having trouble soldering anything thinner than 30AWG hookup wire, so I used 30 in the project.
The LEDs need to have their leads clipped to about 3/16" (5mm or so). Once you do that, you'll loose the polarity mark which is the fact that one of the leads is longer (anode). You will then have to mark that wire with a Sharpie to indicate the anode. I guess you can use tow different color wires but, once again, red is much better than anything else for the transparent box, so it's best to have them both red.
Test all prepped LEDs. Removing and replacing a faulty LED will be a real pain once they are soldered or glued in place.
Step 4: Build a Carrier for the MCU and Other Components
I wanted to have my ATtiny13 removable in case I needed to re-program it (and I didn't want to put an ICSP connector on such a small device), so I built the project using two single-row 8-pin sockets. The left groups of four pins is taken my the ATtiny13, the right one- by the four current limiting resistors.
I plugged a spare 555 timer into the sockets just to keep them together (that's why the photo may be so confusing - the 555 times is just a placeholder) and them bent the leads of the 120Ohm resistors to size, plugged them in and soldered them inside the sockets. That made the "carrier" rigid and the placeholder 555 could be removed.
The ball tilt switch (the larger beige cylinder on the left) and it's resistor are mounted on the underside of the sockets.
Then the rest of the connections needed to complete the circuit (see the complete schematics diagram in the third picture) have been soldered. If the schematics is not well readable, see it's full size version here. I've yet to figure out how to size pictures for Instructables properly ...
Step 5: Solder the LED Wires
Now solder the LED wires to the four pins of the carrier that have the free ends of the 120 Ohm resistors connected to them. Follow the schematics closely because the charlieplexed nature of the LED connections makes the polarity very critical. It's easier to start with LED#1 and go sequentially because there's a pattern to the pins and polarity and you have smaller chances of making a mistake if you follow the pattern. It'll probably help if you print the schematics out and have it in front of you when you solder.
Don't use too much solder, especially on the first two pins - they will receive 6 wires each and it may become a big blob of solder rather quickly if you're not careful.
Once all 10 LEDs (or 12 - the maximum amount of LEDs supported by Charlieplexing on 4 I/O pins) are soldered, solder the battery pack and the switch. I decided that I tend to mess polarity up and used a blue wire for the ground connection from the battery pack. It was the only non-red wire in the whole project which makes it easy to identify. The heat-shrunk cylinder with three LR44 batteries inside and strips of brass on either side proved to be a rather inconvenient way of providing the battery power. Unfortunately, spring-loaded battery boxes for tablet batteries are rather rare and I was glad my dog chewed up a toy that had one, so I've redone the battery before making the video, that's why my batteries are different on the video and this photo.
Once all the connections are done, give it a go. Now's the time to test everything because the next step is hot glue and repairs will get progressively more involved and messy after that.
By the way, if you labeled the LEDs with their sequential numbers, good thinking! I did not do that and ended up with LEDs "naturally" randomized. So, if you kept the LED numbering, you can better choose your "random" LED positions on the next step. If you lost the LED numbers, not the end of the world either. We're looking to have them randomized anyway. But since they are now truly random, two of the sequential LEDs can end up close together and you'd rather have them further apart for better look.
Step 6: Hot Glue Everything in Place.
Once everything is verified working: you shook the sensor and the LEDs ran through one cycle 1-10-1 and stopped, you're ready to put everything into the box.
Take the red plastic insert out of the box and turn it over. Then position your larger part alongside the rim, where it has the most space. Use the hot glue gun to glue the battery and the MCU carrier in place. Keep in mind the position of the ball tilt switch: if the cylinder of the switch is almost exactly parallel to the ground plane, it will be extremely sensitive. I ended up having it positioned like that (by chance) and you could not walk past the table the chocolates are on without setting off the LEDs. If that's the sensitivity you're looking for, aim for laying it horizontally. Just keep in mind that the battery life may be diminished by LEDs blinking often.
If you want it to set off only when someone takes the box into their hands, which is probably a better way to do it, you would want to tilt it towards vertical. Not completely vertical, mind you, but at, say, 30-45° to the horizontal level. That would keep the ball inside of the switch from jumping needlessly.
Once the MCU carrier's glue has set, you can bend the LED wires and route them to the LED locations. Try to randomize those locations, and, as I said in the previous step, if you kept the LED numbering you're probably in a better position to make your "random" LED spread look a bit better by avoiding sequential LEDs being close together. By the way, I've attempted to randomize the sequence in the software and it turned out that the random() function in Arduino C takes up too much programming space and pushes the sketch over the tiny 1K limit of this MCU. So, the LEDs are always sequential, it's their locations that make them look random.
Once you've positioned an LED in a particular valley of the insert you like, hold it by wires with fingers of one hand and use the other to squirt some hot glue right ontop of it. You can't really go wrong or use too much glue - it's (semi) transparent anyhow and won't change much visually when looked at from the front. Be careful with the hot glue gun - its tip will melt the insert itself. Don't let them touch.
Once all the components have been glued in place, dress the wires neatly and put the red insert with everything one it back into the box. Note that the box has a paper insert on the bottom, too. The one with the UPC, the nutrition information etc. Leave that there, it will nicely hide most of the wiring for a better look in case that special someone turns the box over.
Well, don't forget the last step, most certainly not least: LOAD THE CHOCOLATES BACK INTO THE BOX! It's easy to get too excited at the project's end and forget what the box was for :)
Enjoy and let someone else enjoy it too!