Hi guys! Need true 60Hz stable frequency. I make a program with calc 60Hz square wave. I tried check it on mechanic clock but it return wrong time about 0.5% per minute. Please look, where I am wrong?
// 60000000 turns per minute
// 8333,333333333333 * 2 = 16666,66666666667; full cycle
// 16666/2 = 8333;
// 0,66666666667 = 2/3; two mls per 3 cycle
// 833322400 = 39998400
// 833421200 = 20001600
// Total = 60000000!
That is because the Arduino uses a ceramic resonator for the clock. You need a quartz crystal for better
accuracy.
Secondly your code is not accurate in the first place. If you want to output pulses at a regular
rate you don't called delay, that's always going to be slow because there are other delays in
your code that you haven't allowed for.
You need to synchronize to the system clock something like this:
#define PERIOD 8333
unsigned long next_time = 0ul ;
void loop ()
{
if (micros () - next_time >= PERIOD)
{
next_time += PERIOD ;
// do something
}
}
Of course this has the issue that 8333 is not accurate in the first place.
Using a delay for timing is never accurate. The delayMicroseconds itself is not very accurate, the time needed for the functions might change with a new Arduino version. The compiler might take different decisions that influence the timing. There are also interrupts, at least for timing and perhaps more things like communication, but that depends on the rest of your sketch. In short : it will never work.
MarkT:
That is because the Arduino uses a ceramic resonator for the clock. You need a quartz crystal for better
accuracy.
Secondly your code is not accurate in the first place. If you want to output pulses at a regular
rate you don't called delay, that's always going to be slow because there are other delays in
your code that you haven't allowed for.
Of course this has the issue that 8333 is not accurate in the first place.
Thanks, i will tried it with your advice.
Yes, 8333 is not accurate. It has tail 0,333 but it compensation with my algorithm i think). 2/3 of minute it has work time with 8334.
But you still have the +/- 0.3% accuracy of the ceramic resonator to deal with. Some
Unos have pads for installing a 16MHz quartz crystal and its capacitors, but its SMT only I
think.
Unless you're trying to build a clock that shows the time of day, 0.3% accuracy is pretty damn good.
The accuracy we expect from our clocks is absolutely fantastical if you try to do the same for distance measurement. It's like having a 14-foot tape measure marked in thousandths of an inch. You could not even see those marks on the tape without a magnifier.
0.3% is hopeless for many digital protocols though, DMX, I2S, ethernet, and for higher baud
rates it adds to the error due to non-integral clock divisors. In general every digitial appliance
you own has a quartz crystal in it, except the Uno... Even the Uno needs a quartz crystal
for the USB interface!
Sure, cheap crystals are cheap. They might stop working when the temperature is below freezing point, or get damaged if the Arduino board is dropped.
This might be the cheapest Uno R3 at the moment : http://www.ebay.com/itm/381350929204
It looks the same as the one that you found, and also with two crystals.
Koepel:
Sure, cheap crystals are cheap. They might stop working when the temperature is below freezing point, or get damaged if the Arduino board is dropped.
Crystals are hermetically sealed, quartz doesn't care about the freezing point of water...
Digikey does not sell crystals that stop working only by looking at it.
Let's see what is on Ebay: The cheapest 16MHz crystal is 5.85 dollarcents inclusive shipping (when buying 100pcs).
Perhaps for the maker of the Arduino clone it is only 3 or 4 cents (or less).
MarkT, I was only trying to avoid the "Celsius/Fahrenheit" by mentioning "freezing point". The cheapest crystals are specified from 0°C and up. However, I think the crystals on the cheapest Arduino clone might not even have specifications (only for the size and capacitance).
You could put all your Arduino clones into a freezer, and I'm sure a few will stop working.