Arduino Forum

Using Arduino => Microcontrollers => Topic started by: Smajdalf on Apr 27, 2017, 10:03 am

Title: Long term stability of internal RC oscillator
Post by: Smajdalf on Apr 27, 2017, 10:03 am
Hi,
has anyone experience with long term stability of internal RC oscillator and/or WDT oscillator of AVRs? If I calibrate such oscillator against precise time source (GPS, RTC) may I use it as reasonable source of time provided regulated voltage and near constant (indoor) temperature or will it drift too much? I mean +/- minute in a month or so.
Title: Re: Long term stability of internal RC oscillator
Post by: uxomm on Apr 27, 2017, 06:22 pm
My experience with internal RC oscillator:
By tuning you can get a reasonable accuracy of about +/- 1% for a given voltage and temperature. So the frequency may be "good enough" for UART if (and only if) voltage *and* temperature stay constant (UART serial requires timing to within +/-2% or so).

I mean +/- minute in a month or so.
So let's calculate how many minutes it may be off within one month:
60 * 24 * 30 = 43200 minutes per month
1% of 43200 = 432
So it may be +/- 432 minutes off within a month (that is about 7 hours).
Way too much...

But even with a crystal (50 ppm) you may not meet your needs.
I would recommend a real time clock: DS3231 (accuracy +/- some seconds per year).

Title: Re: Long term stability of internal RC oscillator
Post by: Smajdalf on Apr 27, 2017, 08:43 pm
You are speaking about short term accuracy. I imagine the frequency is fluctuating around "mean frequency". While the fluctuations may be quite large and interfere with precise measuring or asynchronous communication the "mean frequency" may be stable. For example if it takes 121,954 seconds for WDT set to 1s timeout to overflow 100,000 times it is reasonable to expect next 100k overflows will take very close to 121,954 seconds - much closer than +/- 1%. But I have no idea how close it will be. And how close it will be after a year?
Title: Re: Long term stability of internal RC oscillator
Post by: Coding Badly on Apr 28, 2017, 03:59 am
Quote
...much closer than +/- 1%...
For the internal oscillator I'm going with "maybe".

For the watchdog oscillator I'm going with "no".

