Loop frequency


I read multiple topics about this (certainly not enough), but I still don't know how to get the loop frequency.
For that purpose, I wrote this sketch:

void loop()  {
  Serial.print(" ");
  state = digitalRead(interrupteur);
  if (state == HIGH)
     // some long stuff
     analogWrite(pinUcom, 0);

This code returns a frequency of 190Hz, and 150Hz for my "long stuff".

I find that really really slow, comparing to the 16Mhz quartz frequency...

Is there something I don't do properly?

Best regards

s there something I don't do properly?

Yes, not posting your code!


How do you get a frequency from that?
By looking at the micros() times?
I bet you get different frequencies just by changing the
to a higher number - but then you flood the serial interface and bog down the PC (in my experience).

Do you have an oscilloscope? That's a better way.
This will yield a quick frequency:

void setup(){
pinMode (2, OUTPUT);
void loop(){
PIND = 0b00000100; // toggle PORTD-bit2 by writing to input register

Thanks for your answers.

My code in "long stuff" is not relevant. The important is that I just notice a difference between no load and complex calculation.

CrossRoads, I will borrow an oscilloscope to have a better precision, but I just want hove now an idea of the value.
Otherwire, what exactly does your code :cold_sweat: ? It "anologWrite" without using analogWrite?

But does this 150Hz value sounds familiar to you?

I don't know about 150Hz, depends on what your code is doing.
Drop the analogWrite and Serial.prints and just use a PIND = 0b000000100;
for whatever free pin have and monitor that with a scope. I think you'll see some improvement.

150Hz = 6667uS, 6.667mS.
I am writing 9x9x9 LED cube software now, it uses Blink Without Delay style time checking to update the next layer of the cube every 3000uS.

You can easily figure out how long a piece of code takes to run without an oscilloscope if you record the value of micros before and after you run the piece of code for 100 or 1000 iterations.

Don't include Serial.print() within the test unless it needs to be part of the test. Serial.print() is slow.


Hi again,

I don't know how to get the time for 1000 iterations without using Serial.print().

I the meantime, I bought an oscilloscope (a basic one, Velleman, but with the required bandwidth), but I get really really bad waveforms. I tried:

if (doubleTest == 1) {
    doubleTest = 0;
  else {
    doubleTest = 1;
  digitalWrite(pinTest, doubleTest);

I am waiting for a 1Hz square waveform, but I get a dirty signal @0.055Hz ...Do I normally have to get a square wave @1Hz?

I tried analogWrite(pinUcom, 0) and analogWrite(pinUcom, 255); and I get the same signal.

Is there precaution to take when using oscilloscope, or arduino output are really dirty and unobservable?




I tried analogWrite(pinUcom, 0) and analogWrite(pinUcom, 255); and I get the same signal

With a 0% and 100% duty-cycle you get. . . what, exactly?

My code in "long stuff" is not relevant. The important is that I just notice a difference between no load and complex calculation.

Well...yeah, complex calculation takes longer than nothing. That's pretty obvious. Also, Serial prints on every pass through loop are going to bog down the processor. You can only clock data out so fast on Serial, and once the buffer fills up (and it will), the print statements block until there's room.

tl;dr, Serial prints take a loooooonnnnnggggg time, relatively speaking.

This simple code:

uint32_t previous = 0;
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600 );

void loop() {
  // put your main code here, to run repeatedly:
uint32_t start = micros();
Serial.println(start - previous );
previous = start;

Runs loop at 160 Hz, and it's all because of the Serial statements. Get rid of them, and your code will speed way up.

Oh, and as for actually solving your problem, I wanted to do the exact same thing one time, and come up with this solution. Paste this code to the end of your loop():

static unsigned long count=0;
  static unsigned long last_monitor_time=0;
  if( millis()-last_monitor_time >= 1000 )
    Serial.println( count );
    count = 0;
    last_monitor_time = millis();

Every second, it will print to Serial how many times loop() ran in the previous second. Because the Serial calls are much rarer, they shouldn't block and won't significantly affect the code's timing.

Thanks for your answer, I did in your manner over 5 seconds, I get a 4kHz treatment.