Saturday, 12 May 2012

Flashing an ATTiny85 from a Teensy.

Here's the project: flash an ATTiny chip from a laptop using a Teensy board.

I've been using Teensy boards for several things lately and they are very good. But for one project it is overkill. Teensy boards can blink lights just fine, but so can ATTinys, and ATTinys are smaller and cheaper.

But flashing a Teensy is very simple, you just plug a USB cable into it and use the usual Arduino IDE to load your code directly from a desktop or laptop. Not so with an ATTiny because it isn't as clever. I could mention boot loaders and similar but I'd get out of my depth fairly soon.

To flash an ATTiny you need to use the SPI protocol and you won't get your laptop talking that through a USB cable, so you need something to act as a go-between, taking the serial data being sent down the USB cable, turning it into SPI and pushing it through to the ATTiny. Obviously this is complicated. The go-between is called an ISP (In-System Programmer).

People use a custom box for this such as a USBTinyISP or they use an Arduino board as an ISP like this. But my projects tend to be very compact which is why I tend to go for Teeny boards, the smaller of which is about the size of the end of my thumb. So I have various Teensys lying around but no Arduinos. Fortunately someone already figured out that you can use a Teensy as an ISP just like an Arduino (there are a lot of similarities between Teensy and Arduino, including using the same IDE).

So we're all good, except the technique didn't actually work.

Here is what I did to get it working:
  1. Follow the steps in this link (same as the one above), except I used a Teensy++ 2.0 and an ATTiny85 chip. These probably don't make much difference, but the pins have different numbers.
  2. I'm using ArduinoIDE 1.0 like he is, and the Teensyduino software. He's using the 64 bit version and I am using 32 bit version, again should be no difference.
  3. I used a modified copy of the ArduinoISP software which I've copied to here. I got this from here and changed the pins to work with the Teensy++ 2.0. So this version has the pins for the Teensy++ 2.0 (this is probably the big difference, ie what made it actually work)
  4. The program for the ATTiny is here. This is just a modification of the blinky example for Arduino. Again, just the pins are different from the Arduino standard.
  5. I did edit the arduino-1.0/hardware/arduino/programmers.txt to arduinoisp.speed to 9600. I forget where that was recommended. My modified ArduinoISP code has a similar change.
This is what my breadboard looks like:

 That's the Teensy on the left, with the USB on the extreme left. In the centre is the ATTiny85 and over on the right is a blinking LED. As you can see there are no resistors or capacitors, just simple connections (the two exceptions are the resistors needed on the LED). Some sites will tell you that you need other bits but I didn't for this.

The wires here aren't that easy to see but I've got GND on the top rail and VCC on the second rail. Near the VCC in the upper left of the Teensy we have MISO, MISI and SCLK across to the corresponding pins in the ATTiny. SS is connected to RESET on the ATTiny.

There is a green LED on pin 3 of the ATTiny85 because my test program will flash the LED attached to that pin. The blue LED flashes when the Teensy is flashing the ATTiny. I ought to add a red LED to indicate an error on pin 8 of the Teensy but I didn't. In fact both of these 'LEDs' are actually just one RGB LED.

I made the heartbeat flash the on-board LED on the Teensy so that I can tell the ISP code on the Teensy is running okay.
It is worth noting the options I used to upload the ISP code from the ArduinoIDE
The board selected is Teensy++ 2.0 and the Programmer is AVRISP MKII, which is just normal for Teensy programming.

But for the ATTiny we need this:
In this case the board is the ATTiny. There are lots of ATTiny options but any ATTiny85 does for this program. The Programmer is Arduino as ISP (and the Arduino IDE does not care that it is a Teensy instead).

This allows you to do everything from the Arduino IDE, as long as you set the options correctly. But ArduinoIDE actually uses avrdude to transfer the files to the Teensy ISP. This is the command it ends up using:

/home/roger/kits/arduino-1.0/hardware/tools/avrdude -C/home/roger/kits/arduino-1.0/hardware/tools/avrdude.conf -v -v -v -v -pattiny85 -cstk500v1 -P/dev/ttyUSB000 -b9600 -Uflash:w:/tmp/build4483866087774593940.tmp/BlinkAttiny85.cpp.hex:i

Your arduino directory will be different to mine but that is easy to tidy up. The reason this is interesting is if you want to use the gcc compiler to build your code instead of ArduinoIDE. You can then invoke avrdude manually using a similar command to this and the hex file will transfer down.

For a handy reference to the pins of the ATTiny and others use this.
Post a Comment