Sunday 24 August 2014

Teensy 3.1 ISP - program an ATtiny85 with your Teensy!

A couple of weeks ago I had this brilliant idea of building an intervalometer for my canon 450D. As I already had my Teensy 3.1 at that stage, it was an obvious choice for me. I implemented a *very* simple Arduino code, uploaded it to my Teensy, hooked up some wires together and had a blast shooting some time lapse videos (see here for example).

However, I quickly realised that I want my intervalometer to be smaller, more portable and battery powered (no USB cable required). I started googling and discovered that people use their Arduinos as In-System Programmers (ISP, see here) to program smaller AVR chips like ATtiny85. What a brilliant idea! I quickly ordered some ATtinys and started building. Here are my results - who knows, maybe somebody will find them useful?

  1. First of all, you'll need to have the Teensy 3.1 configured and working with the Arduino IDE. To make sure everything is working as it should, please follow PJRC's tutorial. If you have already set up your Teensy 3.1, you can obviously skip this step.
  2. Open the Arduino IDE (I'm using version 1.0.5-r2). You should already see the ArduinoISP sketch in the Examples section (File -> Examples -> ArduinoISP).
    Select it and take a quick look at the source code. You should see a section where RESET, LED_HB, LED_ERR, etc. macros are defined. Since RESET's original value is SS, let's change it to 10. Additionally, make sure that the function start_pmode() calls spi_init() before anything else. Save the sketch and upload it to your Teensy 3.1.
  3. Now, let's wire the programmer's LEDs up. They will give you an indication of what your ISP is currently doing. And if it's not doing anything, you can always rely on the heartbeat LED. There are 3 LEDs I'm using:
    • Heartbeat (YELLOW, LED_HB, pin 9)
    • Error (RED, LED_ERR, pin 8)
    • Write (GREEN, LED_PMODE, pin 7)
  4. Now, the next step is to install the ATtiny85 definition in your Arduino IDE. David A. Mellis was kind enough to prepare ATtiny support libraries for the Arduino IDE and share it on GitHub (you can get the zip directly here).

    Once you download the zip, extract it and you should see a folder called attiny inside the package (attiny-master -> attiny). Copy the attiny folder to your Arduino's sketchbook folder's hardware subfolder (it should go like this: Arduino -> hardware -> attiny). Start or restart your Arduino IDE. You should see a list of newly added ATtiny definitions in the Board menu (Tools -> Board -> ATtiny...)

  5. Make sure your Teensy acts as a programmer from now on by choosing appropriate Programmer option (Tools -> Programmer -> Arduino as ISP):

    Select the basic Blink example and choose your board from the new list. I'm working with ATtiny85, so my choice is ATtiny85 (internal 1MHz clock) (I don't want it to consume too much energy, as it's going to be battery powered).

    The last thing before connecting the ATtiny is to make sure we are using correct pin number to drive the LED. ATtiny doesn't have pin 13 (otherwise it wouldn't be so tiny!), so let's change the value of the led variable to 0 (which, funnily enough, is ATtiny's pin number 5).

  6. Now, let's connect the ATtiny. Disconnect the Teensy and stick the ATtiny on a breadboard. Look closely at the top surface of the chip, you should see a small circular shape indicating where the RESET pin is. It will look like this:

    Connect the pins as follows:

    ATtiny85 Teensy 3.1
    RESET (1) 10
    VCC (8) 3.3V
    GND (4) GND
    7 13
    6 12
    5 11

    using the ATtiny's pinout sheet as a reference:

    Your connections should look like this:

  7. Now you are ready to program your ATtiny85. Connect your Teensy 3.1 again and, having the Blink example in front of you, simply upload it. If you connected everything correctly, avrdude should upload the Blink code with no problems reporting something like this:

    ...
    ...
    ...
    avrdude: verifying ...
    avrdude: 836 bytes of flash verified
    avrdude: Send: Q [51] [20]
    avrdude: Recv: . [14]
    avrdude: Recv: . [10]

    avrdude done. Thank you.

  8. Now your ATtiny85 is programmed and you can test it:
I had a couple of problems in the process, but it was mostly due to wrong connections. If avrdude complains about invalid ATtiny's signature (whether it's 0x000000 or 0xFFFFFF), double check the wires and try to connect everything again. I hope this is useful and I encourage you to let me know if it worked in for you.

Thanks and happy hacking!