Tuesday, May 28, 2013

Digispark and programming a raw ATtiny85 with Arduino

I was a backer of the Digispark last year, and love these little devices.  For those who haven't seen them, the Digispark is a tiny ~ 1 inch square board with an Atmel ATTiny85 microcontroller, male USB pins, and custom bootloader to allow it to be programmed directly from a computer USB port.

More succinctly- It's the smallest, cheapest, and easiest way to build a simple Arduino project that doesn't require the larger Atmel microcontrollers.

After playing with my initial three Digisparks for a while, I started coming up with uses for the ATtiny85 that made even the Digispark overkill.  For these simple circuits I didn't want (or need) the bootloader or the USB IO.  I just wanted a way to flash my code onto the raw ATtiny85 and move on.   Enter littlewire - a simple Atmel AVR programmer based on the ATtiny85 and it's ability to implement USB in firmware.

Littlewire uses the same IO pins as the Digispark, so you can actually just flash the existing littlewire firmware onto a digispark, and then immediately start using it as an AVR programmer to program raw Atmel chips!

Below (for my own recollection, honestly) are the steps I took to set this up and my notes about programming raw ATtiny chips with Arduino.  Note: I'm doing this on a 64-bit Linux system.  The steps should be similar for another OS, but YMMV.

Step 1.  Set up & test the Digispark Arduino environment

I just used the pre-built Arduino package with the digispark binaries from digistump.com.   The important part here is getting working micronucleus and avrdude binaries that can talk to the custom digispark bootloader.

Step 2.  Push the littlewire firmware to the Digispark

After you have confirmed that you can program a digispark, you'll need to re-flash it with the littlewire firmware.  The only file you really need is main.hex from the 'firmware' directory, currently located at  https://github.com/kehribar/Little-Wire/tree/master/v1.1/firmware

The 'micronucleus' binary you got (or installed) with the Digispark Arduino package is the magic command to push a pre-compiled .hex file to a digispark.  So, from a command prompt, simply run:
micronucleus main.hex
and then plug in the digispark you wish to re-flash.  (Substitute the full path to main.hex if necessary.)  NOTE: My laptop has some minor timing issues talking to the digispark, so it actually took three or four attempts to make micronucleus successfully upload the firmware.  

Once this is successful, the Digispark will identify itself on the USB bus as a USBtiny after the initial Digispark bootloader 5 seconds is up.

Step 3.  Install Arduino support for the ATtiny* family of AVRs

At the ATtiny AVR support to Arduino.  I used the library available here: https://code.google.com/p/arduino-tiny/downloads/detail?name=arduino-tiny-0100-0016.zip
  •  create a hardware directory called 'attiny'
  •  extract the attiny support library into the attiny/ directory
  •  edit / create "boards.txt" under that directory with the entries from "Prospective Boards.txt"
Note:  The MIT High Low Tech blog guys apparently have their own Arduino ATtiny library as well, which should also work.  I haven't compared them.  [Anyone used both?  Let me know the difference!]

You should be all done!  Verify you can complete the steps below:

Programming AVRs with your new programmer

Connect the Digispark-Littlewire to a chip to be programmed:
DigiSpark => ATtiny85
    p0    => pin 5: PB 0 (MOSI)
    p1    => pin 6: PB 1 (MISO)
    p2    => pin 7: PB 2 (SCK)
    p5    => pin 1: PB 5 (RESET)
Naturally, you'll also have to supply +5V (pin 0) and GND (pin 4) to the target chip as well.  You can safely leech these from the 5V and GND header pins of the Digispark to use the USB power.

From within the Arduino interface:
  • Select Tools -> Programmer -> USBtinyISP 
  • Select Tools -> Boards -> ATtiny85 (xxx)  for the appropriate settings you want.
NOTE:  If you select a new clock speed, you'll have to change the fuse bits on the processor to set them.  The Arduino "Burn Bootloader" command will do this for you if you used the library I did.  Otherwise, set the fuse bits yourself using avrdude from the command line. 
  • Upload!  You're done!
One big advantage to using an AVR programmer in this configuration is that you can leave the AtTiny85 plugged in and re-program it at will, since the AVR programmer uses the pin 1 (RESET) pin to reset and reprogram the ATtiny85.

Conversely, the drawback is that you lose the use of pin 1 as an I/O pin (D5) unless you want to set the fuse bit to disable RESET - at which point you can no longer program it using the AVR programmer.  

You can get DIP package ATtiny85 microcontrollers for cheap from all sorts of places.

Have fun!



1 comment:

  1. I want to use the attiny85 reset pin as I/O
    What are the fuse settings ,I am using avrdude from terminal.
    I have right now used
    e1
    dd
    fe

    ReplyDelete