Attiny45 delay not working

I was trying to make a stopwatch using an attiny45, the problem is that the delay(1000) is not a second, so the function doest add one second every one second, i have compared the time with my phone and they are not the same, my phone seems to be a 2 or 3 second faster
stopwatch.ino (829 Bytes)
Im using the 1mhz internal clock and i previously burn the bootloader, i also ran this code on an arduino nano and it works perfectly fine

Well the clock-speed is not very accurate (but can be tuned) have you tried the 8MHz internal ? Using an external crystal you will get a much higher accuracy.

there is a possibility of a mismatch in the clock configuration. How are you programming the ATtiny85. are you using Arduino as an ISP? What is the clock frequency set?

i would use commands that execute quicker.
not using global variable, static instead.
uint8_t instead of int.
port manipulation instead of digitalRead().
and definitive no delay().

When you burned the bootloader, was the clock speed set the same as what you are compiling with now?

@IsaelGM, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project.

I'm programming the attiny45 using an arduino nano, arduino as ISP, the frequency is 1mhz

Yes it was 1mhz when i burned the bootloader and when i upload the sketch

#define F_CPU 1000000UL
#include <util/delay.h>

and use
_delay_ms(1000);

Does the Attiny45 have millis?

Have you tried running the code on a Nano with a 1MHz internal oscillator to see if the display library is particularly slow?

I just bought a 16mhz oscillator, and it worked perfectly, but i had a push button on the pin 3 where the oscillator is connected, and it doesn't work now, is that because of the externaloscillator?

Well yes of course. The oscillator uses 2 pins and can not share them.

And don't forget about the 2 tiny capacitors. (and that for 16MHz the minimum voltage is 4.5v)

Do you still have enough pins left ?
You can use Pin 1 (reset / PB5 / A0) as an input pin, as long as you make sure the voltage applied does not drop below 0.5 * VCC. Easiest is to read the voltage using analogRead() and use a few resistors
GND -> 10K -> Switch -> A0 -> 4.7K -> 5v+

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.