Arduino Forum

Using Arduino => Microcontrollers => Topic started by: CopperDog on May 12, 2015, 10:21 pm

Title: Internal oscillator 328P how 'inaccurate' is it?
Post by: CopperDog on May 12, 2015, 10:21 pm
So I have my 328P running the calibrated internal oscillator (no external crystal or resonator). The datasheet indicates the internal is not designed for high accuracy. So how inaccurate are we talking about here?

My chip is running a game timer. Can I expect to see timing errors in the range of seconds, minutes or just a few millis in a 30 minute game?
Title: Re: Internal oscillator 328P how 'inaccurate' is it?
Post by: Coding Badly on May 12, 2015, 10:42 pm

Have you tuned the oscillator?

Title: Re: Internal oscillator 328P how 'inaccurate' is it?
Post by: DrAzzy on May 12, 2015, 11:28 pm
Page 304 quotes +/-10% for the '328p, which is terribad - but that's over the full temp range specified, while you're likely to be running it at room temperature.

Some of the other AVR chips spec 2%, or 2% w/in narrow temp range, 10% w/in full temp range.
Title: Re: Internal oscillator 328P how 'inaccurate' is it?
Post by: jboyton on May 13, 2015, 12:15 am
It also says that user calibration via the OSCCAL register can narrow the error to ±1% over the whole temperature range. Even allowing for a narrower range it still sounds like the error would be on the order of seconds per 30 minutes. But I have not tried it, not yet anyway.
Title: Re: Internal oscillator 328P how 'inaccurate' is it?
Post by: CopperDog on May 13, 2015, 01:41 am
This application could be exposed to anything from -15 to 30+ Celsius so the I expect there to be some effects. The calibration is set to 0x9A which in the data sheet is listed as a reserved address. Any idea what that setting is giving me for a clock frequency?
Title: Re: Internal oscillator 328P how 'inaccurate' is it?
Post by: CopperDog on May 13, 2015, 01:43 am
Have you tuned the oscillator?
The 0x9A clock calibration setting refers to the tuned value correct? To get the best internal accuracy the clock calibration should be set to match the system voltage, if I'm reading this right.  If I'm interpolating correctly then my internal oscillator should be set to 7.99 MHz for the 5V rail. Is this correct?
Title: Re: Internal oscillator 328P how 'inaccurate' is it?
Post by: jboyton on May 13, 2015, 02:05 am
The calibration is set to 0x9A which in the data sheet is listed as a reserved address. Any idea what that setting is giving me for a clock frequency?
Huh? What do you mean the value is a reserved address?

It's in the data sheet. At system reset a default value is loaded into the OSSCAL register which gives you 8MHz ± 10%. You can reprogram the register to tweak the frequency and get better accuracy. There are charts in the data sheet that show the relationship between the OSSCAL value and the frequency, as well as the temperature dependence. But if it were me I'd want to measure the clock frequency, either with an oscilloscope or by comparing the output of a counter against a stopwatch over some period of time.

I suppose it would be possible to continually tweak the value based on temperature and voltage measurements. I don't know how well that would work. At some point it's easier to buy a crystal.
Title: Re: Internal oscillator 328P how 'inaccurate' is it?
Post by: CopperDog on May 13, 2015, 02:16 am
Huh? What do you mean the value is a reserved address?
Sorry, this is my first foray into the chip settings. When I search the data sheet the only reference I find for 0x9A is a page that shows it as a reserved address.

I had a crystal on the chip but the audio (tone) output of this chip is getting sent to a siren through a 5W amplifier. I was getting an artifact from the crystal that could be heard in the amplifier output. Running on the 8MHz internal I get none of that.

I'll look for the charts.
Title: Re: Internal oscillator 328P how 'inaccurate' is it?
Post by: CopperDog on May 13, 2015, 02:38 am
Ahh. I see the chart you mean. (ATmega328P: Calibrated 8 MHz RC Oscillator Frequency vs. OSCCAL Value).

So, the 0x9A (154 DEC) at 8MHZ plots outside the charts best range. The 8MHZ line intersects the curve at 184 or 0xB8. Am I interpreting that correctly? 
Title: Re: Internal oscillator 328P how 'inaccurate' is it?
Post by: CopperDog on May 13, 2015, 02:50 am
Is it as simple as putting OSCCAL = 154; in Setup()?
Title: Re: Internal oscillator 328P how 'inaccurate' is it?
Post by: uxomm on May 13, 2015, 03:26 am
Quote
Is it as simple as putting OSCCAL = 154; in Setup()?
Yes!

But with the value you may have to experiment to get good results.

You could also take a look what the OSCCAL of your individual 328P is at the moment like

Code: [Select]

void setup()
{
     Serial.begin(9600);
     Serial.println(OSCCAL);
}
Title: Re: Internal oscillator 328P how 'inaccurate' is it?
Post by: CopperDog on May 13, 2015, 06:52 am
Excellent. Thanks, I'll do that.
Title: Re: Internal oscillator 328P how 'inaccurate' is it?
Post by: westfw on May 13, 2015, 09:38 am
The OSCCAL loaded at reset is one that is programmed at the factory to yield the specification in the datasheet (~10% at some particular operating voltage, IIRC.)
You can fine-tune the value for your particular operating conditions, and/or a slightly different frequency, by loading a different value into OSCCAL, but you probably have to determine the value experimentally - it's not something you can just read off the graph in the datasheet - if the datasheet were exact, they'd just program OSCCAL to have the exactly correct value.

