Tutorials - Fab Academy at AS220 Labs - Providence, RI

Embedded Programming - Arduino IDE

Arduino IDE: 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.3). The ATtiny44A is a small, cheap microcontrollers that are convenient for running simple programs.

David Mellis wrote the original instructions: http://hlt.media.mit.edu/?p=1695 They have been revised, re-written, and extended here (with new screenshots) for use specifically with the Hello Button + LED board example.

Contents:

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.3. You can download Arduino 1.0.3 from the Arduino site.

Here are the Arduino installation instructions.

Download the ATtiny Board Files
Attiny zip
Install the FTDI Drivers
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.

Here is the link to the full datasheet.



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

Download and install the board files
  • Download the board files (so the Arduino IDE can talk to the ATtiny44A).
  • Unzip.
  • Locate your Arduino sketchbook folder:
    • In the Arduino software > "Arduino" menu > preferences
  • Navigate to your sketchbook folder.
  • Create a new sub-folder called "hardware" in the sketchbook folder.
  • Copy the attiny folder extracted from the zip archive to the hardware folder.
Restart
  • Restart the Arduino IDE.
  • You should see ATtiny entries in the Tools > Board menu.

 

Power and Connect the Boards

connecting

  • You'll need to provide power to the ATtiny and connect it to your programmer (FabISP).
  • That is, connecting MISO, MOSI, SCK, RESET, VCC, and GND of the programmer to the corresponding pins on the ATtiny.
  • Connect the programmer to the ISP header on the LED + Button board
  • You will also need to power the LED + Button board with an FTDI cable.

Configuring the ATtiny to run at 20 MHz

select board

  • By default, the ATtiny's run at 1 MHz (the setting used by the unmodified "ATtiny44)
  • You need to do an extra step to configure the microcontroller to run at 20 MHz – as we have an external 20 MHZ resonator on the Hello Button + LED board.
  • Select the "ATtiny44 (external 20 MHz clock)" from the Boards menu.

Select your Programmer

select board

  • 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 > USBtinyISP.

If You are Using Ubuntu 12.04 LTS

Mac and Windows users do not need to do this.

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

Otherwise you will get an error message like the one below:

avrdude:  error: usbtiny_transmit: error sending control message:  Operation not permitted
avrdude: initialization failed, rc=-1
Double check connections and try again, or use -F to override
this check.

avrdude: error: usbtiny_transit: error sending control message: Operation not permitted

To enable sudo access: 

Install uisp (missing in Ubuntu 12.04 LTS)
sudo apt-get install uisp
To enable sudo access: 

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

Type:
cd ~/Desktop
Then:
nano 10-usbtinyisp.rules
A blank file will open up in a text editor. 

Enter the following text.

SUBSYSTEM=="usb", SYSFS{idVendor}=="1781", SYSFS{idProduct}=="0c9f", GROUP="adm", MODE="0666"
SUBSYSTEM=="usb", ATTR{idVendor}=="1781″, ATTR{idProduct}=="0c9f", GROUP="adm", MODE="0666″
SUBSYSTEM=="usb", ATTR{idVendor}=="1781", ATTR{idProduct}=="0c9f", GROUP="plugdev", MODE="0666"
SUBSYSTEM=="usb", ATTR{product}=="usbtiny", ATTR{idProduct}=="0c9f", ATTRS{idVendor}=="1781", MODE="0660", GROUP="dialout"
  • Save the file by pressing: CONTOL + O.
  • Then Hit the ENTER key.
  • 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 service udev restart

Unplug all USB devices. Then plug them back in.

Now you should be able to burn the bootloader. 


Burn the Bootloader (All Operating Systems)

burned bootloader

  • Run the "Burn Bootloader" command from the Tools menu.
  • This configures the fuse bits of the microcontroller so it runs at 8 MHz.
  • Note that:
    • the fuse bits keep their value until you explicitly change them, so you'll only need to do this step once for each microcontroller.
    • this doesn't actually burn a bootloader onto the board; you'll still need to upload new programs using an external programmer.

burned bootloader

Modify the "Button" Example

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

Modify the Code
  • Open the Button sketch from the examples menu.
  • File > Examples > Digital > Button
  • Change the pin numbers from to correspond to the pins used for the LED and the button our your Hello Button + LED board.
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

Program the ATtiny

Power your boards

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.

Upload the sketch

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

Next - Build an Fabduino!

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