Program an ATtiny Using an Arduino

I am a big fan of the Arduino platform. For those who aren’t familiar with the Arduino, it is a microprocessor development environment & ecosystem. You can visit their site for more info. I started playing with the Arduino and soon discovered the ATtiny microprocessor chip, which is a much smaller cousin to the ATmega series which is used in the Arduino.  Unfortunately, the Arduino IDE did not support it. Since most micro development apps run only on Windows (I use Ubuntu Linux), and require either expensive compilers or hard to use C or Assembler, I was never up to the effort required in using the ATtiny chips.

All of this changed yesterday when I was going through my Arduino notes and came across some attempts by others to use the Arduino IDE to program the ATtiny. One fellow was successful on the ATtiny45. Another tried on the ATtiny85 but didn’t seem to get as far. After much searching, I stumbled across the Arduino-Tiny project. The project is fairly mature, and they have already done all the hard work. The result is fantastic. You can program in C++ using the regular Arduino libraries (which have been modified for the ATtiny). Of course, many features are not available, but that is expected. When you have your sketch written, you can upload it to the ATTiny using either an in-system programmer (like USBTinyISP) or even better, you can use an Arduino itself.

Close only counts in horseshoes and hand grenades

I was all set now. I had an Arduino. I had an ATtiny85 chip. Following this tutorial, I wired everything up, and uploaded the sketch to make the Arduino an ISP (in-system programmer). I then opened the example sketch “blink” and changed the pin from 13 to 0. When I tried to upload the sketch to the ATtiny, I got this error:

avrdude: stk500_recv(): programmer is not responding

I was ATtiny85 programmed with ArduinoIDE - Blinky Sketchclose, but no cigar. After considerable searching again, I at last found the solution to the problem. It appears you must tie the reset pin of the Arduino to high (+5v) via a small resistor (120 ohms is recommended, but it also worked with 500 ohms in my case). At last, sweet success – a blinking LED. My thanks to the folks on this Arduino thread for that insight.

Programming Your Own ATtiny – in 9 easy steps

For those wanting an easy way to program ATtiny chips, here is what you need to do:

  1. Make sure you are using the latest Arduino IDE – currently version 022. Close it if you have it open.
  2. Go to http://code.google.com/p/arduino-tiny/downloads/list, and download arduino-tiny-0022-0008.zip or the latest version thereof.
  3. In your sketches folder, create a folder called “hardware” and put the contents of the zip file in it.
  4. In that hardware folder, under the “tiny” folder, you should see a file called “boards.txt”. Open it for editing. For each ATtiny chip you plan to program, you need to set the ISP you’ll be using. In this example, I am using the Arduino as an ISP, so this block (for ATtiny85, 1MHz clock) will look like this when finished:

    # The following DO work (pick one)...
     # attiny85at1.upload.protocol=avrispv2
     attiny85at1.upload.using=arduino:arduinoisp
     # attiny85at1.upload.using=pololu

    If you haven’t figured it out, the ‘#’ sign at the beginning of a line will comment it out. Make sure only one of these lines is not commented – the one you want to use.

  5. Open the Arduino IDE. You should now see each of the boards listed in the file you just edited.
  6. Now open the ArduinoISP sketch (found in the file/examples menu). Make sure your Arduino board is selected in the list of boards (your Arduino board, not the ATtiny, we are not to that step yet). Upload the sketch. You can add some diagnostic LEDs (with 1k resistors) at this point (or even before you upload the sketch). Put an LED (with resistor) on the following pins to ground:

    9: Heartbeat - shows the programmer is running
    8: Error - Lights up if something goes wrong (use red if that makes sense)
    7: Programming - In communication with the slave (use green if you like)

    You should now see the LED at pin 9 slowly pulse on and off.

  7. VERY IMPORTANT: To finish turning your Arduino into an ISP, you must connect a 120 ohm (500 ohm seems to work as well for me anyway) resistor from the reset pin to +5v. What this does is prevents the Arduino IDE from resetting the Arduino itself. After all, we are not programming the Arduino anymore after this, but the ATtiny. Don't forget to remove this resistor when you want to program your Arduino again.
  8. Now wire up your Arduino to the ATtiny chip. Here are the connections you need for each ATtiny pin to your Arduino:
    ATTiny45 & ATTiny85 pinout

    ATTiny45 & ATTiny85 pinout

    1. digital 10
    2. nc
    3. nc
    4. ground
    5. digital 11
    6. digital 12
    7. digital 13
    8. +5V
  9. You are ready to program. To test it do the following:
    1. Open up the Blink sketch, under examples/basics.
    2. Change all instances of pin 13 to pin 0.
    3. Under the Tools/Board menu, select the ATtiny version you are using. I am using an ATtiny85, clock speed 1MHz. No external clock is needed.
    4. Hook up a 1k resistor & LED from ATtiny pin 5 (digital 0) to ground.
    5. Upload the sketch. Your Arduino pin 7 pin should blink, and the error pin should stay off. When it is done, your ATtiny LED should be blinking on and off.

