ATtiny85 - Speed and "Strength"

Hi Guys

I have a couple of questions regarding the use of ATtiny85s.

First is a clock frequency related question. I have a sketch that was written for an ATmega328 running at 16MHz. I now want to use the same sketch using an ATtiny85 running at 8MHz. Now, the original sketch uses both Delay and Tone functions so what I'd like to ask is simply, will both functions produce similar results on the ATtiny85 as they did on the ATmega328? IE will a Delay (50) be 50ms in both cases and will a tone be the same pitch in both cases or will the differing clock frequencies mean I will have to adust the values assigned to the functions?

I'd attempt to experiment myself to compare results, but I'm using a SOIC ATtiny85 and whilst I have a set up that enables me to program it, I don't have a set up that will allow me to run the chip without soldering it into place on a pcb. Also I'm setting the fuse to disable the RESET as I'm using the pin for output, which leads me to question 2.

I'm using pin PB5 (RESET) as an output pin. The data sheet states that it is a weak I/O pin, but what does that actually mean? I want to use it to drive a 4 ohm speaker via a 2N2222 so it only needs to supply around 15mA. Will the pin be "strong" enough?

TIA

Hi Guy,

STDummy: Now, the original sketch uses both Delay and Tone functions so what I'd like to ask is simply, will both functions produce similar results on the ATtiny85 as they did on the ATmega328?

Assuming the fuses are set correctly, yes.

IE will a Delay (50) be 50ms in both cases

Yes.

and will a tone be the same pitch in both cases

Yes. (Within a small percent.)

or will the differing clock frequencies mean I will have to adust the values assigned to the functions?

No.

Also I'm setting the fuse to disable the RESET as I'm using the pin for output, which leads me to question 2.

Unless you have a high-voltage programmer you get one chance to program the target.

I'm using pin PB5 (RESET) as an output pin. The data sheet states that it is a weak I/O pin, but what does that actually mean?

Less current.

I want to use it to drive a 4 ohm speaker via a 2N2222 so it only needs to supply around 15mA. Will the pin be "strong" enough?

I think so not. Let's check the datasheet...

Figure 22-23, Figure 22-24, Figure 22-25, and Figure 22-26 have the relevant graphs.

Will the pin be “strong” enough?

I’m going to go with “no”. There is a 1.5 V drop at 0.5 mA and the graph doesn’t go past 2 mA.

“Very weak” is a better description.

STDummy: will the differing clock frequencies mean I will have to adust the values assigned to the functions?

No. The library uses different values based on the board you select in the Arduino IDE's menu.

STDummy: I'm using pin PB5 (RESET) as an output pin. The data sheet states that it is a weak I/O pin, but what does that actually mean? I want to use it to drive a 4 ohm speaker via a 2N2222 so it only needs to supply around 15mA. Will the pin be "strong" enough?

No. The RESET pin can only provide 1 or 2 mA (see section "Pin driver Strength" in the datasheet).

Maybe you could use a different pin instead (eg. one with a timer output so that tone() is done by hardware - PB4 is a good choice for this).

The RESET pin can be used as analog/digital input without losing the normal reset/programming function. Just make sure you don't go below 0.5Vcc and use analogRead() to read the state.

eg. To connect a switch to the pin all you do is set up a resistor divider and connect the RESET pin to the center. Use the switch to short out the upper resistor, the voltage will go from 0.5 to 1.0 when you press it...

Thanks Fungus

I was thinking of reassigning PB5 as a switch pin actually.

I'm not too concerned about setting the fuse and then not being able to reprogram as the final destination of the ATtiny is to be permanently installed on a board that has no programming pins anyway. I'm afraid I'm not too informed on the use of voltage dividers (I know the formulae but can never get a network calculated that gives the values I need!), so I'll probably still set the fuse but now utilise the internal pull up resistor to use the pin as my pushbutton switch input (which was the role of the pin which will now have to control the speaker).

STDummy: I'm afraid I'm not too informed on the use of voltage dividers (I know the formulae but can never get a network calculated that gives the values I need!)

Two 2k resistors should work. Maybe higher values if power consumption is critical.

I can confirm that it does work. I used two 1.4k ohm resistors that I had laying around and compared analogRead with 600.

/*
  Button

 Turns on and off a light emitting diode(LED) connected to ATtiny's
 PB0, when pressing a pushbutton attached to PB5 (analog 0).

 */

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 0;    // ATtiny85 PB5 = A0, the number of the pushbutton pin
const int ledPin =  0;      // ATtiny85 PB0, the number of the LED pin

// variables will change:
int buttonReading = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonReading = analogRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonReading > 600) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  }
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}

STDummy: First is a clock frequency related question. I have a sketch that was written for an ATmega328 running at 16MHz. I now want to use the same sketch using an ATtiny85 running at 8MHz. Now, the original sketch uses both Delay and Tone functions so what I'd like to ask is simply, will both functions produce similar results on the ATtiny85 as they did on the ATmega328? IE will a Delay (50) be 50ms in both cases and will a tone be the same pitch in both cases or will the differing clock frequencies mean I will have to adust the values assigned to the functions?

Assuming you programmed the fuses correctly, and have the entry in boards.txt set correctly, this will work fine. Boards.txt contains an entry for the clock frequency, this is used by the compiler to get delays and such set correctly.

The most common mistakes leave you with things going at 1/8th speed (ckdiv8 fuse left on), or 1/2 or 2/5ths (from selected board set to be 16 or 20mhz when chip is actually running at 8).