Go Down

### Topic: void loop speed? (Read 32131 times)previous topic - next topic

##### Nov 21, 2009, 02:59 pm
Hello,

Simple question that I can't find a specific answer to:

What speed/frequency does the void loop () run at? - or what sets what speed/frequency it runs at?

I'm calling a function from the void loop that sets a Digital I/O High or Low - just wondering how fast it's going.

Thanks - J.

#### PaulS

#1
##### Nov 21, 2009, 03:09 pm
The loop function is called in an infinite loop from within main.

As soon as loop returns, it is called again.

The length of time between the end of one execution of loop and the start of the next execution of loop calls is a few clock cycles while the return address is popped off the stack, jumped to, pushed back on the stack, and a jump to the loop function location in memory is made.

The length of time that loop takes to execute is a function of what you have it doing.

#2
##### Nov 21, 2009, 03:25 pm
Thanks Paul - that makes sense.

Might stick a frequency counter on it to see exactly what it's doing.

#### PaulS

#3
##### Nov 21, 2009, 03:35 pm
Everything you do in loop affects it's execution time. Don't think that you can DO anything with knowing how long a single pass through loop takes.

The act of measuring how long it takes affects how long it takes.

#### retrolefty

#4
##### Nov 21, 2009, 03:39 pmLast Edit: Nov 21, 2009, 03:39 pm by retrolefty Reason: 1
Quote
The act of measuring how long it takes affects how long it takes.

Ah, just as my signature line states, measurement changes behaviour.

Lefty

#### PaulS

#5
##### Nov 21, 2009, 03:41 pm
I was trying to remember who's line that was.

#6
##### Nov 21, 2009, 03:45 pm
Quote
The act of measuring how long it takes affects how long it takes.

Heh, yeah, fair point. I actually meant sticking a hardware frequency counter on the pin I'm changing the state of...

...which will put a load on it... but I'll stop there before I start questioning whether or not I'm *really* typing this.

#### PaulS

#7
##### Nov 21, 2009, 03:47 pm
Just as a matter of curiosity, why are you doing this?

#8
##### Nov 21, 2009, 03:54 pm
I'm using a 4052 multiplexer to 'multiply' the outputs of a TLV5618 DAC (from 2 - to 2 x 4).

The mux is being run (or clocked or whatever) by the Arduino.

Each output of the mux feeds a sample and hold.

I need to run the mux as fast as I can - but still give the DAC and the S&H time to do their thing and settle.

This is all pretty new to me - so I was just curious what the 'fastest' real world speed out of the Arduino might be.

Hope that makes sense - I'm kind of making this up as I go right now.

Thanks.

#### mem

#9
##### Nov 21, 2009, 04:08 pm
Assuming you only have the two digital writes in loop the frequency is just under 130 kHz (the period is 7.75us). Around half the time is taken by the digital writes, the remainder by the loop code. If you want to generate high frequencies you would be better served by using one of the timers, for example: http://www.arduino.cc/playground/Code/FrequencyTimer2.

This is one of those cases where measurement does not affect the readings in any meaningful way if you use something like a logic analyzer.