Saturday, August 13, 2011

Programming the ATTiny85, Arduino Style

    This is a sort of a continuation of the last post. In the last post, I showed how to load the ATTiny85 cores into the Arduino IDE. If that worked for you, cool! This how to actually program the chip. If it didn't work... put a comment somewhere. I'll try to help.
    Anyway, programming the ATTiny is very easy. The only thing you really need to do is set up a target board. This is either a breadboard, perfboard, or PCB circuit that interfaces the ATTiny to an Arduino board. The circuit should have these connections:

This is called ISP circuit. The chips communicate over
SPI via this configuration.



     What this circuit does is connect the ATTiny to power and ground, but it also has four wires for communication. It does this using a protocol called SPI. The four connections are SCK (clock), MISO and MOSI (communication), and Reset (resets the chips when it is done). That circuit connects the SCK, MISO, and MOSI pins of both chips to the corresponding one on the other chip. The reset pins of the two chips are not connected, however. The Master (Arduino in this case) has a digital pin connected to the actual reset pin of the slave (ATTiny85). On the Arduino Uno and Duemilanove (and some other ones of the same shape), SCK is pin 13, MISO pin 12, MOSI pin 11, and reset pin 10. The ATTiny85 pinout is below.
ATTiny85 pinout, showing the uses of all the pins.
     The breadboard programmer above isn't the only way making a target board. I made one out of a small piece of perf board, a DIP 8 socket, a five-pin header, and a bunch of wires.
That one wire that shoots off in both pictures is going to power.



    It is is the same circuit as the breadboard, only more permanent. Same connections, though.

Actually Programming the Chip

    Once you have one of these target boards, either on a breadboard or perfboard (or something else), we can actually program it. The first thing to do is upload the ArduinoISP sketch onto the Arduino board. You find it here:
File/Examples/Arduino ISP
    And then upload it. What it does is a couple of things: the first thing it does is emulate an AVRISP programmer. The computer asks what it is, and it says it is a AVR programmer instead of a more generic serial device. The computer sends it the program (in hexadecimal form) and then it goes to its second function. It loads the program hex file onto the other chip via SPI and then relays whether or not the operation worked to the computer.

So connect your target board, make sure everything is connected, and (IMPORTANT!) set the Tools/Board Menu to ATtiny85 (w/ Arduino as ISP). Note: you may have to put a 110 ohm resistor between reset and power on the Arduino board. Then you can upload your program. I used this one. It is for the board that I fixed in the last post!


int toast = 0;