However, "much closer" are weasel words (https://www.google.com/search?q=weasel+words) making it impossible to answer your question.

Title: Re: Long term stability of internal RC oscillator
Post by: Smajdalf on Apr 28, 2017, 06:28 am
For the internal oscillator I'm going with "maybe".

For the watchdog oscillator I'm going with "no".

However, "much closer" are weasel words (https://www.google.com/search?q=weasel+words) making it impossible to answer your question.
Do you know anything about the topic or are you just trying to "look smart"? I don't understand what you don't understand about my statement.
Title: Re: Long term stability of internal RC oscillator
Post by: Coding Badly on Apr 28, 2017, 06:32 am
I don't understand what you don't understand about my statement.
"Much closer".  Would that be 0.1%?  0.001%?  0.05%?  0.9999%?

While "much closer" may mean something concrete to you it means nothing to anyone else.

Title: Re: Long term stability of internal RC oscillator
Post by: Smajdalf on Apr 28, 2017, 01:04 pm
... much closer than +/- 1%. But I have no idea how close it will be. And how close it will be after a year?
"Much closer".  Would that be 0.1%?  0.001%?  0.05%?  0.9999%?

While "much closer" may mean something concrete to you it means nothing to anyone else.
While "much closer" may not be rigorously defined I still don't see how it makes my questions impossible to answer since they don't use it.
Title: Re: Long term stability of internal RC oscillator
Post by: MrMark on Apr 28, 2017, 04:45 pm
You are speaking about short term accuracy. I imagine the frequency is fluctuating around "mean frequency". While the fluctuations may be quite large and interfere with precise measuring or asynchronous communication the "mean frequency" may be stable. For example if it takes 121,954 seconds for WDT set to 1s timeout to overflow 100,000 times it is reasonable to expect next 100k overflows will take very close to 121,954 seconds - much closer than +/- 1%. But I have no idea how close it will be. And how close it will be after a year?
Clock error is going to have both a bias from the desired mean and a random variation.  It's the bias term which contributes to long term drift.  Getting drift significantly better than 1%* is simply not going to be possible with an IC RC circuit.  "Significantly better" in this context is "approaching 0.002%" per your 1 minute per month drift requirement (see uxomm's post above).

2E-5 performance will require a crystal oscillator time base.  Good crystals intended for timekeeping with performance on the order of 1E-6 are not expensive items.
Title: Re: Long term stability of internal RC oscillator
Post by: Coding Badly on Apr 28, 2017, 07:59 pm

Given the constraints you have given...

Quote
...provided regulated voltage and near constant (indoor) temperature...
Quote
For example if it takes 121,954 seconds for WDT set to 1s timeout to overflow 100,000 times it is reasonable to expect next 100k overflows will take very close to 121,954 seconds - much closer than +/- 1%.
...I stand by reply #3.

The watchdog oscillator is very sensitive to temperature / voltage.  Minor temperature changes of the die (e.g. manipulating digital I/O) will likely prevent you from getting "much closer than +/- 1%".  Unless you are building an application that performs no useful work.

The internal oscillator is less sensitive.  You may be able to get "much closer than +/- 1%".

Title: Re: Long term stability of internal RC oscillator
Post by: srnet on Apr 29, 2017, 11:21 pm
If I calibrate such oscillator against precise time source (GPS, RTC) may I use it as reasonable source of time provided regulated voltage and near constant (indoor) temperature or will it drift too much? I mean +/- minute in a month or so.
Why dont you calibrate the oscillator as you suggest, monitor it over a month or so, and report back ?

 
Title: Re: Long term stability of internal RC oscillator
Post by: Smajdalf on Apr 30, 2017, 07:15 am
Why dont you calibrate the oscillator as you suggest, monitor it over a month or so, and report back ?
Because it will take a month. I tried to ask before I try it if someone tried this and failed or succeeded.
Looks like noone (here) knows about it, just guessing.
Title: Re: Long term stability of internal RC oscillator
Post by: Coding Badly on Apr 30, 2017, 07:59 am
Looks like noone (here) knows about it, just guessing.
When I started participating in this conversation I was willing, if needed, to dug up the several months of data I have collected for the watchdog oscillator over various voltages and temperatures.  Given the fact that I'm "just guessing" I guess I won't bother.

Title: Re: Long term stability of internal RC oscillator
Post by: srnet on Apr 30, 2017, 08:14 am
I cant see what would be gained even if you had a view as to what what the stability was.

Any test of that type would only be valid for one particular micro and could not be used to accuratly predict and guarantee how another example might behave.

Thus most reasonable designers, when they want such long term stability, would not be relying on the internal RC clock or WDT. 
Title: Re: Long term stability of internal RC oscillator
Post by: Smajdalf on Apr 30, 2017, 08:58 am
When I started participating in this conversation I was willing, if needed, to dug up the several months of data I have collected for the watchdog oscillator over various voltages and temperatures.  Given the fact that I'm "just guessing" I guess I won't bother.
Sorry, I didn't want to insult you. Your answer used wods as "likely" and "may" without any closer information. I thought it is just your opinion withot analysing such data. I am aware this question may be interesting for me but there is little real use of it so I was not suprised noone came with much information. If you have such data I would be glad if you found them and be willing to share.
Title: Re: Long term stability of internal RC oscillator
Post by: MrMark on Apr 30, 2017, 03:48 pm
Because it will take a month.
You can get a pretty good idea of drift rate much quicker than that.  I've done alignment of 5E-11 rubidium standards in under an hour staring at Lissajous patterns on an analog scope back in the day.

I've looked at the AVR datasheet and some application notes and one thing to consider is that the resolution of the calibration register is about 0.3% so that rules out using the hardware directly.  One could do additional compensation in software, perhaps even performing some form of temperature compensation, but it doesn't seem like a particularly robust approach.

In any case, the question has piqued my curiosity enough that I'm working on a test configuration to collect some data.  Since I've been playing in the stm8/stm32 domain recently, I'll be collecting against those rather than AVRs, at least initially, but I expect the behaviors are broadly the same.  I'll post some data to this thread when I have it.
Title: Re: Long term stability of internal RC oscillator
Post by: westfw on May 01, 2017, 08:51 am
Note that while the "internal 8MHz RC Oscillator" is "high accuracy" and able to be calibrated via the OSCCAL register, that is NOT true of the 128kHz watchdog oscillator.  I haven't measured anything on an ATmega328p, but on an ATtiny11 (which uses a similar (?) watchdog oscillator as THE internal clock (nominally 1MHz)), I can tell you that the frequency varies dramatically and visibly (on a blinking-LED-like program) depending on supply voltage; perhaps 60% going from 3V to 5V...
Title: Re: Long term stability of internal RC oscillator
Post by: MrMark on May 01, 2017, 02:56 pm
Note that while the "internal 8MHz RC Oscillator" is "high accuracy" and able to be calibrated via the OSCCAL register, that is NOT true of the 128kHz watchdog oscillator.  I haven't measured anything on an ATmega328p, but on an ATtiny11 (which uses a similar (?) watchdog oscillator as THE internal clock (nominally 1MHz)), I can tell you that the frequency varies dramatically and visibly (on a blinking-LED-like program) depending on supply voltage; perhaps 60% going from 3V to 5V...
Figure 1-2 in this application note shows the 8 MHz sensitivity to supply voltage for an Atmega8: Atmel-2555-Internal-RC-Oscillator-Calibration-for-tinyAVR-and-megaAVR-Devices_ApplicationNote_AVR053.pdf (http://www.atmel.com/Images/Atmel-2555-Internal-RC-Oscillator-Calibration-for-tinyAVR-and-megaAVR-Devices_ApplicationNote_AVR053.pdf)  It's on the order of 10% but certainly significant.  It indicates the full range temperature sensitivity is of similar magnitude.  This doesn't directly address the question of what happens under moderately well controlled voltage and temperature, but improving four orders of magnitude seems really unlikely.
Title: Re: Long term stability of internal RC oscillator
Post by: Mixe on May 03, 2017, 07:07 pm
Funny, I'm designing a test circuit for this very purpose right now. I'm testing the reliability of the watchdog RC oscillator in power down mode, both for longer periods of time and 24 hours for the ATtiny10 (extremely small, 1 kb mc, not programmable by the arduino IDE yet. But it's awesome). The setup is:

An ATtiny10 powered by one single CR2032 battery, running a code that puts it into sleep mode for 24 hours in 8 second intervals. When it wakes up, it fires of a pulse through one of its IO-pins. The pulse triggers an interrupt on an ESP8266 connected to an RTC. It logs the time, calculates the interval and resets the ATtiny10. It also runs a server that dispays the list of intervals to incoming connections.
I will also test longer intervals and one running code.

Right now I have some problems getting the voltage provided by the ATtiny's IO pin to successfully provoke an interrupt on the  ESP. The level is too low to be read as a pin change. If anyone has any idea how to change the level needed I would be very grateful! And naturally post my findings here.

Title: Re: Long term stability of internal RC oscillator
Post by: CrossRoads on May 03, 2017, 07:54 pm
What level is needed?
Buffer the signal with cd74HC4050, powered from the ESP8266 Vcc supply. Output will then be low and  high that is compatible with ESC8266.
Title: Re: Long term stability of internal RC oscillator
Post by: Mixe on May 04, 2017, 08:26 am
I think 3V is the trigger level, but the datasheet for ESP is not as detailed and extensive as those for AVR:s, so I reach that figure by trial and error. If anyone knows how to find out for sure, I'm all ears.

And thank you so much for the tip! I will check out the cd74HC4050 immediately.
Title: Re: Long term stability of internal RC oscillator
Post by: Smajdalf on May 04, 2017, 09:02 am
I don't exactly understand how HC4050 is supposed to do the level shifting. Probably easiest way is transistor in common emitter configuration.
Title: Re: Long term stability of internal RC oscillator
Post by: Smajdalf on May 06, 2017, 06:17 pm
After some problems I have first results:
(https://forum.arduino.cc/index.php?action=dlattach;topic=473471.0;attach=210925)

I recorded number of "16 ms" overflows of both WDT and Timer0 for one minute. On the graph are "moving sum" (like moving average (https://en.wikipedia.org/wiki/Moving_average) without averaging) of 200 minutes - IIRC the blue line is Timer0 and orange is WDT. Data are from not full day experiment. I was monitoring voltage and it dropped at end of the experiment for some reason. I will collect the data for much longer time (at least one week) with monitoring of temperature and voltage and make some decent statistical analysis. I wonder what will be result.

EDIT: I was a bit busy lately so I let my experiment run much longer than expected. Due to a bug in SPI settings no data were recorded :-( It should be fixed now...
Title: Re: Long term stability of internal RC oscillator
Post by: Smajdalf on Jul 24, 2017, 06:06 pm
I got some data from about 14 days. Both WDT and main oscillator have about the same variations. Sadly it depends on temperature so much it is possible to determine temperature variations between night and day indoors hidden in my table drawer and so it is not possible to use it to some good time keeping without callibration to temperature. On the other hand this be exploited somehow: main oscillator frequency increases with increasing temperature while WDT oscillator slows down and so differences may be measured even without better time reference. It is interesting the frequency oscillations are more sensitive than internal temperature sensor. Here is graph from my experiment. If someone is interested in the raw data I can share them but I think everything important is in the graph.

EDIT: the picture was too large and was not included. There it is:

(https://forum.arduino.cc/index.php?action=dlattach;topic=473471.0;attach=220078)
Title: Re: Long term stability of internal RC oscillator
Post by: zoomx on Jul 24, 2017, 07:53 pm
Can you share your sketch?
Title: Re: Long term stability of internal RC oscillator
Post by: Smajdalf on Jul 24, 2017, 08:47 pm
Can you share your sketch?
I can but it was not intended to be shared so I don't know how much you will understand. Also it was written and compiled it Atmel Studio 7 for standalone Atmega328p. Data were saved to 1Mb SPI EEPROM, I read them back by simple sketch in Arduino. That sketch is so messy I won't share it ;-)
Code: [Select]

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
#define WREN  6 //write enable
#define WRDI  4 //write disable
#define RDSR  5 //read status register
#define WRSR  1 //write status register
#define READ  3
#define WRITE 2
#define PE 0b01000010
#define BP1 3
#define BP0 2
#define WEL 1
#define WIP 0
#define StartPin (1<<PB0) //pin 14
#define LivingPin (1<<PB6) //pin 9
#define SSPin (1<<PB2) //pin 16
//pin 4 is for external interrupt
#define DefaultADCSRA ((1<<ADEN)+(1<<ADPS2)+(1<<ADPS1))
#define TempADMUX ((1<<REFS1)+(1<<REFS0)+(1<<MUX3))
#define VADMUX ((1<<REFS0)+(1<<MUX3)+(1<<MUX2)+(1<<MUX1))

#define SavingTime 60*5 //seconds; currently 5 minutes -> overflow in about 37 days
#define maxPointer 6*21-3 //just to be sure
#define DataBufferLength 128

volatile unsigned int WDT_overflows, counter_overflows, seconds;
unsigned int dataToSave[DataBufferLength], minTemp=-1, maxTemp=0, minV=-1, maxV=0, pageAddress=0;
uint8_t timeToSave, dataPointer, readV;

static void sendToMemory();
static void selectMemory();
static void deselectMemory();
static void toggleLiving() {PINB=LivingPin;} //Pin 9 next to GND

ISR(WDT_vect) {
WDT_overflows++;
}

ISR(TIMER0_COMPA_vect) {
counter_overflows++;
}

ISR(INT0_vect) {
seconds++;
toggleLiving();
}

static void debugLED() {
PORTB|=1<<PB1;
}

int main(void) {
deselectMemory();
DDRB=SSPin+(1<<PB5)+(1<<PB3);
PORTB|=StartPin|LivingPin;
SPCR = (1 << SPE) + (1 << MSTR);
SPSR = (1 << SPI2X); //Set double speed
while (PINB&StartPin) {} //Waiting for pressing of button
EIMSK = 1; //enable INT0
EICRA = 2; // falling edge on INT0 generates interrupt
wdt_reset();
WDTCSR=(1<<WDCE)+(1<<WDE);
WDTCSR=(1<<WDIE); //about 16ms interrupt
OCR0A=127;
TCCR0A=1<<WGM01; //CTC mode
TCCR0B=(1<<CS02)+(1<<CS00); //Prescaler 1024
TIMSK0=(1<<OCIE0A); //Enable interrupt
ADCSRA=DefaultADCSRA; //Enable ADC with prescaler 64
TCCR2B=7; //Prescaler 1024
while(1) {
cli();
if (seconds>=SavingTime) {
seconds=0;
dataToSave[dataPointer++]=WDT_overflows;
WDT_overflows=0;
dataToSave[dataPointer++]=counter_overflows;
counter_overflows=0;
dataToSave[dataPointer++]=minTemp;
minTemp=-1;
dataToSave[dataPointer++]=maxTemp;
maxTemp=0;
dataToSave[dataPointer++]=minV;
minV=-1;
dataToSave[dataPointer++]=maxV;
maxV=0;
if (timeToSave) {
debugLED();
}
}
sei();
if ((dataPointer>maxPointer)&&(!(timeToSave))) {
timeToSave=2;
dataPointer=0;
selectMemory();
SPDR=WREN;
}
if ((SPSR&(1<<SPIF))&&(timeToSave)) {
sendToMemory();
}
if (TIFR2&(1<<TOV2)) {
TIFR2|=(1<<TOV2);
ADCSRA=DefaultADCSRA+(1<<ADSC);
}
if (ADCSRA&(1<<ADIF)) {
ADCSRA=DefaultADCSRA+(1<<ADIF);
unsigned int result = ADCW;
if (!(readV)) {
ADMUX=VADMUX;
readV=1;
if (result>maxTemp) {
maxTemp=result;
}
if (result<minTemp) {
minTemp=result;
}
}
else {
if (readV++>8) {
readV=0;
ADMUX=TempADMUX;
}
if (result>maxV) {
maxV=result;
}
if (result<minV) {
minV=result;
}
}
}
}
}

static void deselectMemory() {
PORTB|=SSPin;
}

static void selectMemory() {
PORTB&=~SSPin;
}

static void sendToMemory() {
if (timeToSave==1) { //writing into EEPROM LSB
SPDR=dataToSave[dataPointer];
timeToSave=4;
}
else if (timeToSave==4) {//writing into EEPROM MSB
SPDR=dataToSave[dataPointer]>>8;
if (dataPointer<(DataBufferLength-1)) { //write next byte
dataPointer++;
timeToSave=1;
}
else {
timeToSave=5;//finish writing
dataPointer=0;
}
}
else if (timeToSave==2) { //only WREN was send
deselectMemory();
timeToSave=3;
}
else if (timeToSave==3) {
selectMemory();
SPDR=WRITE;
timeToSave=6; //need to send address
}
else if (timeToSave==5) { //end writing page
deselectMemory(); //deselect EEPROM
timeToSave=0;
PORTC=(pageAddress>>3);  //2^9 pages; to fit to 6 bit PORTC
pageAddress++;
}
else if (timeToSave==6) {
SPDR=pageAddress>>8;
timeToSave=7;
}
else if (timeToSave==7) {
SPDR=pageAddress;
timeToSave=8;
}
else if (timeToSave==8) {
SPDR=0;
timeToSave=1;
}
}

Title: Re: Long term stability of internal RC oscillator
Post by: zoomx on Jul 24, 2017, 09:30 pm
Thanks!
You choosed to save on a SPI memory because is faster than a serial print or than using an SD, right?
Title: Re: Long term stability of internal RC oscillator
Post by: Smajdalf on Jul 24, 2017, 10:05 pm
I used SPI because it is easier to use than SD card (less SW, works on 5V). I did not use serial print because it was standalone chip without USB <-> TTL and because it run for about 2 weeks - I don't have computer that would run so long. I turn computer off before I go sleep/leave for long.
Title: Re: Long term stability of internal RC oscillator
Post by: MrMark on Jul 24, 2017, 10:19 pm
Could you post the WDT and Oscillator data without normalizing the scale as in post #21?  The absolute magnitude of variation is not clear from the graph of #22.

What do you use at the reference time base (INT0)?
Title: Re: Long term stability of internal RC oscillator
Post by: Smajdalf on Jul 24, 2017, 10:37 pm
Reference was PPS signal from DS3231. I think best reasonably available reference. Here are graphs for the oscillators and raw data.
(https://forum.arduino.cc/index.php?action=dlattach;topic=473471.0;attach=220105)
(https://forum.arduino.cc/index.php?action=dlattach;topic=473471.0;attach=220103)
Title: Re: Long term stability of internal RC oscillator
Post by: westfw on Jul 25, 2017, 05:26 am
So the main osillator varies over a range of about 100/18000?  About 0.5%?  That's not too bad.
That they have opposite response to temperature changes is ... interesting!
Title: Re: Long term stability of internal RC oscillator
Post by: Smajdalf on Jul 25, 2017, 08:33 am
That they have opposite response to temperature changes is ... interesting!
Itbis not so surprising: the typical characteristics graphs show this behavior. It is also true for Vcc. With increasing Vcc WDT frequency decreases while main oscillator frequency increases.
Title: Re: Long term stability of internal RC oscillator
Post by: Jiggy-Ninja on Jul 25, 2017, 02:10 pm
So the main osillator varies over a range of about 100/18000?  About 0.5%?  That's not too bad.
That they have opposite response to temperature changes is ... interesting!

They have opposing response to voltage variation as well. The watchdog oscillator decreases in frequency with increases in temperature or supply voltage. The 8 MHz oscillator increases in frequency with supply voltage and temperature increases.
Title: Re: Long term stability of internal RC oscillator
Post by: MrMark on Jul 25, 2017, 03:28 pm
Sadly it depends on temperature so much it is possible to determine temperature variations between night and day indoors hidden in my table drawer and so it is not possible to use it to some good time keeping without callibration to temperature.
This Application Note (http://www.atmel.com/Images/Atmel-8108-Calibration-of-the-AVR%27s-Internal-Temperature-Reference_ApplicationNote_AVR122.pdf) suggests the internal temperature sensor sensitivity is about 1 LSB/degree C.  Your figure has variation over about 8 LSBs. 

Just to get a feel for the oscillator variation over temperature, does about 8 C variation over the duration of the test seem about right?
Title: Re: Long term stability of internal RC oscillator
Post by: Smajdalf on Jul 25, 2017, 04:04 pm
Just to get a feel for the oscillator variation over temperature, does about 8 C variation over the duration of the test seem about right?
Didn't think about this. I don't remember weather in the two weeks. But variation between noon of sunny summer day and midnight of a rainy day may be easily 8°C. We have windows to south and so weather has relatively large impact.