delayMicroseconds() incorrect

Hi,

I need the Arduino to provide a signal 100 - 10000 ns "high" in a 57,5 Hz cycle. Thats my calculation:

At 57.5 Hz one “cycle” takes 17391304,34 ns
Signal “high”-time has to be between 100 ns and 10000 ns.
Example: Signal high for 5000 ns, then low for 17386304,34 ns then cycle

My code for that:

void setup()
{
  pinMode(13,OUTPUT);
}
 
void loop()
{
  digitalWrite(13,HIGH);
  delayMicroseconds(5);
  digitalWrite(13,LOW);
  delayMicroseconds(8693);
  delayMicroseconds(8693);
}

When I measure the signal with an oscilloscope, its just 680 ns high, a frequency counter says 57,1944 Hz.

What's wrong? How to get a precise signal?

Thanks for some help...

What board? What speed? What clock source?

delayMicroseconds() is not great for short delays, but 680ns is really short for what should be 5000ns...

The Arduino works @ micro-seconds level in practice. So 57.5Hz = 17391.xxx uSec
So your 8693's are quite good.
However depending on the Arduino you have (Crystal or resonator) the 16Mhz clock might be off a few percent.
you could correct for this.

note that the call for loop() takes time and the digitalwrite()'s too.

and yes 680ns is indeed too short.

I f you want it more precise, you'll need to get rid of loop jitter and digitalWrite delays.
You'll still see some jitter from micros() and millis() interrupts.

void loop(){
while (1){
PORTB = PORTB | 0b00100000; // pin high
delayMicroseconds(5);
PORTB = PORTB & 0b11011111; // pin low
delayMicroseconds(17386); // tweak as needed for they 6-8 clock cycles of direct port manuipulation
} // end while
} // end loop

Which version of IDE are you running?

Thanks for the replies. I am using an Arduino Nano 3.0, so a quartz clock. IDE is 1.6.6. Will try the code...

tarduino800:
Thanks for the replies. I am using an Arduino Nano 3.0, so a quartz clock. IDE is 1.6.6. Will try the code...

Doesn't a Nano have a 16MHz resonator, not a crystal?

I can't understand how you could get a sub-microsecond delay. Are you sure you measured the pulse width correctly?

Schematic says Resonator.

What I found out: Using the code of CrossRoads (thank you for that) my Arduino delivers a stable signal of 5.06 microseconds - perfect.

The Arduino has to feed the signal to another circuit which expects a timing as mentioned above. When I connect it to that circuit the voltage goes down and then the signal varies between 600 - 900 nanoseconds. Not good as it effects the total timing of the cycle.

What could be the reason for that?

Sorry for these newbie questions...

What load does the other circuit present to the Arduino output? Sounds like either the connecting cable (you are connecting signal plus gnd?) are bogging it down, or the end circuit is.

What is the other circuit?

Are you sure the total time per loop changes, not just the apparent width of that pulse?

Crossroads: The Arduino is supplied with 12V current and ground from the interface, the Arduino delivers the signal. According to the specification there must be a 10 kOhms resistor between ground and the signal wire. A 500 Ohms resistor lowers the voltage of the pulse to 1.9V (specification: 1 - 2V)

DrAzzy: The other circuit is a kind of interface. The interface-log says: 57.1944 Hz, so less than calculated.

5V x 500 / (500 +10000) = 0.238V output level.
Try output output to 2K, 1K to Gnd. 1.6V at the junction.

Can you post a drawing of how you have it wired?

I don't like the sounds of it.