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

25 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?

  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. Once you are going to have an incredible set of six pack abs to
    show off. This means you have to do enough fat
    burning workouts and you need to start taking balanced healthy diets.
    It’s less difficult, and often more gratifying, to indulge our cravings.

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

  21. 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!!!

  22. Thank you very much, saved my day!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s