ATtiny13 - using delay() function

OK, ATtiny is not exactly the Arduino chip, but perhaps someone will be able to help.

I am trying to use delay() function on ATtiny13. As far as I can tell the chip has a default factory setup (checked fusebits with avrdude and they are reported as H: FF, L:6A). Correct me if I am wrong, but that means clock of 9.6 MHz and I am not touching the default clock prescaler, so the chip should run at 1.2 MHz. That's what I selected from the Board menu when compiling the code of the blink example, but it was running way slower than 1 sec ON, 1 sec OFF. I had problems measuring the frequency, so I changed the blink loop to be 10 times faster:

  digitalWrite(4, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(100);
  digitalWrite(4, LOW);    // turn the LED off by making the voltage LOW
  delay(100);

and got frequency of 3.66 Hz (instead of expected 5), for delays of 10 ms I got a consistent frequency of 36.5 Hz and for delays of 1 ms I got 348 Hz (which makes perfect sense, as time lost calling the function and closing the loop starts to play a role when delays are pretty short).

It looks like the loop time scales up quite nicely with the number of milliseconds passed as a parameter, but it is about 30% slower than expected.

Now, I understand the internal clock is not perfect and some differences in speed are to be expected, but 30% looks way off to me. Checked another, identical chip, it was marginally faster (less than 1%), but otherwise pretty consistent with the 30% number. Any ideas what is going on?

On one hand the internal oscillator is not too accurate. On the other hand the internal oscillator gives constant results for a given Voltage (Vcc) and temperature.
30% off is quite a lot. I have used ATtiny13 a lot and what I do is to "tune" the internal oscillator if better accuracy is needed (which is not always the case). By tuning/calibrating you can get about 2% accuracy if temperature and voltage are constant.
For tuning/calibrating you have to deal with register OSCCAL.
You can read and write to this register. Data can be 0...255.
You have to calibrate every individual ATtiny chip.
For further reading take a look here:
http://ernstc.dk/arduino/tinytuner.html

If you just want to play around with the register try something like this

void setup() {
   OSCCAL = 120;   // can be 0 ... 255
   ...
}

void loop() {
   // your blink code
}

And don't forget to use a decoupling cap of about 100nF close to Vcc and GND. :slight_smile:

Which core are you using? There are... issues with the implementation of delay() on clock speeds other than 1, 8 and 16mhz on some cores.

uxomm:
I have used ATtiny13 a lot and what I do is to “tune” the internal oscillator if better accuracy is needed (which is not always the case).

Thanks. I don’t need a high accuracy, but having a ms or us reported/used by the controller within just a few % of the real value will definitely make my life easier. From what you wrote I understand it requires tinkering - no problem with that, I just wanted to be sure I am not trying overcome a problem resulting from my lack of knowledge.

For tuning/calibrating you have to deal with register OSCCAL.
You can read and write to this register. Data can be 0…255.

Actually 0…127, as the oldest bit is always zero, but yes, I know about this route.

And don’t forget to use a decoupling cap of about 100nF close to Vcc and GND. :slight_smile:

I put them on the breadboard even before choosing microcontroller :wink:

DrAzzy:
Which core are you using? There are… issues with the implementation of delay() on clock speeds other than 1, 8 and 16mhz on some cores.

Didn’t know there are different cores on ATtiny13. How do I check?

Bzdurek:
Actually 0..127, as the oldest bit is always zero, but yes, I know about this route.

Your are right - sorry for that.
OSCCAL is 0...255 with ATmegas, ATtiny 25/45/85 (and others).
But it is 0...127 with ATtiny13.

I use this core: GitHub - MCUdude/MicroCore: A light-weight Arduino hardware package for ATtiny13

Ah, you mean core in terms of the software, not in terms of the hardware. Got it :slight_smile: