Attiny84 Tutorial
Attiny84 Tutorial
While making an application for the ATtiny84, I discovered strange problems. It turns out that using an ATtiny as an Arduino is not totally straight forward. At hlt.media.mit.edu they have a nice tutorial and software for using the ATtiny84 and ATtiny85 as Arduinos, but there are some problems with the tutorial (at least for the ATtiny84). In the end the problems were simple to solve and also reasonable, but they help us better understand Arduino and also allow us to close the gap between Arduino and AVR-GCC a bit further. Note: Im using Arduino 1.0. The Arduino tiny libraries come from arduinotiny.googlecode.com (that after all caused the problems, but read further to nd out how I resolved them).
Image 1. The pinout according to hlt.media.mit.edu. The very complete tutorial from hlt.media.mit.edu gives us the pinout like in image 1. Unfortunately when uploading, the ATtiny didnt work as expected... Well, what is your problem then?!
Pinning the pins - Resolving ATtiny Arduino issues. / J.A. Korten / CC Share Alike !
/* TinyBlink Turns on an LED on for one second, then off for one second, repeatedly. Modied by J.A. Korten to let two LEDs alternate. */ void setup() { // initialize the digital pins as outputs. pinMode(4, OUTPUT); pinMode(5, OUTPUT); } void loop() { digitalWrite(4, HIGH); // set the LED on digitalWrite(5, LOW); // set the LED off delay(1000); // wait for a second digitalWrite(4, LOW); // set the LED off digitalWrite(5, HIGH); // set the LED on delay(1000); // wait for a second }
Sketch 1. Our not so well working blinker When putting this sketch on the attiny84 it turns out that (physical) pins 7 and 8 alternate! So how do we nd out what exactly the problem is... First lets see the original Atmel datasheet.
Image 2. The pinout from the attiny24/44/84 datasheet So it turns out PA5 and PA6 become high and low (and vice versa) instead. Now lets see how they are declared in core_pins.h #dene PIN_A0 #dene PIN_A1 #dene PIN_A2 #dene PIN_A3 #dene PIN_A4 (10) ( 9) ( 8) ( 7) ( 6)
2
Pinning the pins - Resolving ATtiny Arduino issues. / J.A. Korten / CC Share Alike !
#dene PIN_A5 ( 5) #dene PIN_A6 ( 4) #dene PIN_A7 ( 3) #dene PIN_B0 ( 0) #dene PIN_B1 ( 1) #dene PIN_B2 ( 2) #dene PIN_B3 (11) /* RESET */ This seems to give some important clue: in Arduino pin 4 and 5 are respectively associated with pins PA6 and PA5! We in fact wanted this: #dene PIN_A0 (10) #dene PIN_A1 ( 9) #dene PIN_A2 ( 8) #dene PIN_A3 ( 7) #dene PIN_A4 ( 6) #dene PIN_A5 ( 5) #dene PIN_A6 ( 4) #dene PIN_A7 ( 3) #dene PIN_B0 ( 0) #dene PIN_B1 ( 1) #dene PIN_B2 ( 2) #dene PIN_B3 (11) /* RESET */ If we want to alternate between PA4 and PA5 we then must change our sketch to this: /* TinyBlink Turns on an LED on for one second, then off for one second, repeatedly. Modied by J.A. Korten to let two LEDs alternate. */ void setup() { // initialize the digital pins as outputs. pinMode(5, OUTPUT); pinMode(6, OUTPUT); } void loop() { digitalWrite(5, HIGH); // set the LED on digitalWrite(6, LOW); // set the LED off delay(1000); // wait for a second digitalWrite(5, LOW); // set the LED off digitalWrite(6, HIGH); // set the LED on delay(1000); // wait for a second }
Pinning the pins - Resolving ATtiny Arduino issues. / J.A. Korten / CC Share Alike !
Physical pin
2 3 [4] 5 6 7 8 9 10 11 12 13
An other library that is less Arduino compatible (I mean, the methods are not Arduino compatible so converting a sketch causes us more work) is the David A. Mellis library. It was modied by R. Wiersma (a fellow adult LEGO hobbyist or as we call it AFOL) for the ATtiny84 and A. Saporetti for the ATtiny45. It might however be that the performance of the Mellis based libraries is slightly better since they dene some functions using macros instead of doing it inline (the normal way). So in fact it turns out that different libraries use different pin mappings (see Source 1). My use of the arduino-tiny.googlecode.com library caused the pin mappings to be off from the examples used in the MIT tutorial. // ATMEL ATTINY84 / ARDUINO // // +-\/-+ // VCC 1| |14 GND // (D 10) PB0 2| |13 AREF (D 0) // (D 9) PB1 3| |12 PA1 (D 1) // PB3 4| |11 PA2 (D 2) // PWM INT0 (D 8) PB2 5| |10 PA3 (D 3) // PWM (D 7) PA7 6| |9 PA4 (D 4) // PWM (D 6) PA6 7| |8 PA5 (D 5) // +----+ Source 1. Some comments from the Mellis tiny14 pins_arduino.h le.
PWM
In Source 1 one can see that the Mellis-based library would in fact work with the examples from the MIT high-low tech tutorial. The guys and gals from MIT actually also state: To program them, well use a port of the Arduino core libraries created by Alessandro Saporetti and slightly modied by HLT. So that is the denite end of this mystery :) Lesson learned, always check your libraries when doing more complex stuff!
Pinning the pins - Resolving ATtiny Arduino issues. / J.A. Korten / CC Share Alike !
References Arduino ATtiny library: arduino-tiny.googlecode.com MIT High-Low Tech tutorial: hlt.media.mit.edu (Programming an ATtiny w/ Arduino 1.0). Note: this is a very educational site, also read other tutorials there! About us We are Arduino enthusiasts that use Arduino for all kinds of applications (mostly educational, from primary to university level. As Adult Fans Of LEGO (AFOLs), we also enable others (that are not so tech-able) to enjoy Arduino for LEGO applications. This project is called LEGuanO. At this moment we use the ATtiny84 for an automated level crossing. The potential of the ATtiny84 is fully used: a. two servos for the (optional) automated barriers b. connectors for red warning lights (alternating) c. white safe blink option d. either a LEGO PF (tm) compatible receiver e. or reed sensors for automated functioning of the crossing The LEGuanO has a footprint of 4 by 4 (LEGO) studs and can be easily connected to LEGO bricks. visit us at www.LEGuanO.nl
Pinning the pins - Resolving ATtiny Arduino issues. / J.A. Korten / CC Share Alike !