Why is my Atmega328P U running at 177kHz?

If that statement even makese sense because clock speed is probably not the same as the execution speed of loop()?

I am using an Arduino UNO R3 with an Atmega328P U on top.

As I'm rather new to microcontrollers I would like to understand "how fast" they are. Particularly because when it comes to data transmission I'm aware that there's some limitations to what I'll be able to do.

However, I thought I can go simple about it and just see how many times I can increment a counter for each loop. Since printing something to the console takes time I count up for two seconds (=2,000,000 mircoseconds) and divide by two:

unsigned long startTime = micros();
unsigned long counter = 0;

void loop() {

  unsigned long elapsedTime = micros() - startTime;

  if (elapsedTime >= 2000000) {
    Serial.println(counter >> 1);
    startTime = micros();
    counter = 0;
  }

  counter += 1;
}

Unless I have an error, it looks like the loop() runs at around ~177kHz which is not really what I expected:

18:07:37.715 -> 176716
18:07:39.706 -> 176716
18:07:41.721 -> 176716
18:07:43.718 -> 176716

Am I missing something here?

This article states that it would run either at 8MHz or 128kHz depending on its configuration.

How can I use the 8MHz oscillator and why am I seeing 177kHz instead of 128kHz?

With 'counter' I suppose you are trying to measure loop() execution time. But I'm not at all clear, why you think this should run at 128kHz or for that matter, and any known rate?

When you compile the code, what you end up with is some arbitrary number of machine cycles measured in this case, in a few hundred. There is also some overhead in the repetitive call to loop() that the core makes.

The IC has clock options, if you read the data sheet you can find out what they are. But the one you are holding, probably has the standard fixed 16MHz external oscillator.

The 128kHz figure is for a clock configuration that you are not using at all.

So that would actually mean that the clock speed must be higher than 128kHz right?

Otherwise I wouldn't be able to count up 177k-times per second. Therefore I can probably assume that the clock speed is 1MHz or 8MHz (rather 1 than 8)?

The UNO R3 clock frequency is always 16MHz. I don't understand why you would doubt it.

I was just assuming that the internal clock of the chip is being used. Didn't know that the Arduino provides an external clock to it.

Okay, that's certainly interesting. If somebody asked my "how many times do you think this thing can 'add one' to an integer given the clock speed is 16MHz" I would have said "more than 177k-times".

Why? The whole system is very well documented.

I looked at the name of the chip and googled the chip. Not the board.

"As I'm rather new to microcontrollers "

I guess you need to understand the whole system better. But I can't lay out the whole Arduino book here. Please study the documentation.

What did you run the tests on? If you Googled and found the data sheet and spent a little time there, you would know about the multiple clock options I mentioned.

Hello snooc

Take a search engine of your choice and ask the WWW for 'bench mark test arduino' to gain the basic knowledge.

Have a nice day and enjoy coding in C++.

Well yeah, I get it. The chip consumes perhaps a number of cycles in order to perform one instructions. My understanding of hardware got a little rusty over time.

Arduino Uno + Atmega328P U

Will do. Thanks.

Yeah, it's been a while since I wrote C/C++ but I think for the project I am aiming at that knowledge should do. I'm not creating a video game :sweat_smile:

It isn't just that. The software requires a number of instructions to perform each one of certain high level (C/C++) operations as well...

I commend your curiosity but you are looking in the wrong place, you just need to do some reading of original documentation, and tutorials and other reference material.

I'm not telling you to do, anything I didn't have to do. :wink:

Okay well, I literally assumed there's an en endless loop around loop() as in

0000  ; My code goes here
000n goto 0000

I didn't expect other machine code being executed I guess.

Will do :sweat_smile:

If you wish, you can run the MCU of the UNO board at the following frequencies:

minimum: 31.25 kHz     maximum: 8 MHz sing internal 8 MHz oscillator
minimum: 62.5 kHz      maximum: 16 MHz  using external 16 MHz crystal

How can you operate it at 177 kHz?

There we go with that bad word again... :grin:

Ah yeah.. how many times that one word has cost me hours of debugging..

How easily do you think an eight bit processor can add one to a 32 bit value?

(Answer: not very)

It's still faster than the time it takes to call a function and return.

By the way, the OP ran out of the first day reply quota for new members.

I doubt very much there's a call/return in there, just an rjmp.
Optimisers are very good.
Load and store a 32 bit value a byte at a time, not so good