I recently saw an interesting project by Karl Lunt that used an ATtiny13 to mimic a firefly (http://www.seanet.com/~karllunt/fireflyLED.html). This in itself, is certainly not unique, but it got me thinking about one of my early microcontroller projects and how difficult it was for a beginning hobbyist to leave the calm waters of Arduino and venture out into other microcontroller chips. Looking at Karlís firefly C code reminded me how insulated the Arduino IDE keeps us from the underworking of the AVR architecture. I wanted to see if I could bridge the gap between prototyping something out on Arduino, and implementing it on an ATtiny chip while still utilizing the simplicity of the Arduino IDE for coding.
There are two major enablers to make the process of programming an ATtiny far easier than it used to be. The first is the ArduinoISP sketch which comes as an example sketch in the standard Arduino software. This sketch allows you to wire up an Arduino board to become a dedicated programmer and load a bootloader or sketch directly on another microcontroller chip via the ISP header. This is very handy if you are moving toward stand-alone breadboard projects or need to replace an Arduinoís ATmega328. A programming shield can make this process even easier by removing the need to wire it up each time you need it.
The second gem is from MITís High-Low Tech Group and allows you to port the Arduino code to the ATtiny architecture. This means you can use the Arduino IDE and itís layer of abstraction to set pin modes and use familiar functions like digitalWrite() and analogRead(). Not all of the Arduino functions are available for ATtiny, but more than enough to get a simple project running.
Here is what you will need to follow along*:
*If you are just getting started, SparkFunís Inventor Kit ($99) or Adafruitís ARDX Kit ($85) contain everything you need except the battery, battery holder, capacitor and the ATtiny.
If this is starting to seem a little daunting, take a look at some of my other projects which might be more suitable for beginning makers:
Step 1: Installing necessary software
First, letís make sure we are playing on the same field. That field would be Arduino 1.0. If you donít have a current version, head over to http://arduino.cc/en/Main/Software and download one. Old timers, time to leave your version 0017 behind! For some reason, version 1.0.2 has some issues, so avoid that (or upgrade to 1.0.3 or higher).
Next, we need the files that tell the Arduino software how the ATtinyís behave. You can get that from the MITís High-Low Tech groups github at https://github.com/damellis/attiny/archive/master.zip. Download and extract the files to a temporary directory.
Navigate into the extracted files and move the "attiny" folder (one level down from the "attiny-master" folder) into your "sketchbook/hardware" directory. You can find the location of the sketchbook in the preferences of the Arduino software, and you may have to create a hardware folder inside the sketchbook directory, but this will allow you to update the Arduino software version without having to reinstall the ATtiny cores each time.
Start, or restart the Arduino IDE and see if it shows the new cores under [Tools] -> [Boards]. If not, go back and double check the your work. If my instructions are confusing, see if MITís work better for you at http://hlt.media.mit.edu/?p=1695.
Now is a good time to connect your Arduino and load the ArduinoISP sketch on to it:
Step 2: Wiring up the ATtiny circuit
Letís wire up the ATtiny for programming first. I keep talking about an ATtiny85, but the pinouts and code would be the same for an ATtiny25 or 45. Additionally, you can use the 14-pin ATtiny 24/44/84, but the pin positions will be different. Letís look at what we are dealing with:
Place the ATtiny in a breadboard, and wire it up to the Arduino as indicated. You are also going to need to override the standard Arduino reset behavior by connecting a 10uF capacitor between the Reset (Arduinoís, not the ATtiny) and GND.
Step 3: Load the code using Arduino
Once you have the circuit wired and double checked, letís try and talk to it:
You should see some warnings about PAGEL and BS2 signals you can ignore.
If you successfully uploaded a program, congratulations. Even though the chip isnít doing anything yet, It is all downhill from here.
Add an LED and current limiting resistor from Pin 6 (D1) to GND. Is it blinking? If not, so back and re-check the wiring and steps.
If you have a blinking LED, yea! This may be all you need (or want) to learn. If you leave the tour here, I wonít be offended, but I do still have a few tricks up my sleeve.
Step 4: Prototype and test light sensor circuit on circuit on Arduino board
Time to move back to the Arduino board. Getting an LED to blink is pretty much a go/no-go test. We want to look at adding some analog reading, and that will be far easier in a place where we can do a Serial.println() to see what is going on. We canít do that on an ATtiny.
We attached the LED to D1 on the ATtiny. D1 on the Arduino is TX which we need for normal Serial.print() to work, so we will have to select another. The ATtiny D1 is also PWM, which will come in handy if we want to do any fading, so letís pick Arduino D5 as a suitable PWM pin. Wire an LED from D5 to GND using current-limiting 330-ohm resistor.
The new trick we want to try is getting an analog light reading. Iím using an CdS LDR with a 10k-ohm pull-down resistor, but also had success with a TEPT4400 light detector with a 100k-ohm pull-down resistor. Part of the advantage of using the Arduino for development is we can see what values are returned and adjust the resistors and threshold values before transferring code to the ATtiny where it is hard to determine what is going on under the hood. Connect one leads of the CdS (or the short leg of the TEPT4400) to VCC and the other leg to A3. The pull-down resistor, which acts as a voltage divider to get the signal into the appropriate range, goes from A3 to GND.
Step 5: Code a working example for Arduino circuit
Now, with a blinking LED and light values being read in, Iíll leave it up to you to decide where to go from there. Decide what behaviors you want from your system and then code and test. There are limitless ideas to try. Firefly? Night light? Intrusion detection? Iíll supply sample code that fades the LED in and out for about five minutes after the lights go out. Sort of a firefly night light.
Step 6: Prototype and test light sensor circuit for ATtiny on breadboard
So now we have a working Arduino circuit and firmware tested. Time to move it to ATtiny land.
First, add the light sensor short leg to VCC and long leg to ATtiny pin 2 (D3). These pin numbers get confusing, especially when you start talking about analog pins. In this case, analog pin 3 is the same as digital pin 3, so that makes it a bit easier unless you start to believe A2 and D2 are the same (they are not). You also need to connect your pull-down resistor from D3 to GND.
For the code, we should be able to just change pin "led = 5" back to "led = 1" like we had originally, right? We will also need to comment out all Serial library calls because they are not available for the ATtiny cores. With a little preprocessor directive action, we can set the sketch up so it will configure the pins and Serial library correctly based on the board selection in the IDE. That would be a cool trick, right?
To upload the code to the ATtiny, we are going to have to reload the ArduinoISP sketch and set up for the ATtiny85. Remember how?
Hopefully, the ATtiny code is behaving exactly as it did on the Arduino. The delay() function should control time the same, within the accuracy of the ATtinyís internal 8-Mhz oscillator. If there are timing issues, it usually relates to a miss-match between the fuse settings and the board selection. When in doubt, re-burn the fuses using the "Burn Bootloader" menu choice.
Step 7: Build a stand alone ATtiny circuit using coin cell
Warning: To minimize the part count, and since we are using a 3V coin cell and PWM, Iím going to omit the current limiting resistor to the LED. With this in mind, should you hook this up to a programmer supplying 5V, and turn the LED full on, you will cook it in short order. You have been warned.
Step 8: But how long will it last?
I'm sure the number one question will be, "how long will it last?", so let's try and answer that here.
I'm not sure where your code ended up, but my example blinks the LED for about 2 seconds out of 3, and keeps going for about 5 minutes before going dark. That means that in any given day, a majority of the time the CPU is just idling. So how much current is drawn by just idling? A little testing with a multi-meter shows the following:
Since a CR2032 coin cell is rated at about 225 mAh, an ATtiny85 idling at 1 mA should give us over a week of run time. Can we do better? The AVR chips do have sleep modes, and since we only need to check light levels every second or so, that could save a bit. Putting my ATtiny in to sleep mode showed about 0.3 mA, but I'm not sure if I trust my meter at those level. It is definately an improvement, and should get us to 3 weeks. Using AAA or AA batteries would definately extend the life considerably!
Unfortunately, the code for sleep mode puts us back in to the cryptic C code land I am trying to help you avoid! Perhaps that will be the topic for another instructable. Here is a good video to get you started: http://www.youtube.com/watch?v=Ob5fHhPDqvU
Step 9: There must be an easier way!
Wiring up an Arduino to program ATtinys is not terribly difficult, but I've had to do it enough to want to simplify the process. Some friends and I designed a shield that can program ATtiny85 (8 pin), ATtiny84 (14 pin), ATtiny2313 (20 pin) and ATmega328 (28 pin), all from the same ZIF socket! It has proved very handy. Check out its instructable at http://www.instructables.com/id/Arduino-AVR-Progamming-Shield/.
Also, since I've completed this project, I've move on to a couple of other interesting Attiny project involving sound, light, and touch which might be more suitable for beginers:
Hopefully youíve learned how easy it is to use the Arduino and Arduino IDE to get working on to an ATtiny! No need to tie up a valuable Arduino board when a $2 chip and coin cell will do. Think of all the possibilities. I look forward to seeing what you do with this!
You can catch a glimpse of what other mischief I've been up to at: http://makersbox.blogspot.com/