Have you ever measured the frequency of your main loop?

I was just curious about how fast my main loop is looping. I thought I could maybe add digitalWrite(4, !digitalRead(4)); to my loop and measure the oscillation on pin 4. I get about 2.9 KHz, dropping briefly to about 1.8 KHz every five seconds (every five seconds it has something extra to do. This is on an LGT8F328P with the clock speed set to 1 Mhz to save power. I haven't finished my sketch yet; it will be a fair bit slower when finished. There's no real point to this post; I just wondered how fast it was looping and was pleased that my effort to measure it seemed to work.

1 Like

void loop()
{
SerialPrnt(micros());

}

You can do some math . . .


UNO
If you have scope something like this can be used:

Thanks. I get a much, much different answer with micros(). Here's a table with some of the values I took from the serial monitor. Did I do the calculations wrong? People are gonna laugh at my slow loop. If it really is ~10 Hz, I should be able to see LEDBUILT_IN flashing if I toggle it as well. Edit: the LED just seems to be on constantly except for the interuptions every 5 seconds. It does however look flickery if viewed from my peripheral vision. If I can sense it flickering, it must at least be closer to 10 Hz than 2.9 KHz. The extra serial prints must have slowed it down a bit but I don't know how much. Edit: it's now 43.8 Hz according to my multimeter. It shoots back up to 1.8/1.0 KHz when I remove the serial print and 2.8/1.7 KHz when I remove the LED blink. By the same virtue, toggling pin 4 must also be slowing it down a bit but I've nothing to measure otherwise.

microS difference in us difference in s Hz
8613184 9408 0.09408 10.6292517006803
8622592 9344 0.09344 10.7020547945205
8631936 9344 0.09344 10.7020547945205
8641280 9408 0.09408 10.6292517006803
8650688 9344 0.09344 10.7020547945205
8660032
90000640 10432 0.10432 9.58588957055215
90011072 10432 0.10432 9.58588957055215
90021504 10368 0.10368 9.64506172839506
90031872 10432 0.10432 9.58588957055215
90042304 10432 0.10432 9.58588957055215
90052736 10432 0.10432 9.58588957055215
90063168

Unfortunately, I don't own an oscilloscope or logic analyzer.

Keep in mind Serial needs time to print the values.

Speed things up by increasing the baud rate to 115200.


The 1.13us in my image is the time from the closing brace to the starting brace in loop()


We are talking about µs

Using a scope, I get about 150kHz for:

void setup() {
  pinMode(2, OUTPUT);
}

void loop() {
  static uint8_t out = 0;
  out = !out;
  digitalWrite(2, out);
}

(on an Uno at 16MHz. About 350kHz on a Nano Every (also at 16MHz)(!))

That's pretty consistent with the known "slowness" of digitalWrite() - your initial example has the digitalRead() in there as well, which will slow things down even more.

Note that digitalWrite() speed is somewhat dependent on exactly which pin you are using...

See also Arduino toggle speed

Micros only increment in steps of 4, not steps of 1.

I was curious and since I also have LGT8F328P I decided to try. The tests are at an operating frequency of 32 MHz.

#define FAST

void setup ()
{
  pinMode ( D4, OUTPUT );
}

void loop ()
{
  #ifdef FAST
  fastioWrite ( D4, HIGH );
  fastioWrite ( D4, LOW );
  #else
  digitalWrite ( D4, HIGH );
  digitalWrite ( D4, LOW );
  #endif
}

Without FAST:
Sketch uses 874 bytes (2%) of program storage space. Maximum is 29696 bytes.
Global variables use 9 bytes of dynamic memory.
352 kHz

With FAST:
Sketch uses 646 bytes (2%) of program storage space. Maximum is 29696 bytes.
Global variables use 9 bytes of dynamic memory.
6.4 MHz

1 Like

@badrequest
I use this and similar to check loop speeds, to check how long a function takes to execute, to check that it does execute and all sorts of other things. The basic idea you are using can be extended to to check for lots of things. It can be a useful alternative to Serial.print for testing.

I am using LED in the middle and on the end of the loop.

void setup() {
  
  pinMode(LED_BUILTIN, OUTPUT);

}

void loop() {
   VRMS = (Voltage3 / 2.0) * 0.707; //root 2 is 0.707
  AmpsRMS = (VRMS * 1000) / mVperAmp;

  voltage = readVoltage();
  voltageString = String(voltage, 2);
  stringLength = voltageString.length();
  displayVoltage(stringLength);

  voltage2 = readVoltage2();
  voltageString2 = String(voltage2, 2);
  displayVoltage(stringLength);
  
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
digitalWrite(LED_BUILTIN, HIGH); 
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

  voltage3 = UzValue();
  voltageString3 = String(voltage3, 2);
  displayVoltage(stringLength3);

  // amp, phase
  voltage4 = readVoltage4();
  voltage5 = readVoltage5();

  ampString = String(voltage4, 0);
  shiftString = String(voltage5, 0);
  stringLength4 = voltageString4.length();
  displayVoltage(stringLength4);y:
  
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 digitalWrite(LED_BUILTIN, LOW);  
 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.