Programming ATtiny85 / 84 / 45 / 44 / 25 /24

About the post

Often there are projects for which you actionally don’t need the big ATmegas like the 328(P) or 168. These microcontrollers are used e.g. on the Arduino Nano or the Arduino Pro Mini board. You should always check whether you can get along with the space- and energy-saving little brothers ATtiny85, 84, 45, 44, 25 or 24 (short: ATtiny85 & Co).

In this post I want to show you how to upload your sketches to ATtiny85 & Co using the following devices:

  • Arduino as programmer and the Arduino IDE
  • USBtinyISP programmer and Arduino IDE
  • AVR Dragon or Atmel-ICE and Atmel Studio

I will only briefly go into the latter method, as a step-by-step introduction would be too long for this post. At a later date, I will come back to this as a separate topic.

Likewise, I will discuss the characteristics of the ATtinys and the differences to the big brother ATmega not here, but in another post. This post is primarily about getting started and uploading sketches.

If you want it even easier, you can also use the Digispark as an alternative. This is a module based on the ATtiny85, which is programmed via USB. I have described it here.

Pinout of ATtiny85 & Co

Pinout of ATtiny85 / 45 / 25
Pinout of ATtiny85 / 45 / 25

The pinout schemes of the ATtiny85, 45 and 25 are identical. In addition to VCC and GND, there are six I/O pins that have multiple functions as you are used to from the Arduino. Pins 1, 5, 6 and 7 alias PB5, PB0, PB1 and PB2 alias RESET, MOSI, MISO and SCK are used to upload the sketches.

A data sheet for this subfamily can be found here.

Pinout of ATtiny84 / 44 / 24

The pinout schemes of the ATtiny84, 44 and 24 are also identical. In contrast to the ATtinyx5 series, 12 I/O pins are available. In the further course of this post, I will primarily deal with the ATtinyx5 series. It shouldn’t be too difficult to transfer the circuits and explanations to the 4 series. 

A data sheet for this subfamily can be found here.

Uploading with the Arduino IDE

Preparing the Arduino IDE for ATtiny85 & Co

First, you need to “teach” the Arduino IDE how to deal with the ATtinys. In the first step, you add the board manager URL for the ATtinys:

File – > Preferences – Additional Board Manager > URLs.

Click on the marked icon and add the following:

https://raw.githubusercontent.com/damellis/attiny/ide-1.6.x-boards-manager/package_damellis_attiny_index.json

as a separate line in the window that pops up. Then close the windows with “OK”. 

Enter board manager URL for ATtiny family
Enter board manager URL for ATtiny family

Then go to:

Tools – > Board: …. – > Board Manager

and search for “attiny”. You select and install the package developed by David A. Mellis. Then you restart the Arduino IDE and that’s it.

Install the board manager package
Install the board manager package
Board manager package ATtiny for ATtiny85 and Co.
Board manager package ATtiny (ATtiny25 and 24 are also supported)

Arduino as programmer for ATtiny85 & Co

Wiring

First of all, we use an Arduino UNO as programmer. A Nano, Mega, Pro Mini, etc., would do the same. My example is a simple blink sketch. You find the circuit below. The flashing LED is connected to PB4 (pin 3). Between the Arduino RESET Pin and GND you place a 10 µF capacitor. This is usually recommended, but for me it also worked without this measure. Otherwise, you connect the components as follows:

Connection scheme Arduino UNO - ATtiny85 (also applies to ATtiny45 and 25) or ATtiny84 (also applies to ATtiny44 and 24)
Circuit for programming the ATtiny85 / 45 / 25
Circuit for programming the ATtiny85 / 45 / 25

Example sketch

I don’t think I have to explain the sketch, except perhaps that the pin designation (ledPin = 4) refers to PBx and not to the pinout number. 

int ledPin = 4;

void setup() {
  pinMode(ledPin, OUTPUT);
}

void loop() {
  digitalWrite(ledPin, HIGH);   
  delay(200);                     
  digitalWrite(ledPin, LOW);    
  delay(200);                     
}

 

Upload

Now you have to make sure that the sketch is uploaded to the ATtiny and not to the Arduino. To do this, first upload the ArduinoISP Sketch (examples) to the Arduino. For this step you still choose the Arduino UNO as board in the Arduino IDE. 

Selection of the ArduinoISP sketch
Selection of the ArduinoISP sketch

Then you choose your ATtiny, the clock speed you want and the right port:

Selection of ATtiny85, 45 or 25; selection of clock and port in the menu.
Selection in the Tools menu

Finally, you have to choose the right programmer:

Tools – > Programmer – > “Arduino as ISP”

(not: ArduinoISP!). Then you made it and you are ready to upload the sketch. 

Note: if you change the clock speed and also – to be on the safe side – before the first upload, you need to burn the bootloader. Try it out: upload the sample sketch with the selection of 16 MHz and then another time with 1 MHz – if the bootloader is still the same, the flashing frequency at 16 MHz is sixteen times slower. 

Little helpers

If you work a lot with the ATtinys, then I recommend investing in a breadboard adapter (in the next picture on the left) as it makes working much clearer. The adapter is particularly useful in conjunction with the USBtinyISP Programmer, which we will come to in a time. You can also use the adapter with a UNO if you build a small shield. The breadboard adapter can be bought here or a version with 2 x 3 pin header here. 

Adapter Shield for programming ATtiny85 & Co
Self-build adapter shield with breadboard adapter for ATtiny85 & Co
Adapter shield - bottom
Adapter shield – bottom

Uploading with USBtinyISP

The beauty of Arduino as a programmer is that you have it anyway. However, working with a USBtinyISP Programmer is more convenient, as you don’t have to take the detour via the ArduinoISP Sketch. In addition, you can directly plug in a breadboard adapter. The programmer is available for < 10 Euro e.g. on Amazon

USBtinyISP programmer
USBtinyISP programmer

Preparation

Even when using the programmer, the steps described in “Preparing the Arduino IDE” must of course be completed. Then you need a driver for the USBtinyISP. You can download the driver installer here. Start it, tick “Drink / Pro Trinket / Gemma (USBtinyISP)” and click on “Install”. 

Adafruit USBTtnyISP driver installation

Wiring

The circuit is basically the same as above, you only use the programmer instead of the Arduino:

Circuit for programming the ATtiny85 with USBtinyISP programmer
Circuit for the ATtiny85 / 45 / 25 with USBtinyISP programmer

Upload

In the Arduino IDE, you choose “USBtinyISP” as programmer.  You will probably notice that “Port” is not available as a selection, even though you have connected the programmer. Don’t worry, that’s the way it should be. Then you select the ATtiny type and clock speed. Upload the sketch. For older versions of the Arduino IDE you have to select “Upload with Programmer” in the menu item “Sketch” or alternatively click on the upload arrow while pressing the Shift key. In my Arduino IDE version (1.8.9) it is possible without these measures. 

If it doesn’t work

A notorious error message is “Could not find USBtiny device (0x1781/0xc9f)”:

Error message

If you receive this message even though the USBtinyISP is connected, it’s likely that you have a driver issue. I already had that. If you are using Windows, go to Device Manager and search for USBtinyISP. Check the driver details in Properties. This should look something like this (sorry, German version of Windows):: 

USBtinyISP in the device manager

With me, Windows had selected another driver that I had on my computer. The solution: Go to “Update Driver” – > “From computer” – > “Choose from list…..”. If there are several drivers listed, just try it out. 

Uploading with Atmel Studio

For the sake of completeness, I would like to list this variant. However, the subject is so extensive that it would go beyond the scope. I will therefore only briefly outline it here and provide a step-by-step instruction in a separate post. 

Atmel Studio (currently version 7) is a free IDE that can be downloaded here. It includes everything you need on the software side to program Atmel microcontrollers. 

Various programmers are available for Atmel Studio, e.g. the AVR Dragon or Atmel-ICE:

Programmer for Atmel Studio, left: Dragon, right: Atmel-ICE
Programmer for Atmel Studio, left: Dragon, right: Atmel-ICE

Usually programs in Atmel Studio are written in “C” which is much closer to the hardware than Arduino sketches. The blink sketch looks like this in “C”: 

#include <avr/io.h>
#include <util/delay.h>


int main(void)
{
    DDRB |= (1<<PB4);
    
  while (1) 
  {
    PORTB = (1<<PB4);
    _delay_ms(500);
    PORTB &= ~(1<<PB4);
    _delay_ms(500);
    }
}

 

This may seem a little daunting, but it has its advantages, e.g. in terms of speed. In addition, you can see better what is really happening at the level of ports and registers.

Other advantages of working with Atmel Studio and the above-mentioned programmers include:

  • Access to the Fuse Bits
  • Debugging: step-by-step program execution and variable tracking
  • Better structuring of large projects

The downside: it takes some time to get used to it and the programmers are relatively expensive. Still, fancy more? Then stay tuned and wait for the post about Atmel Studio.

Leave a Reply

Your email address will not be published. Required fields are marked *