There are some tools that you can use to find more accurate values, assuming that you have an accurate thing to measure them.  Google "osccal calibration clock arduino" and there are lots of results...

Title: Re: Internal oscillator 328P how 'inaccurate' is it?
Post by: CopperDog on May 13, 2015, 06:34 pm
Yes I see your point. Just referencing the graph is not going to do it. I'll do some testing. The accuracy may be enough as is.
Title: Re: Internal oscillator 328P how 'inaccurate' is it?
Post by: Nicknml on May 13, 2015, 09:23 pm
I've had an experience with inaccuracy of an internal oscillator, not with a 328p but with a 1284p.  When I was using the serial peripheral, I was getting garbage output.  I thought the serial peripheral was fried from a wiring mistake.  I then decided to switch over to an external crystal and the serial worked fine, which pointed to the internal oscillator being problematic.  I then used PWM on one of the pins to get a frequency low enough that could be measured my my multimeter's frequency measurement function. Turned out, it was off a bit.

I then hooked up the external crystal (and of course made the necessary changes to the fuses) and read the factory programmed OSCCAL value to give me a starting point.  I then switched back to the internal oscillator and experimented until I found an OSCCAL value that got me as close as I could get to 8 MHz.  After that, the serial peripheral worked off of the internal oscillator just fine.
Title: Re: Internal oscillator 328P how 'inaccurate' is it?
Post by: CrossRoads on May 13, 2015, 09:31 pm
What voltage is the 1284P running at? There was a topic last summer where a 16 MHz unit running at low voltage also had serial issues. I think with a crystal.
Speed Grades chart in 28.2, Figure 28-1, looks to have same speeds & voltage as 328P, so below ~3.8V running at 16 MHz is out of spec.
Title: Re: Internal oscillator 328P how 'inaccurate' is it?
Post by: Nicknml on May 13, 2015, 10:08 pm
The 1284P is running at 5V.
Title: Re: Internal oscillator 328P how 'inaccurate' is it?
Post by: pito on May 13, 2015, 11:46 pm
According to the spec the 8MHz internal osc is calibrated in factory at 3.0V (not 5V nor 3.3V).
I did measurements with 328p and the OSCAL 1-254 did 5-15MHz freq range (nonlinear, btw).
The int osc frequency is temperature and voltage dependent - provided you have set the frequency to a known value (must be measured by a frequency counter - you can pass the clock to RB0 output pin via fuse settings) and run your chip at stable temperature and voltage, you may get 1% "average stability" (it is an RC oscillator).
The OSCAL frequency "step" was something like 40-50kHz (plus minus, afaik). I also observed the frequency wobbled within a step, so the frequency counter reading was like 77xx.xxx kHz (for 7.7MHz). But that could be caused by noisy voltage source during measurements.

Mind however, the chip temperature changes based on the calculation you perform :)
Title: Re: Internal oscillator 328P how 'inaccurate' is it?
Post by: CopperDog on May 14, 2015, 12:10 am
OK so back to the original question then. How much inaccuracy would manifest in a real time clock? Does  10% freq error directly equate to +/- 3 min over on a 30 min timer?
Title: Re: Internal oscillator 328P how 'inaccurate' is it?
Post by: westfw on May 14, 2015, 01:01 am
Quote
Does  10% freq error directly equate to +/- 3 min over on a 30 min timer?
Yes.
Note that there oscillator is supposed to be pretty "stable"; so the error will be a particular amount in a particular direction, depending on the environment.  You won't be +10% in the first 30 minutes and -10% in the next...
Title: Re: Internal oscillator 328P how 'inaccurate' is it?
Post by: CopperDog on May 14, 2015, 02:14 am
Yes I agree the error would be a consistent one that changed with temperature however I'm trying to understand just what , for example a 5% error would consist of on a 8 MHz clock. Would that show up as a couple of seconds per minute (gained or lost depending on the error) or a few millis?
 
An angle error for instance can be calculated to be x amount at y distance.

If there's a way to calculate for an inaccurate clock there's a way to compensate with code or design. Just trying to wrap my head around it.

Title: Re: Internal oscillator 328P how 'inaccurate' is it?
Post by: Coding Badly on May 14, 2015, 03:12 am
If there's a way to calculate for an inaccurate clock there's a way to compensate with code or design.
Yup...
http://forum.arduino.cc/index.php?topic=80489.0 (http://forum.arduino.cc/index.php?topic=80489.0)
https://github.com/leomil72/swRTC (https://github.com/leomil72/swRTC)

Quote
I'm trying to understand just what , for example a 5% error would consist of on a 8 MHz clock
A 5% error is a 5% error.  If you do the math you will find it makes no difference where you apply the 5%.

