Programming ATtiny13 using Arduino ISP (and all the hacking involved!)

I recently came across MIT’s high low tech lab and was immediately drawn into their tutorial that allowed using Arduino Uno (and older versions) to be used to program ATtiny boards. The steps were simple and they had already updated their tutorial for Arduino 1.0 release (making it even more simpler). I couldn’t wait to try this out (especially since a lot of my projects barely require more than a couple of pins) and immediately went to sparkfun to order the ATtiny85 that they support.

I was already eyeing the Magic Chassis (discussion for another post) for some time and decided I would club them together. The Magic Chassis was quickly running out and ATtiny85 were already in backorder. Being an impatient person, I decided to order the ATtiny13 and try and hack the existing setup to make it work.

With the Arduino 1.0 IDE out, the new tutorial on the website is all the more simpler and only required modifying 2 files. You can access these changes from the github https://github.com/tekstop/attiny/tree/Arduino1 (Arduino1 branch).

The instructions are the same as those on the MIT high-low tech lab’s tutorials and the only real change required was adding this board to the boards.txt file. To do this, all I needed to do was look into a couple of datasheets and understand what the different settings really meant. This was the first time, I was really going into fuses and clk registers and it was pretty interesting and straight forward. My methodology in hacking this was:

Check the value of the fuse bytes in boards.txt for an existing mcu and find the values of the actual bits by going through the datasheet. The next part was just insuring that I set similar bit values for the ATtiny13 mcu. This was quite interesting especially with choosing the internal clock (9.6 Mhz) and prescaler options available.

Because the internal clock of the ATtiny13 is quite different from ATtiny85, the Software Serial library (only supports 8,16 or 20 MHz) will not work with ATtiny13 (9.6 MHz clock).

The pins_arduino.h file did not need to be touched as the pinouts for the tiny8 are the same.

With just these changes, I was ready to test my Arduino programmer.

1) Programmed the ArduinoISP sketch on my Arduino Duemilanove.
2) Copied the Github into the Arduino Sketch folder
3) Choose my Board from the IDE, selected the programmer as Arduino and connected pins as explained in the tutorials.
4) Burn the boot loader (one time). {It worked, gave some benign BS2 warning that can be safely ignored}
(At this point I was giddy with excitement, because nothing works on the first go, right!!!)
5) Chose the Blink sketch and wham uploaded it on to the board (success!)

And then reality struck! My LED didn’t blink! It just stayed on!. I reversed my commands and now saw that the LED stayed off, which gave me hope that at least something was being programmed correctly.

20111230-012614.jpg

My experiments showed that there was something wrong with the delay/millis commands. Normal read/write operations were running fine. Ofcourse, this prompted me to check my fuse values for the Clock [which seemed correct and changing them had no effect].

At this point, I started investigating the actual C code for delay and mills functions. Looking at the code in hardware/arduino/cores/arduino/wiring.c, I saw this line in code that seemed suspicious.


#if defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
SIGNAL(TIM0_OVF_vect)
#else
SIGNAL(TIMER0_OVF_vect)
#endif

I then further dug into the actual header file for the attiny13 (iotn13.h) to find that the interrupt macro for timer overflow for attiny13 was TIM0_OVF_vect and not TIMER0_OVF_vect. My Timer was not reporting the overflow and hence my timing functions were not working (This seems like an actual bug in the arduino 1.0 IDE). The fix is just adding an additional OR condition:


#if defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) || (__AVR_ATtiny13__)
SIGNAL(TIM0_OVF_vect)
#else
SIGNAL(TIMER0_OVF_vect)
#endif

This shouldn’t affect anything else and making that fix has my LED blinking correctly!

20111230-012837.jpg

