How do I set the clock speed fuses on an ATtiny85 when using an Arduino as a programmer?

I'm following this tutorial, programming the ATtiny85 with an Arduino, using it to play some tones (through a piezo speaker). I'm struggling with getting the tones at the right pitch (I'm creating the wave forms manually, as the tone() function is unsupported on the ATtiny85).

I believe the problem may be down to differing clock speeds on the Arduino and ATtiny. I understand the clock speed can be altered on the ATtiny, how do I accomplish this using the arduino environment?

-
by the way this is really cool, thanks for sharing! –  vicatcu Dec 14 '10 at 17:38
Hats off! I've never managed to program my attiny85 this way.. I always have "programmer out of sync" errors when avrdude attempts to write the flash. Swapping the arduino with a bus pirate works though... –  JonathanD Dec 14 '10 at 20:27
@Jon: I'm not sure of the root cause behind "programmer out of sync", but with AVR Studio you can adjust the programming frequency. There may be a similar option in avrdude, might be worth a look if you ever give it a whirl again –  Nick T Dec 16 '10 at 2:19

I believe the Arduino software (libraries and all) assumes you are operating at 16MHz. If you apply that assumption to the clock you are actually running at... things should work out. Assuming you are running the Tiny85 on it's internal oscillator I think it runs at 1MHz, so just multiply all your delay statements (and other notions of time) by 16.

If you need better accuracy than the internal oscillator provides you should think about using an external crystal or a resonator, but you will need to change the fuse settings of the AVR for that to work, and I think you will need a programmer like the AVRISP mkII to do that with AVR Studio (my recommendation).

I don't know much about the ArduinoISP sketch but to me it looks like it bit bangs the ISP protocol to upload a program to the target chip (not the on board Mega328), not sure it is equipped to manipulate fuses. ArduinoISP is documented here http://arduino.cc/en/Tutorial/ArduinoISP, fwiw. Note that you can't use a UNO currently to run the ArduinoISP sketch. It doesn't look to me like you can use the sketch to make the Arduino a viable interface for using the AVR Studio GUI tools.

-
Thanks for the heads up about the UNO, I'm actually using a Duemilanove with the 168 chip swapped for a 328, and can get the normal ATtiny sketches to upload properly via ArduinoISP. I'll be trying your /16 tip! –  fearoffours Dec 15 '10 at 9:02
@fearoffours You know another thing you could try is writing a sketch based on ArduinoISP that sets the fuses to a certain value. If ArduinoISP can write the Flash, there's no reason I can think of that you couldn't bit-bang the ISP protocol described in the AVR datasheets to set the Fuses. –  vicatcu Dec 16 '10 at 0:42
@fearoffours, also I just realized (and update post) to say multiply by 16 instead of divide by 16 (your running a slower clock it will take more ticks to get the same duration) –  vicatcu Dec 16 '10 at 0:45
I know it's over a year later, but I successfully use and UNO as AVR programmer. (However, I did alter the ArduinoISP sketch to delay(20) insted of delay(40) in its definition of void heartbeat().) –  JellicleCat May 30 '12 at 19:26

I'm using Arduino UNO + ArduinoISP successfully.

Add -U flags to your avrdude command to set any or all of the three ATtiny fuses.

avrdude -p attiny85 -P com8 -c stk500v1 -b 19200 -U lfuse:w:0x6f:m -U flash:w:main.hex


The clock selection is done in bits[3:0] on the third fuse ('Fuse Low Byte'). Set them as follows to make use of an external crystal (of 8MHz or faster):

-U lfuse:w:0x6f:m


Its definition (I infer) must be something like: [Fuse Low Byte]:[write]:[hex value]:[set manually]

The default value for the four high bits of this byte are 0110, so leave the 6 in 0x6f as it is, and only change the second digit, the f (its default value is 2).

NB: If your processes take longer or shorter than you expect, check your clock prescaler and your definition of F_ CPU.

-
I wanted to caution that I just bricked my AtTiny85 by using the above -U lfuse:w:0x6f:m avrdude option. I'll need to reset it using a high-voltage setup. So a word of caution to folks (like myself) who are trying to reset fuses without knowing what they are doing. –  M-V Jan 11 at 4:02

Try using the related tutorial by the same group (MIT's High-Low Tech) entitled Programming an ATtiny w/ Arduino 1.0.

A quick summary: From the Tools-> Board menu in the Arduino IDE, select the ATtiny85 and the frequency you wish to run at (1 or 8 Mhz internal clock, or 20Mhz external crystal) and then use the Tools -> Burn Bootloader". I believe selecting the desired speed board modifies the way the delay() and other time-related Arduino functions work in order to sync up with the clock speed.

I have had success with this approach myself using some ATtiny84 chips. The simple blink program is fixed, as well as more sensitive timing required for manually controlling pulses sent to a servo using delayMicroseconds().

-

It appears you are using ArduinoISP, try using AVR Studio or the command line options for avrdude. Depending on how the ArduinoISP was written, it may function just like an Atmel AVRISP mkII or similar.

-
I didn't think the Arduino was itself an ISP programmer - I thought it has an ISP header on it so that you can burn a new bootloader onto a fresh ATMega328 in circuit... –  vicatcu Dec 14 '10 at 17:33
reading the article more closely, it does seem like you can use the ArduinoISP sketch to do this - you learn something new every day! :) –  vicatcu Dec 14 '10 at 17:42

Default fuse settings for the ATtiny85 are: lfuse 0x62, hfuse 0xdf, efuse 0xff. This uses the internal RC (8 MHz) oscillator with "divide by 8" so clock is 1 MHz.

See http://www.engbedded.com/fusecalc for more fuse settings.

You can still use avrdude directly to change fuse settings of your ATtiny85 even when using an ArduinoISP as the programmer.

If you do end up manually changing fuse to alter the clock, be sure to edit the attiny85.build.f_cpu line of your [arduino_folder]\hardware\attiny45_85\boards.txt file.

-
+1 this is really useful, esp the clock speed being 1MHz, thanks –  fearoffours Oct 12 '11 at 10:08