# Arduino Operational Frequency

Just wanted to clarify some pretty basic Arduino concept:

If I put this code into an arduino board:

double start, endTime;
long int counter = 0;

void setup()
{
Serial.begin(19200);
start = micros();
}

void loop()
{
endTime = micros();
counter++;

//Point A

if((endTime - start) > 1000000)
{
Serial.println(counter);
counter = 0;
start = micros();
}

}


...I see >38000 value in my serial monitor (for the 'counter' variable).

However, the moment I put in some heavy calculation in 'Point A', the value drops to 150 - 170. This is expected, I guess.

My question is: Is the only way to push up the operational frequency lies in optimising the code/calculation? Or, is there some other way I can get faster execution?

The microcontroller you are using has a certain clock frequency. Every operation you make it compute takes a specific (but constant) number of clock cycles to perform. What your code does is it enters the loop function, it performs all the tasks within it and then it jumps back to it's beginning and does everything again.

As you are limited by the number of operations you can perform per second and not the number of loop iterations, you need to either decrease the number of operations you are performing per loop iteration or find a way to increase the number of operations you can do per second.

In order to increase the number of operations you can do per second you need to increase the clock frequency which might mean buying a new chip if the one you are using is already at it's maximum.

• Regarding the chip's max freq, most 5V Arduinos have 16MHz crystals or resonators. The ATmega328 rated max freq (at 70C) is 20MHz but it can be overclocked at lower temperatures, eg to about 30MHz Mar 12, 2014 at 20:46
• Ok, got the point. Very nicely explained. Just one more slightly related question: is there anything remotely resembling multi-threading in ATmega328? Mar 13, 2014 at 16:42
• You can implement concurrent algorithms by using timer interrupts for example and using them to share resources (work a bit on one task and a bit on another one). However, you do not have multiple cores which means you unfortunately cannot achieve real parallelism. Mar 14, 2014 at 1:34
• @metsburg: Please ask about multi-threading as a separate top-level question. Yes, there are several techniques resembling multi-threading that work great in a ATmega328. There's not enough room in this comment to mention even the few approaches I know. The BlinkWithoutDelay approach to cooperative multithreading is very simple but often more than adequate. Mar 27, 2014 at 13:29
$$\text{operational frequency} = \frac{\text{calculations}}{t} = \frac{\frac{\text{instructions}}{t}}{\frac{\text{instructions}}{\text{calculation}}} = \frac{\text{16 MHz}}{\frac{\text{instructions}}{\text{calculation}}}$$