Friday 25 May 2012

Program an ATTiny45 with a Teensy 2.0

I have a Teensy 2.0 and some ATTiny45 chips from Atmel, which I want to program with a minimum of fuss and parts. For me this means using the Teensy as an "In System Programmer" or ISP, and there are plenty of other articles on how to do this. But I thought I'd file what I've got here for reference anyway.

It's very easy - no hardware required except a Teensy, the ATTiny, a breadboard and 4 wires, and for software I started with the Arduino 1.0 environment with the Teensyduino additions - just what you'd normally need to upload sketches to the Teensy. Here's what I did.

  1. Download and install the ATTiny cores for the Arduino IDE (for now I'm using the IDE, I'll update this when I switch to make and avrdude). There are a quite a few different cores developed by different people and they're not all equal, which is not obvious when you start this process. I picked the ones from the above repository.
  2. Open the Arduino IDE and plug in the Teensy 2.0. Select the "Arduino ISP" from the File/Examples menu and change the #define for LED_HB to 11. This will blink the orange light on the Teensy when the programmer is running. Then upload the sketch to the Teensy.
  3. On a breadboard, wire these pins together:
    • Teensy 2.0 pin B0 <-> ATTiny45 pin 1
    • Teensy 2.0 pin B1 <-> ATTiny45 pin 7
    • Teensy 2.0 pin B2 <-> ATTiny45 pin 5
    • Teensy 2.0 pin B3 <-> ATTiny45 pin 6
    • ... and connect 5V to ATTiny45 pin 8 and GND to Attiny45 pin 4
    Here are pinouts for the ATTiny45 and the Teensy.
  4. In the Arduino IDE, under the Tools menu change the "Programmer" to "Arduino as ISP" and the "Board" to "ATTiny45 @ 1Mhz". Then load your sketch for the ATTiny and upload as before. For testing I used the "Blinky" example and changed the pin to 4. It should upload with a couple of "Please define PAGEL and BS2 signals in the configuration file for part ATtiny45" warnings, which you can ignore
  5. If you'd prefer to program directly from avrdude, the command should look something like:
    avrdude -c avrisp -P /dev/tty.usbmodem12341 \
       -p attiny45 -U flash:w:main.hex:i
    
    where:
    • /dev/tty.usbmodem12341 is the USB port the Teensy is plugged into
    • attiny45 is the type of AVR chip you're programming
    • main.hex is the hex file you have compiled and built with avr-gcc and avr-objcopy

That's it, really, and it's all ground that's been covered before.

If you're going to do this a bit then it's worth getting it off the breadboard, so I rustled up a quick circuit in Eagle (which you can download). It has sockets for programming 8, 14 or 20 pin ATTiny chips, plus a couple of LEDs to indicate error (red) and communicating with the ATTiny (green), and you can see a photo at the top of this blog. You'll need the following parts:

The download contains the circuit as a PNG for etching (make sure you etch so the text is the right way around) plus the source for a slightly modified version of ArduinoISP which I've called "TeensyISP.ino" - this has the correct #defines for the pins on the Teensy. Then just plug the Teensy into the PCB connectors in the middle of the board, put your ATTiny chip in the appropriate socket and upload as described above. Easy.

Incidentally, with a cost of about $16 for the Teensy plus maybe $2 for the LEDs and sockets, this is also probably the cheapest way to build an ISP programmer.

Notes

  • The first Blinky sketch I uploaded I was expecting to see the light blink on for a second, then off for a second. What I actually saw was on for a second, off for a fraction of a second. I haven't quite figured this one out yet but if I reverse the order in Blinky to "led off" then "led on" it works as expected. Still pondering this.
  • The LEDs on the circuit above don't normally come on during programming, at least not for me so far
  • I've double checked but haven't actually tested the 14 or 20 pin sockets in this circuit

No comments:

Post a Comment