True 60Hz problem

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!

for (int i=0; i <= 2400; i++){

digitalWrite(MOSFET_N1, 1);
delayMicroseconds(8333);

digitalWrite(MOSFET_N1, 0);

digitalWrite(MOSFET_N2, 1);
delayMicroseconds(8333);

digitalWrite(MOSFET_N2, 0);
}

for (int i=0; i <= 1200; i++){

digitalWrite(MOSFET_N1, 1);
delayMicroseconds(8334);

digitalWrite(MOSFET_N1, 0);

digitalWrite(MOSFET_N2, 1);
delayMicroseconds(8334);

digitalWrite(MOSFET_N2, 0);
}

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.

You could try millis() or micros() or use one of the hardware timers inside the Arduino.
Try the tone to create 60Hz : https://www.arduino.cc/en/Reference/Tone
I think there are other more accurate functions that are closer to the hardware.
Perhaps this one : teckel12 / Arduino toneAC / wiki / Home — Bitbucket

If you need a precise 60Hz for a clock, you need extra hardware with a temperature compensated crystal.

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.

I saw on my board already uses 16Mhz oscillator.

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!

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!

It's interesting that as the price of resonators and crystals fluctuate, I think some of the Chinese clones going back to using crystals on the processor. If you search around through the images on ebay you will see units with what looks like two crystals.
http://www.ebay.com/itm/NEW-UNO-R3-ATmega328P-CH340-Mini-USB-Board-for-Compatible-Arduino-/311155383820?hash=item48724e5e0c:g:QKMAAOSwdpxUU0UP

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.

This is board with crystal, as i think )

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...

Of course, a crystal that works over the widest spec temperate range there is, -55C to +125C, is only 70 cents

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.

Hi,
Why waste a UNO to make 60Hz.
They make IC's to do this. google..google 60Hz oscillator IC

http://www.electronicecircuits.com/electronic-circuits/50hz-60hz-frequency-generator-circuit-using-crystal-oscillator

Tom.... :slight_smile: