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.
Updated 07/02/2013
Did you know you can use an Atmel ATtiny45 or ATtiny85 microcontroller with Arduino software? Well you do now. The team at the High-Low Tech Group at MIT have 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 and share the instructions with you in my own words. 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:
Clik here to view.

it’s the one on the left
Therefore we have less I/O pins to play with. Consider the pinout for the ATtiny from the data sheet:
Image may be NSFW.
Clik here to view.
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()
- SoftwareSerial
Other functions may work or become available over time.
Limitation four - You need Arduino IDE v1.0.1 or higher, except for v1.0.2. So v1.0.3 and higher is fine.
So please keep these limitations in mind when planning your ATtiny project.
Getting Started
There are two ways you can program your ATtiny. Method one describes using an Arduino board, method two describes using a separate USB programmer.
Programmer method one
You can use an existing Arduino-compatible board as a programmer with some external wiring. Before wiring it all up – 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, build the circuit as shown below (click image to enlarge):
Image may be NSFW.
Clik here to view.
Depending on the Arduino board you’re using, you may or may not need the 10uF capacitor between Arduino RST and GND. Follow the schematic above each time you want to program the ATtiny.
Software
From a software perspective, to use the ATtinys you need to add some files to your Arduino IDE. First, download this zip file. Then extract the”attiny” folder and copy it to the “hardware” folder which sits under your main Arduino IDE folder, for example (click image to enlarge):
Image may be NSFW.
Clik here to view.
Now restart the Arduino IDE. As you’re using the Arduino as a programmer, you need select “Arduino as ISP” – which is found in the Tools>Programmer menu. Next – select the board type using the Tools>Board menu. Select the appropriate ATtiny that you’re using – with the 1 MHz internal clock option. Now you can enter and upload your ATtiny sketch. When uploading sketches you may see error messages as shown below:
Image may be NSFW.
Clik here to view.
The message is “normal” in this situation, so nothing to worry about. Now – scroll down until you reach the “Creating Arduino sketches for ATtinys” section to continue with the tutorial.
Programmer method two
Get yourself one of these USB programming boards:
Image may be NSFW.
Clik here to view.
They’re great – you just drop in the ATtiny – plus you can use it as a breakout board. And there’s an LED on pin 5 for testing with blink and debugging in general.
Insert your ATtiny into the board, plug it into a free USB port and the hardware is done. If you’re using Windows – install the necessary drivers (32-bit or 64-bit). Next, restart the Arduino IDE and select “USBtinyISP” – which is found in the Tools>Programmer menu. Next – select the board type using the Tools>Board menu. Select the appropriate ATtiny that you’re using – with the 1 MHz internal clock option. Now you can enter and upload your ATtiny sketch. Note that you don’t need to select a USB port in the IDE, and the option may well be blanked out – instead, check the bottom-right of the IDE. It will show the USB port being used by the programmer board, for example:
Image may be NSFW.
Clik here to view.
Creating Arduino sketches for ATtinys
When creating your sketches, note that the pin number allocations are different for ATtinys in the IDE. 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
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 methods described earlier. If you’re using programmer method one, your matching circuit is:
Image may be NSFW.
Clik here to view.
If you’re using programmer method two, this will blink the on-board LED.
Final example
We test the digital outputs with digital and PWM outputs using two LEDs instead of one:Image may be NSFW.
Clik here to view.
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.
Image may be NSFW.
Clik here to view.
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.
Image may be NSFW.
Clik here to view.
Clik here to view.