void setup(){
  pinMode(0, OUTPUT);
  pinMode(1, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  PORTB = B0000100;
}


/*


0 - H
1 _ H
2 - L
3 - H


*/


void loop(){
  PORTB = B0001011;
  delay(int(random(2000))*10);
  toast = int(random(4));
  for(int i = 0; i <= int(random(5,12)); i++){
    delay(int(random(30, 60)));
    digitalWrite(toast, LOW);
    delay(int(random(30,60)));
    digitalWrite(toast, HIGH);
    delay(40);
    PORTB = B0001011;
  }
  //delay(10000);
}



That should do it. You'll get this message in the bottom window:


If another set of text appeared, something is wrong. Here is a good site for finding out what the errors mean. Otherwise just Google it. Now you can test the chips on a breadboard or a PCB or something... I really hope this helps people. I just love the ATTiny chips because they're so small yet can still do many of the same things as a normal Arduino. Remember, though, not every function works.

27 comments:

  1. This is too cool. I think some of the Arduino stuff is massive overkill. This seems to hit those projects on the spot. I will give this a try.
    Good work.
    Mike

    ReplyDelete
  2. a few questions
    1.the tiny needs the arduino bootloader to work right?
    2. which are the digital and analog pins for the tiny?
    3. well thats it!!

    ReplyDelete
  3. No, actually. The tiny doesn't need a bootloader. The chips are programmed by SPI by default (like shown here), but a bootloader changes that to something else, like how a normal arduino loads a program via serial. Also, a bootloader would take up all the program space on the chip, so it's not a great idea.

    I think there's a picture of the pinout on the second from top picture.

    Haha I hope this has helped!

    ReplyDelete
  4. How would you put the resistor between reset and power? Can you post a picture of that?

    ReplyDelete
  5. hey i got one more question.
    the program(here)for blinking an led is longer than the program used to blink an led on the arduino!11why??and wat is that int toast and everything??????
    and one more thing after doing this will there be any differences in the arduino board like do we need to change any settings????

    ReplyDelete
  6. @JD I will try to when the weekend comes. It's pretty simple.

    @francisroan well this is a random interval blink program. Your typically blinky code is on, delay, off, delay, loop. This chooses a random pin and blinks it randomly for a bit. Int toast is the variable that I stored the random value in. What do you mean change settings? You will have to choose it in the board menu...

    ReplyDelete
  7. so which board should i chose?the arduino uno or tiny?
    and wat i ment of changing the settings is that after programming the tiny should i make any changes if i want to upload a program for arduino?

    and if i just wan to put one led on, on the tiny (pin no:1) should i just just upload the arduino example blink?

    and wat is the memory of this tiny?

    ReplyDelete
  8. @francisroan I'm pretty sure its in the article... You can upload the example blink, but you'll have to change ""13" to "1".

    ReplyDelete
  9. hey one more!!;

    for connecting the arduino and the tiny we just need to use the iscp pins on the arduino right? and connect that to the tiny!?

    ReplyDelete
  10. Seems you didn't have time to make the 110ohm resistor change this weekend.

    Is it correct to put the resistor between pin 1 (reset pin, see also your schematic where blue wire is connected) of the tiny and pin 8 (5V pin, red wire is connected to it in schematic)?

    ReplyDelete
  11. @JD Yikes! Forgot that. Actually, you put the resistor between the power and reset on the Arduino board. This effectually disables the autoreset function that is part of the circuit.

    http://www.arduino.cc/playground/Main/DisablingAutoResetOnSerialConnection

    This is where I learned about it. It interferes with the ArduinoISP sketch otherwise.

    ReplyDelete
  12. This comment has been removed by the author.

    ReplyDelete
  13. @Thatcher, Thank you, that helps!

    ReplyDelete
  14. do u know how to do this for an attiny 2313 since i couldnt get attiny85 nor45??

    ReplyDelete
  15. bump last post^^^ I'd like to know that too

    ReplyDelete
  16. According to what I found, the attiny2313 has 20 pins, so this won't work.

    I found some ATtiny85's on eBay: http://www.ebay.com/sch/?_nkw=attiny85&clk_rvr_id=296416320626

    ReplyDelete
  17. @francisroan @James It requires other cores. I'll do a tutorial on it if you want. It's the same process, really, but just different files.
    If you want to give it a go, the files are on http://hci.rwth-aachen.de/luminet This page, about half way down. Best of luck!

    ReplyDelete
  18. If you get errors like

    avrdude: stk500_paged_write(): (a) protocol error, expect=0x14, resp=0x11
    avrdude: stk500_cmd(): programmer is out of sync

    Get the corrected version of ArduinoISP

    http://mega-isp.googlecode.com/files/ArduinoISP.04.zip

    (thanks to http://amanica.blogspot.com/2011/11/my-quest-to-program-attiny85-with-my.html)

    ReplyDelete
  19. I made a shield to do this (in cooperation with Flytron): http://flytron.com/open-source-hardwares/162-tinyshield-all-in-one-avr-programmer-shield-for-arduino.html

    ReplyDelete
    Replies
    1. Hey! That's pretty cool! I may have to get one

      Delete
  20. Great work, I appreciate your hard work.
    what is arduino

    ReplyDelete
  21. Thanks for this great Tutorial!

    I have tried this with the Arduino V 1.0 and a Arduino UNO.
    The Result was a few Error Messages like "Arduino.h: no such file or directory!" and others. I have tried a few things and found the following Workaraound:

    - go to the Folder "Arduino Sketch Folder"/hardware/attiny45_85/cores/attiny45_85/

    - create a empty File named "Arduino.h"

    - insert the following line into this Arduino.h File:
    #include "WProgram.h"

    - Restart Arduino and Select from the Toolbar "Tools->Programmer->Arduino as ISP

    - Compile and upload your Attiny Sketch

    I have tried a few Example Sketches on the Attiny45 with this Workaround and all Sketches worked!

    ReplyDelete
  22. What are the connections for this if you are using an ATtiny2313? I'm kinda stuck...

    ReplyDelete
  23. Do you mean the physical connections or the core installation? Here's a page that might help - http://toasterbotics.blogspot.com/2011/08/attiny2313-with-arduino.html - if you want help with the installation of the cores.

    If you want help with the physical connections from the programmer/Arduino to the ATtiny 2313, they're essentially identical. If you look in the datasheet of the '2313, you should see a pinout diagram showing the location of the MISO, MOSI, SCK, and RESET pins. Once you know where the pins are, just connecting them to the programmer in the same way as the ATtiny85.

    ReplyDelete
  24. Thanks for the quick response! It was the second one i was asking for. Thanks a lot!

    ReplyDelete
  25. Oh, and i've got another question: do you need an external crystal/timer IC, or can you run an ATtiny by itself on a breadboard? Thanks!

    ReplyDelete
    Replies
    1. You can run the ATtiny85 without an external crystal oscillator.

      Delete