Quote
Would that show up as a couple of seconds per minute (gained or lost depending on the error) or a few millis?
1 minute = 60 seconds; a more convenient unit for understanding the error.

60 - (5% * 60) = 57
60 + (5% * 60) = 63

A 5% error over one minute is ±3 seconds.

Title: Re: Internal oscillator 328P how 'inaccurate' is it?
Post by: westfw on May 14, 2015, 03:19 am
Ah.  I remembered what one of the calibrators was called.  Check out: http://ernstc.dk/arduino/tinytuner.html (http://ernstc.dk/arduino/tinytuner.html)

Title: Re: Internal oscillator 328P how 'inaccurate' is it?
Post by: CopperDog on May 14, 2015, 05:05 am
Thanks westfw. Just downloaded the tinytuner files.
 Coding Badly, if you're still here, I see the TinyTuner stuff is your baby. Will this need much tweaking to work on a 328P?
Title: Re: Internal oscillator 328P how 'inaccurate' is it?
Post by: CopperDog on May 14, 2015, 06:46 am
Testing the timing on my setup just using a stop watch and I can tell it's not going to work on the 8MHz internal without tuning and I'll have no idea what will happen with temp fluctuation later on. Switched it back to 16MHZ with a crystal and if there's an error I can't detect it using a stop watch. I'll live with the artifact on the amplifier output if I can't filter it.

When I have time I'll experiment with tuning the internal 8MHz.
Title: Re: Internal oscillator 328P how 'inaccurate' is it?
Post by: Coding Badly on May 14, 2015, 07:20 am
Coding Badly, if you're still here, I see the TinyTuner stuff is your baby. Will this need much tweaking to work on a 328P?
Version 1 does not have a binding for the m328p processor.  At the bottom of TinyTuner.h is this...

Code: [Select]
#if defined( __AVR_ATtiny2313__ )
  typedef TinyTunerTemplate<0x1B,0> Tiny2313Tuner;
  typedef Tiny2313Tuner TinyTuner;
#elif defined( __AVR_ATtiny84__ )
  typedef TinyTunerTemplate<0x16,1> Tiny84Tuner;
  typedef Tiny84Tuner TinyTuner;
#elif defined( __AVR_ATtiny85__ )
  typedef TinyTunerTemplate<0x16,4> Tiny85Tuner;
  typedef Tiny85Tuner TinyTuner;
#elif defined( __AVR_ATtiny45__ )
  typedef TinyTunerTemplate<0x16,4> Tiny45Tuner;
  typedef Tiny45Tuner TinyTuner;
#else
  #error Add a definition for your processor.
#endif


Adding something like this may work...

Code: [Select]
#elif defined( __AVR_ATmega328P__ )
  typedef TinyTunerTemplate<0x03,7> Tiny328Tuner;
  typedef Tiny328Tuner TinyTuner;


The parameters are I/O port, bit.  I usually put Tiny Tuner on the XTAL2 pin.  The snippet above should put the Tiny Tuner input on PINB7.

Version 2 is easier to configure (TinyTuner2.h) and already has a binding for the m328p processor (XTAL2 / PINB7).

Title: Re: Internal oscillator 328P how 'inaccurate' is it?
Post by: CopperDog on May 14, 2015, 03:22 pm
The parameters are I/O port, bit.  I usually put Tiny Tuner on the XTAL2 pin.  The snippet above should put the Tiny Tuner input on PINB7.

Version 2 is easier to configure (TinyTuner2.h) and already has a binding for the m328p processor (XTAL2 / PINB7).

Thanks for the response Coding Badly. I didn't realize there was a version 2 of the Tiny Tuner. I'll have to find that.
Title: Re: Internal oscillator 328P how 'inaccurate' is it?
Post by: Coding Badly on May 14, 2015, 08:32 pm

See Reply #22.

Title: Re: Internal oscillator 328P how 'inaccurate' is it?
Post by: jboyton on May 21, 2015, 03:42 am
I was hoping I could use the internal oscillator for a project I'm just starting. So I played around with it a bit to see what was what. Out of the box my Atmega328 defaulted to OSCCAL = 0xA6. At an ambient temperature of 71°F it ran a little slow, about 0.8% below 8MHz (for Vcc = 5V).

Tuning it from A6 to A7, A8, A9, AA as well as 66 and 67 (on the other side of the OSCCAL curve) produced this:

(http://s14.postimg.org/qsylfd1bl/OSCCAL_v_error.jpg)

Interestingly A7 and A8 were almost the same whereas A9 jumped up quite a bit. Also, while I expected it to be impossible to nail it given the 8-bit OSCCAL register, I was hoping the frequency would be stable and so amenable to software calibration. Instead the variation (for a given temperature and voltage) was something around ±0.1%. In comparison, when using a resonator to clock the chip, the variation at a fixed voltage and (relatively) fixed temperature was less than ±1ppm.

Pushing the chip through a range of temperatures produced this:

(http://s14.postimg.org/jvqmxm0vl/internal_temp_v_error.jpg)

And of course varying the voltage will do something similar.
From the datasheet:

(http://s18.postimg.org/opuscx0m1/Atmel_Vcc_temp_v_freq.png)


For what it's worth.