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!

11 comments:

  1. The &*%^%& signature problem .. drove me mad! Untill ... ArduinoISP sketch has an odd failure it seems: init_spi() is called before setting up MISO/MOSI etc. Put it after setting up in/outputs and the signature problems are gone.

    ReplyDelete
  2. Thanks for your comment and for reminding me - I totally forgot I had changed something in the sketch as well. Will check if it had any effect and if it did, I'll update the tutorial.

    ReplyDelete
  3. @gartnl Not sure if I changed anything or not after all, but I'll put that information in my tutorial.

    ReplyDelete
  4. @piotr What I needed to do was the following:

    original code:
    void start_pmode() {
    spi_init();
    // following delays may not work on all targets...
    pinMode(RESET, OUTPUT);

    pinMode(MOSI, OUTPUT);
    spi_transaction(0xAC, 0x53, 0x00, 0x00);
    pmode = 1;
    }

    I changed it to:
    void start_pmode() {
    // following delays may not work on all targets...
    pinMode(RESET, OUTPUT);

    pinMode(MOSI, OUTPUT);
    spi_init();
    spi_transaction(0xAC, 0x53, 0x00, 0x00);
    pmode = 1;
    }

    Don't know if this is Teensy specific, but it did the trick. No more signature failures.

    ReplyDelete
    Replies
    1. Thanks for your comment. That's very interesting - I'm using the original version - like code which I changed from something like you have right now. Glad you got it sorted!

      Delete
  5. Ive used your method before and it has worked, but I've was trying it again today and while the code loaded fine to the teensy, I had an issue with the Tiny

    when i upload the code I get;
    avrdude.exe: ser_open(): can't open device "\\.\com10": The system cannot find the file specified.

    any idea about this?

    ReplyDelete
    Replies
    1. Are you using the same version of Arduino IDE? Haven't tried this code with the latest version.

      Delete
    2. Im using 1.0.5
      I just tried again tonight and still nothing.
      heart beat is present on the leds but nothing else

      Delete
    3. I've never seen such issue, interesting. A couple of ideas:
      - can you program your teensy at all?
      - can you pinpoint which line/command causes this?
      - do you have another teensy or arduino to test this on?
      - have you tried playing with avrdude from the command prompt level?

      Delete
  6. Thank you for such an excellent article. I was able to follow your tutorial to use my Teensy3.1 to program ATtiny13a chips. I had to go dig up the ATtiny13a board for the Arduino IDE, which was easy to find. Otherwise, much was the same. Thanks again, for giving me a head-start. Nothing like the satisfaction of seeing that LED blink for the first time!

    ReplyDelete