Electronic – attiny85 clock precision

attinyattiny85timer

I am using an attiny85 coupled to a mosfet and a water valve to water my plants at a regular interval. I want to water it every day. I programmed it to turn on every 24h, and today it is not on time. I expected a drift, but watering can occur 4 hours earlier or later, no problem. When should I expect there to be a problem in my setup? In other words, given that I use the internal clock of the attiny85 (which I run at 1MHz), what is the time bracket I can expect the water to turn on if I target 24h of delay between two waterings? Also, is it always going to be biased in the same way, i.e. is the bias systematic or random?

Best Answer

As always, start by reading the datasheet:

6.2.3 Calibrated Internal Oscillator

By default, the Internal RC Oscillator provides an approximate 8.0 MHz clock. Though voltage and temperature dependent, this clock can be very accurately calibrated by the user. See “Calibrated Internal RC Oscillator Accuracy” on page 164 and “Internal Oscillator Speed” on page 192 for more details.

...

During reset, hardware loads the pre-programmed calibration value into the OSCCAL Register and thereby automatically calibrates the RC Oscillator. The accuracy of this calibration is shown as Factory calibration in Table 21-2 on page 164.

The referenced table:

------------------------------------------------------
| Method  | Frequency | Vcc      | Temp   | Accuracy |
------------------------------------------------------
| Factory | 8.0MHz    | 3V       | 25°C   | ±10%     |
------------------------------------------------------
| User    | 6-8MHz    | 1.8V-5.5 | -40-85 | ±1%      |
------------------------------------------------------

So the factory calibration may be off by as much as 10%, at 3V and 25°C. If your voltage and/or temperature is something else, there's no specified accuracy at all.

You can calibrate the RC oscillator in your specific attiny to be 1% accurate at a fixed voltage and temperature (details, again, are in the datasheet).

But honestly, if you need any sort of temporal accuracy, the easiest way is to just use a crystal as a clock source, assuming you can spare the I/O pins. The internal oscillator is nice for when you don't need the accuracy, or when you have large volumes and need the absolute lowest BOM cost.

If you want to go the RC oscillator route, see Atmel RC oscillator calibration app note for more details. (Suggested in the comments by bigjosh - Thanks! Didn't know that one yet.)