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. |
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 |
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:
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.
ReplyDeleteGood work.
Mike
a few questions
ReplyDelete1.the tiny needs the arduino bootloader to work right?
2. which are the digital and analog pins for the tiny?
3. well thats it!!
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.
ReplyDeleteI think there's a picture of the pinout on the second from top picture.
Haha I hope this has helped!
How would you put the resistor between reset and power? Can you post a picture of that?
ReplyDeletehey i got one more question.
ReplyDeletethe 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????
@JD I will try to when the weekend comes. It's pretty simple.
ReplyDelete@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...
so which board should i chose?the arduino uno or tiny?
ReplyDeleteand 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?
@francisroan I'm pretty sure its in the article... You can upload the example blink, but you'll have to change ""13" to "1".
ReplyDeletehey one more!!;
ReplyDeletefor connecting the arduino and the tiny we just need to use the iscp pins on the arduino right? and connect that to the tiny!?
Seems you didn't have time to make the 110ohm resistor change this weekend.
ReplyDeleteIs 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)?
@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.
ReplyDeletehttp://www.arduino.cc/playground/Main/DisablingAutoResetOnSerialConnection
This is where I learned about it. It interferes with the ArduinoISP sketch otherwise.
This comment has been removed by the author.
ReplyDelete@Thatcher, Thank you, that helps!
ReplyDeletedo u know how to do this for an attiny 2313 since i couldnt get attiny85 nor45??
ReplyDeletebump last post^^^ I'd like to know that too
ReplyDeleteAccording to what I found, the attiny2313 has 20 pins, so this won't work.
ReplyDeleteI found some ATtiny85's on eBay: http://www.ebay.com/sch/?_nkw=attiny85&clk_rvr_id=296416320626
@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.
ReplyDeleteIf 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!
If you get errors like
ReplyDeleteavrdude: 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)
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
ReplyDeleteHey! That's pretty cool! I may have to get one
DeleteGreat work, I appreciate your hard work.
ReplyDeletewhat is arduino
Thanks for this great Tutorial!
ReplyDeleteI 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!
What are the connections for this if you are using an ATtiny2313? I'm kinda stuck...
ReplyDeleteDo 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.
ReplyDeleteIf 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.
Thanks for the quick response! It was the second one i was asking for. Thanks a lot!
ReplyDeleteOh, 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!
ReplyDeleteYou can run the ATtiny85 without an external crystal oscillator.
DeleteThis is really cool. Any pointers on using TWI or similar? Just using the Arduino Wire library does not work. Do I need to write from scratch?
ReplyDeleteI haven't used many libraries on these chips. However, last I checked, a number of people had written libraries specifically for the Attiny chips. You might want to check around for those.
DeleteAlso, maybe you would want to learn some straight AVR code! I haven't tried this ever to be honest but it would be a good way to delve deeper into the platform