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:

  • [Tools]->[Serial Port] -> select your correct port.
  • [Tools]->[Boards]-> select the Arduino board you are using (not the ATtiny).
  • [File] ->[Examples] -> “ArduinoISP”
  • [File] ->[Upload]

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:

  • Arduino D10 -> ATtiny pin 1 [RST]
  • Arduino D11 -> ATtiny pin 5 [MOSI]
  • Arduino D12 -> ATtiny pin 6 [MISO]
  • Arduino D13 -> ATtiny pin 7 [SCK]
  • Arduinio 5V -> ATtiny pin 8 [VCC]
  • Arduino GND -> ATtiny pin 4 [GND]

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:

  • [Tools]->[Boards]-> select “ATtiny85 (internal 8 MHz clock)”
  • [Tools]->[Programmer]-> “Arduino as ISP”
  • [Tools]->”Burn Bootloader” (this sets the chip fuses, nothing else).
  • [File]->[Examples]->[01.Basic]-> “Blink”
  • Change “led = 13” to “led = 1” since ATtiny doesn’t have a D13!
  • [File]->”Upload Using Programmer”

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?

  • [Tools]->[Boards]-> check the Arduino board you are using (not the ATtiny).
  • [File] ->[Examples] -> “ArduinoISP”
  • [File] ->[Upload]
  • [Tools]->[Boards]-> select “ATtiny85 (internal 8 MHz clock)”
  • [File]->”Upload Using Programmer”

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

Now we have a working ATtiny prototype circuit running code we are happy with. Time to build one for real. One of the things that interested me in Karl’s original firefly project was that he didn’t use a circuit board. It is a bit tricky, and takes some trial and error, but it can be done. If the chip is placed on it’s back, it will still be possible to place it in a programmer or breadboard to update the code later if we need to. I am also going to follow his example of eliminating the current limiting resistor. We can get away with that since we are using PWM and not applying continuous current. The internal resistance of the coin cell will also keep us from popping an LED (I use them to test LEDs all the time).

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:

  • Arduino on 5V: ~ 8 mA.
  • Attiny85 on 3V at 8 Mhz: ~4 mA
  • Attiny85 on 3V at 1 Mhz: ~1 mA

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 https://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/

Happy Making!

<p>Nice project - have been looking through your projects and they are very professional looking.</p><p>Did you know that you don't actually need the light sensor?</p><p>An LED acts as a photodiode so you can use the same LED to sense light and generate it. That was the basis of the ATTiny Throwie (Throwduino) here: </p><p><a href="https://www.instructables.com/id/Throwduino-Basic-Light-Sensing-Flashing-Throwie-/" rel="nofollow">https://www.instructables.com/id/Throwduino-Basic-L...</a></p><p>An arduino so cheap you can throw it away!</p><p>Ugi</p>
<p>Nice instructable.<br>Given the fact that the UNO (clone) is available for about 2.50 euro the Adafruit and Sparkfun kits are a tad pricy. <br>In case one wants to make several attiny projects, consider buying 10 for about 77cts a piece: </p><p><a href="http://www.aliexpress.com/item/Free-Shipping-10PCS-LOT-X-Original-Integrated-circuit-parts-ATTINY85-ATTINY85-20SU/1326708202.html" rel="nofollow">http://www.aliexpress.com/item/Free-Shipping-10PCS...</a></p>
When you buy Cheap clones, you are short-cutting the people who put the work into the project. Think about all the good support and community that SparkFun and Adafruit provide. I consider buying their kits as an investment. You know they have researched the parts and tested the designs.
<p>that ofcourse is a personal decision. however since it is open hardware, defacto there is nothing wrong with buying cheap.</p><p>i am sure sparkfun and adafruit &quot;researched the parts and tested the design&quot;, but for the arduino itself there isnt really that much to research or test anymore. I have bought stuff at adafruit, if it was new and unique, but for a simple clone? or an old and established chip?</p>

About This Instructable




More by MakersBox:Easy Joule Thief Soldering Project Using an Arduino Drawing Robot with Hour of Code Tutorials Arduino Drawing Robot 
Add instructable to: