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:

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:

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 off digitalWrite(1, LOW); // set the LED on 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.

If you have any suggestions with regards to our next article, leave a comment below and we’ll look into it. Furthermore, don’t be shy in pointing out errors or places that could use improvement. Why not follow us on twitter, Google+ and facebook to keep up with new articles, news and other items of interest. Article by John Boxall for Little Bird Electronics. 

Otherwise, have fun, stay safe, be good to each other – and make something!