void loop speed?


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.

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.

Thanks Paul - that makes sense.

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

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.

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

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


I was trying to remember who's line that was.

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

Just as a matter of curiosity, why are you doing this?

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.


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.

Thanks mem - that's a help.