Loop Frequency?

How much time pin 13 value change per second ?
Please let me know Which arduino board is running on higher frequency? Specially i want to know about Arduino MEGA ADK and Arduino Due

int flag = 0; 
void setup(){
  pinMode(13, OUTPUT);      // sets the digital pin as output
}

void loop()
{
  if (flag == 0){
    digitalWrite(13, HIGH);
    flag=1;
  }else{
    digitalWrite(13, LOW);
    flag=0;
  }
}

Thanks in Advance :slight_smile:

Arduino Due, IDE 1.5.8, Results:

Test 1: 2.61 µs per iteration, 0.383 MHz
Test 2: 0.37 µs per iteration, 2.702 MHz

int flag = 0;
float startTime = 0.0;

void setup() {
  Serial.begin(115200);
  pinMode(13, OUTPUT);      // sets the digital pin as output

  // test 1 ----------------------
  startTime = micros();
  for (int i = 0; i < 10000; i++) {
    if (flag == 0) {
      digitalWrite(13, HIGH);
      flag = 1;
    } else {
      digitalWrite(13, LOW);
      flag = 0;
    }
  }
  Serial.println((micros() - startTime) / 10000.0, 2);

  // Test 2 ----------------------
  startTime = micros();
  for (int i = 0; i < 10000; i++) {
    if (flag == 0) {
      digitalWriteDirect(13, HIGH);
      flag = 1;
    } else {
      digitalWriteDirect(13, LOW);
      flag = 0;
    }
  }
  Serial.println((micros() - startTime) / 10000.0, 2);
}

void loop() {
}

inline void digitalWriteDirect(int pin, boolean val) {
  if (val) g_APinDescription[pin].pPort -> PIO_SODR = g_APinDescription[pin].ulPin;
  else    g_APinDescription[pin].pPort -> PIO_CODR = g_APinDescription[pin].ulPin;
}

The rate at which loop() is called is almost entirely a function of what code is IN loop(). While an empty loop() will run at 10kHz or more, this will only run at 1kHz:

void loop()
{
   delay(1);
}

Regards,
Ray L.

asamad:
How much time pin 13 value change per second ?

Maximum change frequency one one pin is 4 MHz on 16 MHz Arduino boards, I think.
So time is 1/4000000 = 0.00000025 s = 0.25 µs

But you'd have to avoid Arduinos 'comfort functions' such like 'digitalWrite()' completely and instead will have to program the controllers port and pin registers directly to achieve that high speed.

The Arduino provides some easy-to-use functions like 'digitalWrite()' which makes it easy to program an Arduino board for clueless people, but those functions are slow, so better avoid those if you want to very write fast operating code. A single call of 'digitalWrite()' may take something from 4µs to 6µs, which is much slower than what is possible when programming the hardware directly.

On the other hand, 'digitalWrite()' is fast enough to do a few dozen of thousands changes per second, which is more than enough for many applications.

Each pass thru loop() takes some time, on a 16 MHz '328P I think about 8-10uS.
Using this:

void loop(){
  while(1){
  PINB = 0b00100000; // write 1 to PB5 input register to toggle the output
  }
}

cuts out some of the overhead to let it toggle faster. You'd have to use an oscilloscope or logic analyzer to see it, the LED will just on all the time.
But all it does is toggle D13. Not very useful overall.

This looks like a classic XY problem to me.