ATtiny Embedded Programming with the Arduino IDE



The assignment for this week is to read a microcontroller datasheet and to program your Hello Button + LED board from week 7 to do something, in as many different programming languages as possible.

Arduino: Program your Hello Button + LED Board

This tutorial shows you how to program an ATtiny44A microcontroller on your Hello Button + LED board that we made during the Electronics Design week using the Arduino software (version 1.0). The ATtiny44A is a small, cheap microcontrollers that are convenient for running simple programs.

This content is extracted from: http://hlt.media.mit.edu/?p=1695 and revised to work with the Hello Button + LED board example.

Materials and Tools

You need an in-system programmer (ISP) use the FabISP we made in class.

Fab ISP

Get the Software

Download the Arduino IDE Software
You'll need the Arduino software, version 1.0. You can download Arduino 1.0 from the Arduino site. Installation instructions are available for Windows and for Mac OS X.
Download the ATtiny Board Files
Arduino 1.0 and the ATtiny zip (from this GitHub repository)
Make Sure You Have the FTDI Drivers Installed
Get the drivers for your OS here: http://www.ftdichip.com/Drivers/VCP.htm


ATtiny 44A Microcontroller Pin-Outs

Note that the pinouts on the microcontroller are not the same numbers in the Arduino code. The microcontroller pin number is listed on the microcontroller body below. The corresponding Arduino pin is listed on the outside - to the right or left.



ATtiny 44A vs. Arduino Pin-Out Numbering

ATtiny 44A
Pin Number
Corresponding Arduino
Pin Number
Details
1 No number, no access? VCC (+)
2 Pin 10  
3 Pin 9  
4 No number, no access? Reset
5 Pin 8 PWM
6 Pin 7 PWM, Analog Input 7
7 Pin 6 MOSI, PWM, Analog Input 6
8 Pin 5 Analog Input 5, PWM, MISO
9 Pin 4 Analog Input 4, SCK
10 Pin 3 Analog Input 3
11 Pin 2 Analog Input 2
12 Pin 1 Analog Input 1
13 Pin 0 Analog Input 0, AREF
14 No number, no access? GND (-)


Installing ATtiny support in Arduino


Connecting the ATtiny

connecting

Configuring the ATtiny to run at 20 MHz

select board

If You are Using Linux (Ubuntu)

Avrdude needs sudo access in order to send your Arduino code to the board. You will need to run avrdude as sudo. Mac users do not need to do this.

To enable this:  We need to create a file called 10-usbtinyisp.rules in the directory /etc/udev/rules.d.

Here is how to do it (in terminal):

Type:

cd ~/Desktop
Then:
nano 10-usbtinyisp.rules
A blank file will open up in a text editor.  Enter the following text. Paste code is all in one line.
SUBSYSTEM=="usb", SYSFS{idVendor}=="1781", SYSFS{idProduct}=="0c9f", 
GROUP="adm", MODE="0666"

Save the file by pressing: <CONTOL> + O. Hit <ENTER>

Close the file by pressing: <CONTROL> + X

Move the file from the desktop to /etc/udev/rules.d by typing:

sudo mv 10-usbtinyisp.rules /etc/udev/rules.d    

Then type:

sudo restart udev
Then you should be able to burn the bootloader. 


Burn the Bootloader (Mac and Ubuntu)

burned bootloader

Programming the ATtiny

Next, we can use the Arduino IDE to upload a program to the ATtiny.

Modify the Code

Locate this section of code:

// constants won't change. They're used here to 
// set pin numbers: const int buttonPin = 2; // the number of the pushbutton pin const int ledPin = 13; // the number of the LED pin

Change it to match the pins on your Hello Button + LED Board:

// constants won't change. They're used here to 
// set pin numbers: const int buttonPin = 3; // the number of the pushbutton pin const int ledPin = 7; // the number of the LED pin

Select a Programmer

select programmer

Upload the Sketch

Make sure your board is plugged into the computer via the FTDI cable and that your FabISP is connected to the 6 pin programming header on the Hello Button + LED board.

To upload the sketch to your Hello Button + LED board - select the arrow button in the Arduino IDE (shown below)

upload program

You should see "Done uploading." in the Arduino software and no error messages.

Your LED should be glowing on the board. When you press the button, the LED should turn off.

Code Example

Here is a code example with lots of comments for explanation of what is happening in the code. Download the code example. (or just cut and paste the code below)

 /*
 LED Off Until Button Pressed 
 
 Blinks a light emitting diode(LED) connected to digital  
 pin 7, when pressing a pushbutton attached to pin 3. 
 
 
 The circuit:
 * LED attached from pin 7 to ground 
 * pushbutton attached to pin 3 from +5V
 * 10K resistor attached to pin 3 to +5V 
 * 10K resistor pulls pin 3 and the button to HIGH by default
 
 created 2005
 by DojoDave 
 modified 30 Aug 2011
 by Tom Igoe
 modified for Hello Button + LED Board - 19 Mar 2012
 by Anna Kaziunas France
 
 */

// constants won't change. 
// They're used here to set pin numbers:
const int buttonPin = 3;     // the number of the pushbutton pin
const int ledPin =  7;      // the number of the LED pin

// initialize variables:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);    
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     
}

void loop(){
  // read the state of the pin the pushbutton is connected to:
  buttonState = digitalRead(buttonPin);

  // is the push button pressed?
  // if not pressed - the button state is HIGH 
  // the pull up resistor the button / pin 3 makes the button state HIGH by default.
  if (buttonState == HIGH) {     
    // turn LED off (LED is off by default) 
    digitalWrite(ledPin, LOW); 
  } 
  //otherwise.....
  // button is pressed
  else {
    // turn LED on:
    digitalWrite(ledPin, HIGH); 
  }
}

Try Modifing the Code

Modify the code so that the LED blinks when you press the button. Hint - use the delay() function. See the "Blink" example for details.

The blink example comes with your Arduino IDE - It is under File > examples > basics > blink

Supported Arduino Commands / Limitations

Due to the memory limitations of this microcontroller, not all of the Arduino commands are supported.

The following Arduino commands are supported:

Next - Program Your Board in C to Echo Keyboard Input

For details on how to do this, see:
Program Your Board in C to Echo Back Keyboard Input (seperate page)

Next - Build an Arduino!

For details on how to build a fabbed Arduino with more memory and functionality, see:
Make an Arduino for Your Final Project (seperate webpage)