Troubleshooting

Verify each step and the results. Hook up the LEDs to your Arduino to make sure it is working as an ISP. Make sure you have selected the right board for each chip you are programming.

Conclusion

You are now (hopefully) able to program an ATtiny with the same ease as you can an Arduino. The ATtiny chip is cheaper and smaller than the ATmega, and is perfect for light duty applications. The default fuse settings for ATtiny85 appear to be 1MHz for the clock. If you want a faster clock, you will need to change those. That will be my next project.

If you have trouble or questions, post me a comment below. I'll do what I can to help. Enjoy those ATtinys.

This entry was posted in Tech and tagged , , . Section: . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

43 Comments

  1. Posted May 29, 2011 at 4:36 am | Permalink

    You might have a typo in step 6. I think it is “ArduinoISP”, not “ArduinoIDE”.

    • Posted May 29, 2011 at 1:29 pm | Permalink

      You are absolutely correct. I have made the correction. Thanks for the tip.

  2. pendantic
    Posted May 29, 2011 at 4:08 pm | Permalink

    You might have a typo in step 6. I think it is “ArduinoISP”, not “ArudinoISP”.

    (-, just kidding, great tutorial!

  3. Rick
    Posted May 30, 2011 at 3:13 pm | Permalink

    It would be quite helpfull if pics could be programmed too from the Arduino

  4. Posted June 1, 2011 at 2:30 am | Permalink

    @Rick
    Chipkit would be better option. ChipKITs can be programmed using the original Arduino™ IDE modified to support PIC32.

  5. Posted June 3, 2011 at 9:14 am | Permalink

    These comments on programming PICs in an Arduino-like fashion are great. In looking at the two solutions mentioned, the Pinguino is promising. I like it being open source. The downside is it uses a different IDE and currently does not support C++; only C. The support of C++ is the Arduino’s biggest advantage to me. The Chipkit also looks great – it uses the ArduinoIDE. The downside is it is a proprietary board for only one PIC chip.

    What would be great to see is the combination of these two. The ideal would use the ArduinoIDE as does the Chipkit, but support entire families of PIC chips, such as the PIC18F series with built-in USB support. It looks doable – the Chipkit people have done it for one of the chips. Comments? Ideas?

  6. Mahmut
    Posted June 7, 2011 at 8:16 am | Permalink

    Thank you for your article. I was trying to program another chip using arduino UNO for 4-5 weeks. I’ve read lots of articles but somehow I couldn’t upload. At last, I managed to program attiny2313 at 1mhz with your code. And there is no resistance between 5v and reset pin! Thank u.

  7. Vincent
    Posted July 6, 2011 at 10:18 pm | Permalink

    hello all’

    I am new ao this audriuno.
    recently I got the Audriuno board. Now I have a new Atmega168 .
    How can I program it in Audriuno?
    Is there any special needed when programming a IC first time?

    Reply pls I am a kid help me

  8. Coding Badly
    Posted July 11, 2011 at 12:38 am | Permalink

    Thank you for writing this article.

    The pin diagram is incomplete. AREF (pin 0) should be indicated. Pin 4 also supports PWM. It would be helpful to mark SCK, MISO, and MOSI.

    • Posted August 24, 2011 at 11:22 pm | Permalink

      I’ve been working on some better pinouts for the ATtinys. If I get them done, I’ll try to post them.

      In the library code I have from the ATtiny project for Arduino, digital pin 4 does not support PWM, even though the underlying hardware does. Is my code out of date?

  9. Florian
    Posted August 13, 2011 at 3:08 am | Permalink

    I think I love your blog ^^
    can I use smd attiny’s too?

  10. Coding Badly
    Posted August 14, 2011 at 1:28 am | Permalink

    > The default fuse settings for ATtiny85 appear to be 1MHz for the clock. If you want a faster clock, you will need to change those.

    You can change the fuse settings by selecting the appropriate board then executing Tools / Burn Bootloader.

  11. yachris
    Posted September 11, 2011 at 7:49 pm | Permalink

    Hey — another big thanks for putting this together… it works!

    Worth noting: I got the “please define PAGEL and BS2 signals” error, but it appears from a bit of googling that it’s more of a warning. The code works fine, no one seems to be stopped, and it’s expected that the next release of the IDE will have a newer release of the AVR-GCC compiler, which will have the right values for this setting.

    Thanks again!

  12. Posted November 21, 2011 at 10:35 am | Permalink

    I had to change two things to let my ATtiny85 work
    1 The reset resistor MUST be ~ 120ohm, not larger.
    2 Boards.txt for
    attiny85at8.name=ATtiny85 @ 8 MHz (internal oscillator; BOD disabled)

    attiny85at8.build.f_cpu=1000000L
    #attiny85at8.build.f_cpu=8000000L 8 x times too slow!

    • Posted December 6, 2011 at 5:01 pm | Permalink

      Thanks for the clarification.

      1) 120 ohms is recommended. The 500 ohms that worked for me is not universal.
      2) The ATTiny85 chip comes pre-programmed at 1MHz, not 8MHz. Instead of making the change you did, you should use attiny85at1 instead of attiny85at8. I referenced the 8MHz section by mistake in the article. I have corrected it to reflect the 1MHz default.

      You should probably make the changes to CHIP.upload.using=arduino:arduinoisp to every ATTiny chip & clock you intend to use, where CHIP is attiny85at8, attiny85at1, etc. Then, make sure you choose the right chip/clock for your application in the Tools/Board menu of the Arduino IDE.

  13. Posted January 10, 2012 at 4:47 am | Permalink

    Thanks again for the article.Really thank you! Awesome.

  14. fellow programmer
    Posted February 7, 2012 at 2:53 pm | Permalink

    Hi.

    I know the following arduino commands are supported by the ATtiny
    pinMode()
    digitalWrite()
    digitalRead()
    analogRead()
    analogWrite()
    shiftOut()
    pulseIn()
    millis()
    micros()
    delay()
    delayMicroseconds()

    but will the following control structures work on it aswell?

    if
    if…else
    for
    switch case
    while
    do… while
    break
    continue
    return
    goto

    if you can reply asap, that would be great!

    • Posted February 7, 2012 at 6:29 pm | Permalink

      Yes – you use the same compiler and the instruction set is the same. Not only can you write standard C, but C++ classes as well – just like the Arduino.
      You do need to watch memory more carefully though. Since there is no bootloader, you do have the entire memory space (an extra 2k). I am finding I can do a fair amount of stuff in 8K, but it is pretty limited to simpler programs. I would not even consider using a chip with less memory than that.

      • fellow programmer
        Posted February 9, 2012 at 1:50 pm | Permalink

        thank you for your reply… im not to worried about memory… my projects are complex, but small!

      • Anurag
        Posted June 12, 2012 at 1:30 pm | Permalink

        Hey Scott,

        thanks for this awesome info and tutorial.

        I am a newbie to Arduino my question is … if I write up code in Arduino code (NOT C++ or C) will that work on the ATiny or will I have to write the code only in C++ for it to work ?? Because I am not good with c/c++ it would be very comfy if I could just use the default arduino code… And also like the previous person asked how do I find out which arduino command will work and which ones won’t ??

        thanks

        • Posted June 12, 2012 at 11:32 pm | Permalink

          Anurag,

          Thanks for the feedback.

          The default Arduino code is C++. The Arduino part is their library to make it easy to use. The same commands/functions work for both Arduino and ATtiny for the most part. The main difference is which pins are available. I am working on another article to give more details. Check back in a bit.

        • Edwin
          Posted September 13, 2012 at 11:27 am | Permalink

          as Scott has not come back yet, let me add. Yes it is completely possible. You can set up and test your program in the IDE/Arduino and then use your Arduino to burn that code on an attiny85.
          Just make sure you are only using those commands that the Attiny knows.
          you will need a ‘core’ though for yr attiny

          Have a look here:
          http://elabz.com/arduino-shrunk-how-to-use-attiny13-with-arduino-ide/#comment-17350

  15. kees van den akker
    Posted February 21, 2012 at 10:38 am | Permalink

    I allready used this succesfully for Attiny45 but can I use this methode for Attiny2313 as well ?
    The fact is that I need more inputs, my sketch is small enough for the 2313.

    • Posted February 22, 2012 at 2:42 pm | Permalink

      Absolutely. You may need to check your SKETCHES/hardware/tiny/core/tiny/pins_arduino.c file for the proper pinouts.

  16. Posted April 14, 2012 at 6:02 am | Permalink

    You ought to be a part of a contest for one of
    the best websites on the web. I most certainly will recommend this blog!

  17. Posted April 24, 2012 at 2:46 pm | Permalink

    How about the attiny 2313 , how do i have to connect it to the arduino and where to see how the pins are positioned (where is digital pin 1,2,3 and so on?)
    Thanks!

  18. Posted April 25, 2012 at 6:04 am | Permalink

    Sorry just found the pins_arduino file, but which pins are analog inputs – only PB0 and PB1 ?

  19. EG
    Posted August 4, 2012 at 8:49 pm | Permalink

    Great job! I’m using ATtiny 45 for this tutorial. Thanks.

  20. Palmtree
    Posted August 15, 2012 at 1:30 pm | Permalink

    Hi Scott,
    I am new to this so I was wondering if I can use the ATtiny 13 with your code?

  21. Mike
    Posted October 19, 2012 at 1:24 pm | Permalink

    What about a .c file with a makefile? Do I have to compile and install that from a CMD window or can I do it from within the Arduino IDE? The makefile has avrdude fuse settings:
    avrdude -p $(AVRDUDE_TARGET) -c $(PROGRAMMER) -P $(PORT) -v -e -b 115200 \
    -U lfuse:w:0xE4:m -U hfuse:w:0xDF:m \
    -U efuse:w:0xff:m \
    -U flash:w:$(PRG).hex

  22. Mike
    Posted October 22, 2012 at 8:14 pm | Permalink

    well, I thought I had it working but now when try to upload a sketch I see one quick blink on the red led (error) then the green led flashes several times. The IDE reports that uploading is complete and displays no error messages but the attiny led does not blink. I wonder if I damaged the attiny at some point?

    • Posted October 24, 2012 at 2:02 pm | Permalink

      Hard to say – lot’s of things can go wrong. Try again, turning on verbose output during upload. It sounds like it is uploading. Check your sketch and LED pin to make sure they are indeed the same. Also, try using a different pin.

  23. Posted November 17, 2012 at 10:31 am | Permalink

    Howdy! This post could not be written any better!
    Reading this post reminds me of my old room mate! He always kept talking about this.
    I will forward this article to him. Fairly certain he will
    have a good read. Many thanks for sharing!

  24. newbieJ
    Posted July 8, 2013 at 5:52 am | Permalink

    hey I want to know,if the micro-nucleus.hex file which is the bootloader needed for attiny 85 to be programmed by usb ,can I burn it using arduino as ISP ??

    • Posted July 12, 2013 at 10:15 pm | Permalink

      Yes. The Arduino when configured as an ISP and the ArduinoIDE selected to use it as such will burn any code desired to the ATTiny through the standard ICSP connector. Sounds like a fun project.

  25. Jim Peachey
    Posted December 27, 2013 at 6:10 am | Permalink

    Hi, this is just what i am looking for as i build models and want to incorporate Arduinio in getting all the lighting correct. I was going to use an Arduino board in the model! Which ATTiny85 can you sue, there seem to be so many different types. Is it any of the ATTiny 85 range? I see 20su and 20 PU etc when i do a search for ATTiny 85. Any help greatly appreciated.

  26. Posted February 26, 2014 at 3:21 am | Permalink

    Hey… Thanks for the great tutorial.
    Looks like highlowtech.org moved the tutorial on you… It’s now here: http://highlowtech.org/?p=1706

  27. Jeannine Huffman
    Posted May 27, 2014 at 11:16 pm | Permalink

    I am a teacher trying to help students make simple inexpensive projects using the ATtiny 85 so they can take their projects home. We are able to use the Arduino to program the ATtiny to blink but cannot seem to get a servo to work. The libraries out there do not work, and we don’t know c++ just simple code modifications with arduino.cc

10 Trackbacks

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code lang=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" extra="">

*