Attiny
28904-dscn3788.JPG

This is a tutorial on how to program an ATTiny. It is adapted from the ATTiny Tutorial from MIT's High-Low Tech. (I think the link is dead…)
The tutorial is written assuming you are pretty comfortable programming and using an Arduino Uno.

The Tutorial teaches how to program an ATtiny 85 and 85


Programming an ATtiny

This tutorial shows you how to program an ATtiny45, ATtiny85, ATtiny44 or ATtiny84 microcontroller using the Arduino software. These are small, cheap ($2-3) microcontrollers that are convenient for running simple programs. The ATtiny45 and ATtiny85 have eight legs and are almost identical, except that the ATtiny85 has twice the memory of the ATtiny45 and can therefore hold more complex programs. The ATtiny44 and ATtiny84 have 14-legs and more inputs and outputs. Thanks to Mark Sproul for his work on making the Arduino core portable across processors.

+ ATtiny45/85 vs. an Arduino Board

The ATtiny45 or 85 is a great option for running simple Arduino programs: it’s small, cheap and relatively easy to use. It does, however, have some limitations relative to the ATmega328P on an Arduino Uno.

  • There are fewer pins, meaning you can’t connect as many components.
  • There’s less flash memory (4KB or 8KB instead of 32KB), meaning your programs can’t be as big.
  • There’s less RAM (256 or 512 bytes instead of 2KB), meaning you can’t store as much data.
  • There’s no hardware serial port or I2C port (Wire library), making communication trickier. (There are workarounds, like the SoftwareSerial library or the TinyWire library, but they’re not as robust and flexible.)
  • It has a slower clock speed and runs natively at 1 MHz (see below for changing to 8 Mhz or 20 MHz)

In short, then, if your project requires only a few simple inputs and/or outputs, you’re probably fine using an ATtiny. If you’re trying to hook up more components or do more complex communication or data processing, though, you’re probably better off with something like the ATmega328P on an Arduino Uno. If you want something smaller and cheaper than a full Arduino board, you might try using an ATmega328P on a breadboard instead.

Materials and Tools

  • An in-system programmer (ISP), a piece of hardware used to load programs onto the ATtiny. Either:
  • ATtiny85 or ATtiny85 (8-pin DIP package) or an ATtiny44 or ATtiny84.
  • A solderless breadboard and jumper wires (unless you’re using the TinyProgrammer w/ the ATtiny45 or 85)

Download

* ATtiny master.zip (hosted by GitHub)
* ATtiny master.zip (hosted here, 11/12/2013)

Installing ATtiny support in Arduino

  • Download and install the Arduino software.
  • Download the ATiny master.zip file from the link above.
  • Unzip the attiny master.zip file. It should contain an “attiny-master” folder that contains an “attiny” folder.
  • Locate your Arduino sketchbook folder (you can find its location in the preferences dialog in the Arduino software)
  • Create a new sub-folder called “hardware” in the sketchbook folder, if it doesn’t exist already.
  • Copy the “attiny” folder (not the attiny-master folder) from the unzipped ATtiny master.zip to the “hardware” folder.
  • You should end up with folder structure like Documents > Arduino > hardware > attiny that contains the file boards.txt and another folder called variants.
  • Restart the Arduino development environment.
  • You should see ATtiny entries in the Tools > Board menu.
ATtiny-Boards-Menu1.png

Connecting the ATtiny

You’ll need to provide power to the ATtiny and connect it to your programmer. That is, connecting MISO, MOSI, SCK, RESET, VCC, and GND of the programmer to the corresponding pins on the ATtiny. (Or, if you’re using a circuit w/ an ATtiny, simply connect the programmer to the ISP header on the board – you may also need to power the board separately.)

Screen-shot-2011-06-06-at-1.46.39-PM.png6121376853_e8355afe33.jpg

Programming the ATtiny

Next, we can use the Arduino as an ISP to upload a program to the ATtiny:

  • Open the Blink sketch from the examples menu.
  • Change the pin numbers from 13 to 0.
  • Select the appropriate item from the Tools > Board menu (leave the serial port set to that of your Arduino board).
  • Select the appropriate item from the Tools > Programmer menu (e.g. “Arduino as ISP” if you’re using an Arduino board as the programmer, USBtinyISP for the USBtinyISP, FabISP, or TinyProgrammer, etc).
  • Upload the sketch.