65 responses to “Programming ATtiny13 using Arduino ISP (and all the hacking involved!)

  1. Hi,

    I’m a real real Arduino newbie but wanted to try programming the ATtiny13. Followed your instructions but got stuck when compiling the Blink example. The Arduino IDE 1.0 tells me, that ‘pins_arduino.h’ can’t be found:

    D:\src\arduino-1.0\hardware\arduino\cores\arduino/Arduino.h:212:26: error: pins_arduino.h: No such file or directory

    Do you have any suggestions?
    Markus

    • Hi Markus,
      Just look at the MIT tutorial (linked in the post) and the github site. You will need to copy and paste the folder from the github in to the default Arduino preference directory (where it defaults to saving sketches).
      The folder has pins_arduino.h.

  2. Hi,
    thanks for your quick reaction. I’m going to try figuring it out next weekend.

  3. Attiny13 is a lot slower than atmega, and one second delay on it lasts for about 6 seconds, how should this be compensated? Of course i could do smaller delay, but I wuld like to run the same code on arduino and on attiny. Is there a solution?

    • Hi , i m also having the same problem, 1 sec delay last for about 6 secs. You said that we have to burn bootloader first,i wanted to know the sequence of steps ,i mean first upload arduino isp sketch then burn bootloader then upload your code to attiny13 or do we have to do it any other way,like first burn isp sketch then upload attiny code and then burn botloader,pls reply as soon as possible

      • It is not bootloader, there is a problem with schematics. Share it for us to check

      • your talking about the code or the way connections i have made??
        if its the code then i’m pasting it below
        if not then reply again..
        const int buttonPin = 1; // the number of the pushbutton pin
        const int ledPin = 0; // the number of the LED pin

        // variables will change:
        int buttonState = 0; // variable for reading the pushbutton status

        void setup() {
        // initialize the LED pin as an output:
        pinMode(ledPin, OUTPUT);
        // initialize the pushbutton pin as an input:
        pinMode(buttonPin, INPUT);
        }

        void loop(){
        // read the state of the pushbutton value:
        buttonState = digitalRead(buttonPin);

        // check if the pushbutton is pressed.
        // if it is, the buttonState is HIGH:
        if (buttonState == HIGH) {
        // turn LED on:
        delay(300);
        digitalWrite(ledPin, HIGH);
        delay(300);
        }
        else {
        // turn LED off:
        digitalWrite(ledPin, LOW);
        }
        }

      • I have some comments regarding the code, but first show me schematics (connections)

      • how can i send an image here ,or give me your email address,
        b.t.w it is working nicely in proteus simulation and i have made connections same as that explained in many videos for miso ,mosi ,ss reset and sck,
        apart from these, switch is connected at pin 6 of attiny13 and led at pin 5, the other end of the switch is connected to 5v dc supply.

      • hi! pls reply if you can,i’m waiting for your reply..

  4. OK, I’ve found out, that you have to burn the bootloader first for it to run at the correct speed. Who would have known? 🙂

  5. That was the initial challenge for me! Figuring out the config files and the settings for the device.

  6. You know what is very weird? It seems, that for the first loop() run attiny13 executes the last statement in a loop first, and then proceeds to work normally. Why could this happen?

  7. I haven’t had time to look at that, but that would be weird! In my experiments I didn’t see that!

  8. You maked my day ! You’re awesome ! I’m trying for a long time to program ATtiny13 with arduino . thx a lot !

  9. this project support ATTINY2313 ?
    if yes,where can i find attiny2313 core ?

  10. I keep running into the error “avrdude: usbdev_open(): did not find any USB device “usb””. I believe the boards.txt script needs to specify the ArduinoISP as it’s loader. Any ideas? I’ve tried this on two different machines with the same outcome. Thanks!

    • I hadn’t realized that after programming the Arduiino as a programmer, there was an additional setting to set it as the active programmer for the software. “Tools > Programmer > Arduino as ISP”. Programmed like a champ. Thank You!!!!

  11. Pingback: The Tech Notes » Blog Archive » Programming an ATtiny13

  12. Thank you. Just modded wiring.c for tiny13!
    Works!

  13. Hi, I followed all the steps above and ended up with the LED lit continuously instead of blinking. Any idea where I should adjust?

    I’m using Arduino 1.0.1 and Arduino as ISP as programmer for my ATTiny13A.

    Thanks a lot for this interesting tutorial.
    Tom

  14. Hi, just made everything work now. I used IDE v1.0 with my Uno; all other steps are the same. I did not have to use 10uF capacitor.

    Thanks again for this excellent tutorial, reference links and modified files. Awesome indeed!

  15. This was extremely helpful, thanks! I’m new to all this stuff, so it took me quite a while to figure out that to find the hardware/arduino/cores/arduino/wiring.c file I had to go into the main Arduino.app package by right-clicking on it. Once I did that, it was smooth sailing!

  16. really thanks, I had the same error (no blinking led), and I had no idea what is the wrong. 1rst I ordered a new pack of attiny13…
    Thanks,

  17. “Programming ATtiny13 using Arduino ISP (and all the hacking involved!
    ) | Tekstop!” was in fact a really good post, . Keep
    creating and I’ll try to keep following! Many thanks ,Jeffrey

  18. Thanks for working this out…saved a lot of aggro!!

  19. Pingback: Прошиваем ATTiny13 c помощью Arduino ← BigBarrel

  20. Awesome, I had the same problem like you did, the LED wasn’t blinking, just shining. I did what you said and now it’s working. Very thank you!!!

  21. Thank you very much, saved my day!

  22. I’m using attiny13 and mega as isp ,I’m using led and switch,i have given a delay of 3 secs for led blinking before and after pressing switch,on pressing switch led turns on after around 3 secs(not exactly 3 secs) and then stays on for around 10 secs, I it something related to burning bootloader or problem is something else.

  23. ok ,i had not burned the botloader not even once so the problem of delay have reduced,the only problem now i have is that i’m not getting exact delay of 3 secs only small variation (around 2.9 secs).so thanks for the help
    Mr. Flicker

    • Glad to hear that it is OK now. The problem with duration may be lays in the fact that attiny is using internal crystal, which is not that precise as an external one :\

  24. Oops! sorry for spelling mistake 😛

  25. I’m having the problem above with the blink sketch where the LED stays on all the time. I investigated the solution above, modifying the iotn13.h file. I’m running 1.0.5-r2 Arduino software. I can’t find the lines that need to be modified in the iotn13.h file. Probably because I’m running a newer version of the software?
    Be gentle, I’m a noobie anxious to learn

  26. Hi Curt, You shouldn’t need to change anything iotn13.h. All you need to do is add the additional condition in wiring.c (which is still located at hardware/arduino/cores/arduino/wiring.c).
    Add the condition || (__AVR_ATtiny13__) to change the TIMER.

    • Thanks for the info. I’ll give it another try and report back. Thanks again for the quick reply!
      Curt

  27. Fantastic! I’m now blinking after making the change to the wiring.c file. You guys are lifesavers. With my very limited to nonexistent programming skills I would never have figured that out. Now to change the frequency so that it matches the program timing. I know I have that info somewhere.
    Again, thanks a million! Curt

  28. Here is the code change to get your blink rate in the ballpark.

    We then need to offset the clock speed (not a very elegant solution, but i’m working on it!):

    Open Hardware/attiny/boards.txt
    Scroll to the bottom of the file, this last few lines contains the data relevant to our chip
    Change “attiny13.build.f_cpu=9600000L” to “attiny13.build.f_cpu=1000000L”

    Thanks to this thread for the information!

    http://www.instructables.com/id/Programming-an-ATTiny13A-using-Arduino-servo-int/step2/Setting-up-the-IDE/

    Trying to give back for some of the help I have received here.

    Thanks, Curt

  29. many many thanks……

  30. I have changed the wiring.c but then I get the following message when I try to upload the code: processing.app.debug.RunnerException: Sketch too big; see http://www.arduino.cc/en/Guide/Troubleshooting#size for tips on reducing it.

    I’m just trying to upload the blink example.

    • In my wiring.c it says ISR(TIM0_OVF_vect) not SIGNAL …

    • I didn’t find any reply to your comment on my ISR vs SIGNAL. Thank you!

      In this blog post you say I have to add (__AVR_ATtiny13__) but in the description at GitHub it says defined(__AVR_ATtiny13__). It’s not a problem for me to test both. But I still get the error saying that my sketch is too big (only after I add (__AVR_ATtiny13__) ).

      Do you have any idea why? I use Arduino IDE 1.0.5 and have followed your example exactly. Both the video on YouTube and in this blog post.

  31. @Helge Johnsen: Maybe your sketch is really too big for the tiny13’s 1k Byte Memory.

    But what do I have to type / paste into the wiring.c file? Do I have to put something in these brackets “defined(..)”
    (Sorry, I’m new in C or C++)

  32. @Kilian You should just need to add an additional condition as shown below:

    #if defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny13__)
    SIGNAL(TIM0_OVF_vect)
    #else
    SIGNAL(TIMER0_OVF_vect)
    #endif

  33. Thank you very much for this fast awnser!
    But I’m still getting this error: http://qra.de.vu/006/1.html
    As I said I’m not good at it. 😛
    (My wiring.c file now looks like that: http://qra.de.vu/006/2.html)

  34. I don’t have an ATTiny13 anymore! 😦 So I can’t really test it out. The Atmel website seems to have also retired this!

  35. Ok. Could it be, that this hack doesn’t work with the new version of the IDE? (I installed 1.0.6 a few days ago.)
    Yesterday i uninstalled the IDE and deleted all remaining files (after backup). Then I installed it again and I still have the problem.

  36. Hi Kilian, its possible that Arduino has updated the libraries which might not be correctly compiling anymore. I wrote it up with the Arduino IDE 1.0.0, so you could try that. Also, others have gotten it to work with Arduino 1.05 as well.

    http://arduino.cc/en/Main/OldSoftwareReleases

  37. SUPERTHANKS * 10000!!! This almost drove me crazy. You saved me!

  38. hola a todos mi nombre es lorenzo y te agradesco despues de tanta lucha funciona correctamente ide 1.0.5. segui los paso y funciona gracias

  39. http://www.htlinux.com/product/attiny-isp-shield Here is the ATTiny ISP Shield makes the job very much easier.

  40. Buenas tardes. El parpadeo me funciono perfecto desde que corregi el (__AVR_ATtiny13__), pero si quiero leer el valor de un potenciometro con analogRead() y con ese valor prender y apagar leds en base a la funcion if(), no funciona y no se donde puede estar el problema. A alguien le paso esto mismo? Como lo puedo solucionar? Uso un IDE 1.0.0
    Tambien soy nuevo en ATtiny 13.

    Gracias!

  41. The information you posted about it is so useful, I am expecting for your next post.

  42. Hey man! Thank you very much! I don’t know if you still write in this blog and read this messages, but you gave me a great help with this old article! I’ll review the rest of the blog, looks interesting, but it’s a pain that you don’t write more here 😦
    Thank you again!

  43. I truly enjoy reading through on this internet site , it has got good articles . “Wealth and children are the adornment of life.” by Koran.

  44. Pingback: Tic-Tac TV Remote Jammer • Tech Projects

  45. Pingback: Tic-Tac Remote Disruptor: Innovating TV Jamming Technology

Leave a reply to alam Cancel reply