Saturday, July 26, 2014

Use an Arduino Mega 2560 to program an ATTINY84A

This is a hello world AVR programming project. A simple way to get started programming an inexpensive AVR chip for anyone who has an Arduino board. I am going to use an Arduino Mega 2560 as an ISP AVR programmer but you can also do this with an Arduino UNO or any board with a SPI interface if you know how. 

If you have been working with some developer boards like the Arduino UNO or the Arduino Mega 2560, and you are comfortable soldering your own projects, then you might have realized that the chips alone usually cost from $1-$3 and take up a lot less space than the breakout boards do.  The good news is, you can easily get started programming these chips using the developer board that you have on hand! 


For this example, I am going to use my Arduino Mega 2560 as an ISP AVR programmer to program an  ATTINY84A-PU chip to do a simple LED blink. There are other ways to program this chip, but if you happen to already have an Arduino board on hand and don't want to spend the extra money to buy a programmer, this is a quick and easy way to get started.  


Parts List: 

  1. 10mm Ultra-High Brightness Blue LED - any LED will work
  2. An Arduino Mega 2560  or UNO 
  3. A 10uF capacitor
  4. An ATTINY84A-PU  ($1.41)
  5. Small Breadboard
  6. Connection Wires

The first thing you need to do is be able to program your chip using the Arduino Mega. To do that with the Arduino IDE you will need to install the Tiny bootloader and supporting libraries into the Arduino IDE. Then you will need to set up the circuit for the programmer, upload a blink sketch to the chip, and finally, setup the chip circuit to work with the blink circuit.

Using the Arduino Mega as an AVR programmer


Step 1: Prepare the Arduino IDE environment to program the ATTINY84A 

You need to be able to compile code into a hex file to load onto the ATTINY84A and upload that code from the Arduino IDE but the functionality to do that requires some additional software components that are not included by default.


  1. Click here to download the "Tiny" archive 
  2. Create a new directory named tiny under your ..\Arduino\hardware directory.
    For newer versions of the Arduino IDE, this may be located in:
         "C:\Program Files (x86)\Arduino\hardware"
  3. Copy and paste the contents of the tiny/avr directory of the archive to the ..\Arduino\hardware\tiny directory you created.
  4. In the  ..\Arduino\hardware\tiny directory, rename the Prospective Boards.txt file to boards.txt
  5. Open the Arduino IDE. You should see the ATTINY84 under the Tools->Boards menu.


Step 2: Program the Arduino Mega 2560 to be an AVR ISP

To turn the Arduino Mega into an AVR ISP you need to upload the  ArduinoISP sketch to your Arduino board.
  1. Using the USB cable, connect your Arduino Mega 2560 or Arduino Uno to your computer.
  2. Open the the Arduino IDE. 
  3. Under Tools -> Boards select the "Arduino Mega 2560" (or select "Arduino Uno" if you are using that instead) as the board.
  4. Under Tools -> Programmer select "AVR ISP"
  5. Select File -> Examples -> ArduinoISP to open the Arduino ISP sketch. 
  6. Compile the sketch and upload it to your Arduino Board. 
   Your Arduino is now ready to use as an in system programmer!


 Step 3: Hardware Setup to use the Arduino ISP (for the ATtiny84)


Next we need to make the connections between the Arduino Mega (or Uno) and your ATTINY84A chip.
The Arduino Mega has a hardware SPI interface on pins 50 - 53 that can be used to program other chips like the ATTINY84A as shown in the picture below. (If you are using the Uno, pins 50, 51, 52, and 53 on the Mega should be mapped to pins 12, 11, 13, and 10 on the Uno). 

Arduino Mega 2560 Hardware SPI Pins
(note: we also use pin 53 - the SS pin to reset to ATtiny84)

We need to connect the SPI pins from our Arduino to the SPI pins on the ATTINY84A. I am using the ATTINY84A-PU because the PU extension chips are breadboard friendly. The pins needed for this on the ATTINY84A are shown below.

ATTINY84A SPI / ISP Pins
(note: the RESET Pin 4 will also be needed)


Curcuit setup for the ISP AVR programmer


Unplug the Arduino Board and make the following connections:

Arduino Mega/ Uno Pins to  ATTINY84A Pins
MISO pin 50/ pin 12 ->pin 8
MOSI pin 51/ pin 11 ->pin 7
CLK pin 52/ pin 13 ->pin 9
SS pin 53/ pin 10 ->pin 4
5V ->pin 1 VCC
GND ->pin 14 GND
RESET to Capacitor to ->GND


Step 4: Use the AruinoAVR ISP to program the ATTINY84A


  1. Plug in the USB connection for Arduino board to your computer.
  2. Open the Arduino IDE.
  3. Open the Arduino Blink sketch by selecting
              File -> Examples -> Basics -> Blink.
  4. Change the led pin in the blink sketch by changing
                       "int led = 13;" to "int led = 1;"
  5. Select the ATtiny84 @ 8 MHz Internal Oscillator by selecting
              Tools -> Boards ->  ATtiny84 @ 8 MHz Internal Oscillator

    (Caution: Do not select the External oscillator unless you have added an external oscillator; this can cause issues if selected by mistake).
  6. Choose the Arduino as an ISP programmer by selecting
               Tools-> Programmer-> "Arduino as ISP"
  7. Upload the sketch to the chip by selecting
               File-> "Upload Using Programmer" or pressing Ctrl +Shift + U.

Step 5: ATTINY84A-PU LED Blink Hardware Setup

This is the easiest part.. connect the LED o Pin 3 of the ATtiny84, connect the power and ground pins and you will see the LED blink on and off slowly.

Make the connections:

   ATtiny84 Vcc Pin 1     -> 3.5V to 5V source voltage
   ATtiny84 GND Pin 14 -> Ground
   ATtiny84 Pin 3 /PB1     -> LED +
                            LED-  -> Ground


I used a Lipo battery I had to power it but you could also power it from the Arduino 5V, or another power source you might have.


Here is another useful link with more details for doing this with the UNO.
Using the Arduino Uno to program ATTINY84-20PU