You should see “Done uploading.” in the Arduino software and no error messages. If you then connect an LED between pin 0 and ground, you should see it blink on and off. Note that you may need to disconnect the LED before uploading a new program.

Configuring the ATtiny to run at 8 MHz (for SoftwareSerial support)

By default, the ATtiny’s run at 1 MHz (the setting used by the unmodified “ATtiny45″, etc. board menu items). You need to do an extra step to configure the microcontroller to run at 8 MHz – necessary for use of the SoftwareSerial library. To change the clock speed, you need to 'burn' a new boot loader. (Note this doesn’t actually burn a bootloader onto the board)

  • Connect the Attiny as described above.
  • Select the appropriate item from the Boards menu, as you did above (e.g. “ATtiny45 (8 MHz)”). Then, run the “Burn Bootloader” command from the Tools menu.
  • This configures the fuse bits of the microcontroller so it runs at 8 MHz.
  • The fuse bits will keep their value until you explicitly change them, so you’ll only need to do this step once for each microcontroller. (Note this doesn’t actually burn a bootloader onto the board; you’ll still need to upload new programs using an external programmer.)

ATtiny Microcontroller Pin-Outs

ATtiny45-85.png
ATtiny44-84.png

Reference

The following Arduino commands should be supported

pinMode()
digitalWrite()
digitalRead()
analogRead()
analogWrite()
shiftOut()
pulseIn()
millis()
micros()
delay()
delayMicroseconds()
SoftwareSerial

Suggestions and Bug Reports

To report problems or suggest changes to the ATtiny support, please use the issues list on GitHub.
References

arduino-tiny: alternative approach to ATtiny support for Arduino
TinyWireM & TinyWireS: Wire (I2C / TWI) library for the ATtiny85 (using USI)
Servo8Bit: Servo library for the ATtiny45/85.

Alternative: ATmega328P on a Breadboard

If the ATtiny isn’t quite powerful enough but you still want to use a bare microcontroller instead of a full Arduino board, see this tutorial on using an ATmega328P on a breadboard. It allows you to use all the same functions and libraries as the Arduino Uno, but with just a microcontroller and a few small components.


Using Arduino as the ATtiny programmer

This tutorial shows you how to wire up an Arduino used as an ISP (programmer) to an ATtiny45 or ATtiny85. This allows you to write code for the microcontroller using the Arduino IDE.

Materials and Tools

  • Arduino Uno
  • ATtiny45 or ATtiny85 (8-pin DIP package)
  • A 10 uF capacitor (e.g. from Sparkfun or from Digi-Key)
  • Breadboard
  • Jumper wires

Turning the Arduino board into a programmer

We’re going to use the Arduino board to program the ATtiny. First, we’ll need to turn the Arduino board into an “in-system programmer” (ISP). To this:

  • Open the Arduino IDE.
  • Open the ArduinoISP sketch from the examples menu.
  • Note for Arduino 1.0: you need to make a small change to the ArduinoISP sketch before uploading it. Find the line in the heartbeat() function that says “delay(40);” and change it to “delay(20);”.
  • Select the board and serial port that correspond to your Arduino board. (The one you will use as the ISP, i.e. Arduino UNO)
  • Upload the ArduinoISP sketch.

Connecting the Arduino board and the ATtiny

  • Connect the Arduino board to the ATtiny as shown in the following diagram. (Use the dot in the corner of the ATtiny to orient it properly)
  • Cconnect a 10 uF capacitor between reset and ground on the Arduino board as shown in the diagram. (the stripe on the capacitor that’s marked with a negative sign (-) should go to ground).
    • The capacitor prevents the Arduino board from resetting (which starts the bootloader), thus ensuring that the Arduino IDE talks to the ArduinoISP (and not the bootloader) during the upload of sketches.
Screen-shot-2011-06-06-at-1.46.39-PM.png

Pin connections:

  • ATtiny Pin 2 to Arduino Pin 13 (or SCK of another programmer)
  • ATtiny Pin 1 to Arduino Pin 12 (or MISO of another programmer)
  • ATtiny Pin 0 to Arduino Pin 11 (or MOSI of another programmer)
  • ATtiny Reset Pin to Arduino Pin 10 (or RESET of another programmer)

Acknowledgement

This page is based off of MIT's High-Low Tech website. This is a bit of an update on it, but credit mainly goes to whoever wrote that post. Thanks!

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License