Using an ATtiny as an Arduino
This is chapter forty-four of a series originally titled “Getting Started/Moving Forward with Arduino!” by John Boxall – a series of articles on the Arduino universe. The first chapter is here, the complete series is detailed here.
Please note that the tutorials are not currently compatible with Arduino IDE v1.0. Please continue to use v22 or v23 until further notice.
Welcome back
In the last few weeks an article about how to use either an Atmel ATtiny45 or ATtiny85 microcontroller with Arduino software took my interest. The team at the High-Low Tech Group at MIT had published the information and examples on how to do this, and it looked like fun – so the purpose of this article is to document my experience with the ATtiny and Arduino. All credit goes to the interesting people at the MIT HLT Group for their article and of course to Alessandro Saporetti for his work on making all this possible.
Introduction
Before anyone gets too excited – there are a few limitations to doing this…
Limitation one – the ATtiny has “tiny” in the name for a reason:
Therefore we have less I/O pins to play with. Consider the pinout for the ATtiny from the data sheet:
So as you can see we have thee analogue inputs (pins 7, 3 and 2) and two digital outputs with PWM (pins 5 and 6). Pin 4 is GND, and pin 8 is 5V.
Limitation two – memory. The ATtiny45 has 4096 bytes of flash memory available, the -85 has 8192. So you may not be controlling your home-built R2D2 with it.
Limitation three – available Arduino functions. As stated by the HLT article, the following commands are supported:
-
pinMode()
-
digitalWrite()
-
digitalRead()
-
analogRead()
-
analogWrite()
-
shiftOut()
-
pulseIn()
-
millis()
-
micros()
-
delay()
-
delayMicroseconds()
So please keep the limitations in mind when planning your ATtiny project.
Getting Started
Hardware
The ATtiny needs to be wired up a certain way to allow the Arduino to act as a programmer.
For those with an Arduino Duemilanove/Freetronics TwentyTen (click schematic to enlarge):
For those with an Arduino Uno/Freetronics Eleven/EtherTen etc. (click schematic to enlarge):
Note the Uno version of the schematic has a 10uF electrolytic capacitor between Arduino RST and GND. Follow the schematics above each time you want to program the ATtiny. For more frequent use they would be an excellent candidate for a protoshield.
Software
From a software perspective, to use the ATtinys you need to add some files to your Arduino IDE. First, download this zip file. Create a folder called “hardware” in the the folder where you save your sketches. If you are unsure of this location, in the Arduino IDE select the File>Preferences option and you will see the following:
The top field “Sketchbook location:” will tell you where to put the files.
Next, extract the contents of the downloaded .zip file into the newly-created hardware folder. Finally, plug in your Arduino board, load the IDE and upload the ArduinoISP sketch which is in the File>Examples menu. Whenever you want to upload a sketch to your ATtiny, you need to upload the ArduinoISP sketch to your Arduino first. Consider this sketch the “bridge” between the IDE and the ATtiny.
Next, create your sketch. Note the following pin number allocations:
- digital pin zero is physical pin five (also PWM)
- digital pin one is physical pin six (also PWM)
- analogue input two is physical pin seven
- analogue input three is physical pin two
- analogue input four is physical pin three
Before uploading your sketch – you need to select the correct board type. Select Tools>Board>ATtiny45 (or 85) (w/ Arduino as ISP). Then upload as normal. You will see an error message in the status window of the IDE as such:
The message is “normal” in this situation, so nothing to worry about.
For a quick demonstration, load the Blink example sketch – File>Examples>1. Basics>Blink. Change the pin number for the digital output from 13 to 0. For example:
void setup()
{
pinMode(0, OUTPUT);
}
void loop() {
digitalWrite(0, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(0, LOW); // set the LED off
delay(1000); // wait for a second
}
Upload the sketch using the method as described earlier. The matching circuit is:
Although it’s only a blinking LED, by making it work you have mastered the process. However, for the non-believers:
Final example
We test the digital outputs with digital and PWM outputs using two LEDs instead of one:
And the sketch:
void setup()
{
pinMode(0, OUTPUT);
pinMode(1, OUTPUT);
}
void loop()
{
for (int a=0; a<6; a++)
{
digitalWrite(0, HIGH); // set the LED on
digitalWrite(1, LOW); // set the LED off
delay(1000); // wait for a second
digitalWrite(0, LOW); // set the LED off
digitalWrite(1, HIGH); // set the LED on
delay(1000); // wait for a second
}
for (int z=0; z<3; z++)
{
for (int a=0; a<256; a++)
{
analogWrite(0, a);
analogWrite(1, a);
delay(1);
}
for (int a=255; a>=0; --a)
{
analogWrite(0, a);
analogWrite(1, a);
delay(1);
}
}
}
And a quick demonstration video:
So there you have it – another interesting derivative of the Arduino system. Once again, thanks and credit to Alesssandro Saporetti and the MIT HLT Group for their published information.
Update 25/11/11 - this method has been updated for use with Arduino v1.0 beta.
Have fun and keep checking into tronixstuff.com. Why not follow things on twitter, Google+, subscribe for email updates or RSS using the links on the right-hand column, or join our Google Group – dedicated to the projects and related items on this website. Sign up – it’s free, helpful to each other – and we can all learn something.











Would you be so kind as to help me decipher the multiple roles of the pins? It’s been a while and I can’t seem to remember all of the acronyms beyond VCC and GROUND.
It’s all in the data sheet – http://www.atmel.com/dyn/resources/prod_documents/2586S.pdf
[snip]
KUTGW!
Thanks – fixed. I had pasted in some later testing code in instead of the demo sketch. It was late.
john
thats neat, am I right to asume u can use the I2c bus to expand out the i/o
Yes, please see this – http://www.arduino.cc/playground/Code/USIi2c
Thanks for the great article. However, I miss the usual stuff around the ATiny (clock) etc. Are those actually delivered by the connected Arduino? Would it be possible to disconnect the Arduino after having programmed the ATiny?
Yes. Please read the full article, there are example schematics showing how it works independently after programming via the Arduino.
Fantastic! in fact I`v just bought 10 of them on the strength of this, sometimes a whole a 328p is overkill.
just out of curiosity though, what clock speed were you running at?
I think they default to 1MHz, but can be set to 8MHz with a fuse bit thingy?
pins being scarce, I`d like to avoid tying up another 2 of them with an external xtal, and 1MHz may be a little too slow if I`m shifting out to pair of 74595`s.
all the best!
Pretty sure it’s 1MHz. I don’t work with raw AVR so can’t comment about changing fuses.
Yesterday: Why is my LED blinking to fast?- I’ve set my ATtiny85 to run at 8 Mhz. Setting CKDIV8 gives me the proper blink frequency. I was very confused at this point. 1Mhz or 8 Mhz?, Do I have to change the boards.txt file?
Today: Oh updateI Nice! But if i select one of the new boards i get: Error Compiling(‘TCCR1B’ undeclared).
Do you have any suggestions what wents wrong?
Many thanks for another nice written article
and cheers from Austria,
Johannes
Sorry, not sure. I don’t work with raw AVR programming
I just had a look in that ZIP file that goes in the IDEs h/ware directory, and the file “Boards.txt” seems to contain the clock freq for the chips
set to 1MHz, have a go at changing the 1 to an 8 (after you back up the original), and see what if any difference that makes.
I did the similar with the TV out Lib as I needed the Sync to be on another pin and it worked.
look the sections that read “cpu=1000000L” and make it “cpu=8000000L” instead, it might work and certainly wont hurt anything if it doesn`t.
should be good for a laugh just to see what happens
Ups, yes of course it’s 1Mhz. I already looked at the boards.txt file before posting but for any reason “cpu=1000000L” looked like “cpu=8000000L”
Another thing i noticed while playing with the Blink example on different clock sources was that the internal RC oscillator is very inaccurate. Of course i’ve read about this but i won’t have thought that it’s that bad. I made a short video using two ATtiny85′s(one running at 1Mhz internal RC oscillator; the other runs at 20Mhz external crystal) and two LED’s to demonstrate this. Both tinys run the same Blink Example sketch: http://www.youtube.com/watch?v=VUs8w1A6ROQ .
Very well done – thank you for sharing that with us John.
john
I thought the Uno had a bug in the firmware, where it couldn’t be used as a programmer? If that’s been fixed, it’s worth mentioning. (Every Google sends me to the old Arduino forum saying Uno’s new firmware doesn’t work for this).
The Uno works, but circuit has an extra capacitor as described in article.
cheers
john
One thing I don’t get is uploading the sketch. For the ATtiny, I’ve only used programmers (like the USBtinyISP). I see here a regular Arduino board is configured to act AS a programmer… but each time you run it, is it going to -both- flash the mini Arduino bootloader -and- upload the sketch? Or just the sketch?
Also, is it a small deviation from this if one wants to use an USBtinyISP to upload the sketch to the ATtiny? I have an Uno and I could use it, but it would be more practical to use the programmer for this. If that’s something I should glean from the datasheets, I’ll keep reading
.
So once the tiny is programmed, all you need to make it a “mini arduino” is the chip, resistor, and 1 LED and that’s it? I realize a lot of what goes into an Arduino is “unnecessary” for basic running purposes, but shouldn’t the most barebones arduino have a few caps in there? Interesting.
Thanks again for this series. Alessandro’s page is good, but it’s also a little discouraging if you’re a novice, and you plug in a few more details than the MIT HLT page.
“One thing I don’t get is uploading the sketch. For the ATtiny, I’ve only used programmers (like the USBtinyISP). I see here a regular Arduino board is configured to act AS a programmer… but each time you run it, is it going to -both- flash the mini Arduino bootloader -and- upload the sketch? Or just the sketch?”
- not sure, however in this case it seems moot.
“Also, is it a small deviation from this if one wants to use an USBtinyISP to upload the sketch to the ATtiny? I have an Uno and I could use it, but it would be more practical to use the programmer for this. If that’s something I should glean from the datasheets, I’ll keep reading .”
- I don’t own a USBtinyISP so can’t comment on this
“So once the tiny is programmed, all you need to make it a “mini arduino” is the chip, resistor, and 1 LED and that’s it?”
Yes. See my demo schematic, sketch and video
Yep, I read it all and watched the video… I usually prefer to understand something before I actually make it, but in this case that seems unlikely, and I’ll just go ahead with the Uno-as-programmer method (at least for the first attempt).
The attiny series are my favorite articles, so far. I just noticed your blog does not have a “donate” link, nor any affiliate links one could order products through (in the US) . Your call, but you go above and beyond any other blog both with these articles and your comment monitoring. Thanks.
Ah, fair enough.
I’m glad to hear you enjoy the site. Moderated comments are necessary, you wouldn’t believe the crap people try and post.
Have fun
john
Hey John – How difficult do you reckon it would be to get the ATTiny working with a shift register IC and an LCD screen with TMP36 to build a standalone temperature unit (like one of your previous tutorials). I realise I could just use the Freetronics Eleven chip into a DIP socket, but that’s no fun!
I am trying to save space too and as has already been mentioned, the full AT chip is a bit overkill.
Well it does support shiftOut… but honestly I wouldn’t be bothered. Instead there is an I2C library for the ATting85 – http://www.arduino.cc/playground/Code/USIi2c which could work with an I2C LCD backpack. Personally I would just use the normal ATmega328 – save time and a lot of effort.
Great Stuff, however, I see nothing about an Attiny when I do “Tools/board”
Was that supposed to come from the zip file? I extracted that as you describe.
I stand corrected, I just needed to restart the software. Can an Arduina Nano actually be used to do the programming of the Attiny this way?
Excellent. Hypothetically a nano should work, however I don’t have one to test this for you at the moment.
john
Thanks, I settle for hypothetically and will try myself
Anyone know what would cause this error, when choosing File->Upload Using Programmer?
Tools>Programmer is set to Arduino As ISP (and I previously uploaded that to the Uno, without error).
Selected Board is ATtiny 85 (tried both 1 and 8MHz).
Actually I get the error when running through the MIT article this references (when I attempt upload of the modified Blink sketch).
Using arduino 1.0-rc2. The only avrdude installed is the one that comes with arduino (and it is 5.11)
…
avrdude: stk500_paged_write(): (a) protocol error, expect=0×14, resp=0×11
avrdude: stk500_cmd(): programmer is out of sync
I am not sure abt yr firts error message, but I get the 2nd only if I am connected to the wrong com port. Not sure though if that is the cause and solotion of yr problem
neat, I`v just done this project myself now and it works well, a Note for those with Arduino Nanos, you also have to put the capacitor across GND and RST like you do with the Uno, else you`ll get a Sync error, I used a 33uF as there was one handy on my desk at the time (Lazy).
Cheers John
Thanks YT2095, your comment was just in time as I was going to program an ATtiny85 today. However, I will share my experience as adding the 33 uF (I also happened to have that laying on my desk) gave me an initial problem: If put in the circuit too early, one cannot load any sofware too the Arduino Nano. So here is my sequence:
-Connect Arduino Nano and Attiny85 as described in the article, but do not connect the capacitor yet.
-Load the ‘Arduino as ISP sketch’
-Disconnect USB
-Insert capacitor
-Reconnect USB
-Load Attiny sketch in the Arduino software
-choose ‘ATtiny85 with Arduino as ISP’ in yr ‘Tools->boards’
-Upload
-remove USB
-Remove capacitor and at least the line to the reset of the Attiny (the one from pin 1)
Done!
A question as well: I could ofcourse just try, but maybe someone knows: can the Attiny85 be programmed again using ISP after the first programming? I read somewhere that in programming, Pin1 was changed into an output pin and therefore no longer available as a Reset. Therefore the fuses must be restored to their original state before ISP can be used again? Is this true? It wld seem a bit odd!
I can (and have) re-programmed mine may rimes, I even set the reset pin to an output for an extra line so I could run 6 leds, it refused to set as an output and always stayed as a reset.
so although it probably Would prevent further reprogramming IF you could successfully make the reset an Output, it`s unlikely you`ll be able to do that with a simple Pinmode command as I tried.
and yes, I made my nano into an ISP before I even connected anything up, but when it Is a programmer, then it`s good to add the cap and the Tiny85.
the problem I`m having currently, is trying to get the thing to run at 8MHz or even 16, but it just wants to run at 1MHz no matter what I try?
I`v edited the Boards text file to 8MHz and it`s made no difference, I thinks it`s a Fuse bit thing I need to alter, and I`m not sure the arduino IDE will allow that.
Thanks YT2095, I already thought it cld easily be reprogrammed, but it is nice to hear it confirmed. I guess the announcement I read was just one of the many ‘not true statements’ abt atmel programming.
Yep, I guess the 1 vs 8/16 MHz is a ‘fuse’ issue. For the moment 1 MHz is OK with me, saves two pins I can use as output. But I wld like to know I am able to run it at 8 MHz, should I wish so.
Thanks for yr excellent reply on this excellent site
Anyone know if arduino can be loaded on an ATtiny24? If so, how?
Thanks.
Juliean
I am by no means an expert on this but I think I know. I presume you mean to put a bootloader on the Attiny24 and thus use it in combination with the Arduino IDE software?
I dont think that is possible coz the Attiny doesnot support all the functions of e.g. the 169 or the 328 and that are present in the software. Also, I fear there wld not be much space left if the bootlaoder is in there.
Having said that, I am sure there could be some form if limited bootloader that could work on the Attiny’s, but I have not found one yet.
Juliean, I seem to have trouble posting here but google on Attiny bootloader and you will find some links to existing attiny bootloaders
Every comment is moderated in order to stop spammers, idiots and foul language.
Cheers
john
I see, sorry for my many tries then
Relax, post away.
If you are interested in having more detailed discussion perhaps use our Google Group – http://groups.google.com/group/tronixstuff
Very interesting post. Looking forward to try it.
I have one question about the pwm output:
Apparently Pin 5 is linked to timer 0 (OC0A), Pin 6 to timer 0 and 1 (OC0B, OC1A) and Pin 3 to timer 1 (OC1B). Is there a way to have 3 pwm outputs available using the simple analogWrite on Pin 3 ?
I have been successful programming the Attiny with an Duemilanove as the isp but have had no success using the Uno. I have tried with the 10uf cap between reset and ground but i always get a not in sync error. Any suggestions?
hello. I am a newbie, and as I read everywhere, attiny has two pwm outputs. My question is: Can I read and use a pwm signal?. It would be great to use in one ir shooter for rc toys.
thank you and regards.
You could use an ATmega328 with the IR library to receive IR, however I don’t think there would be enough memory in an ATtiny to be able to do that.
John
the question is not to receive IR. The project is to read a pwm signal from the RC receiver, and send via IR a signal to shoot one camera. Very similar to this: http://www.pcmx.de/?p=30#more-30 but activated with pwm instead the buttons.
I am no expert in this but I think it should be possible as the arduino can read a pulse width I guess. Have you tried here?:
http://www.arduino.cc/en/Reference/PulseIn
Can’t you have more digital outs with this method ? I am thinking I could control an RGB led with an ATTiny as Arduino…
Not sure what you mean with can’t you have more outputs? More than what? Oviously the Attimy has less outpits than the Atmega, but the ‘BlinkM is based around an Attiny and that controls LED’s and I presume you could hook uo a 595 and have more outputs that way
Great article! would this work with a ATtiny2313? (using the correct pins when programming obviously)
Is the setup different for a Mega2560? I have seen tutorials that use the Uno and the Duemilanove, but none for the Mega2560. So far I have been unsuccessful in programming an ATTiny45, so I was wondering if something in the wiring needed to be changed for the Mega2560. For example, is a capacitor used between reset and ground or should a resistor connect reset to 5V? Before I go on forums and start posting my error messages, I thought I would check to see if the problem was a simple different between Uno/Duemilanove and Mega2560 setup.
Good question. I’m afraid I don’t have access to a Mega to check for you, so would be best to ask around.
Cheers
john
Really enjoying the ATtiny85. Driven by a couple of AA cells you don’t even need the resistors on the LEDs! — no other components besides the chip and LEDs. I’ve been making some neat electronic candles like this. Great little kits for learning to solder too!
It’s SO much fun being able to put an Arduino where and Arduino would cost too much.
[m]