How Fast Does Your Arduino Code Run? ⏱

preview_player
Показать описание
In this video you will learn how to measure the execution speed of any Arduino code very accurately just by adding a few lines of code! We'll talk about clock cycles, timers, and how to set them up to figure out exactly how fast your code is.

By the end of this video, you will have a powerful way to tell how fast a piece of Arduino code runs. We'll also measure the accuracy of delay(), compare the speed of digitalWrite() versus direct port register manipulation, and see how choosing different pins can affect digitalWrite's speed.

Arduino Port Registers Reference:

Dice Roller Tutorial:

The Arduino Uno:

Code from the video:

Blog post: 👇👇
Рекомендации по теме
Комментарии
Автор

6:35 TIP: If your frequency is 16 Mhz then (1/f = T) it means that 1 cycle takes 1/16 000 000 = 62.5 ns and if you have 38 cycles. 38 * 62.5 ns = 2375 ns = 2.38 micro seconds.

Electrologia
Автор

Keren om, penjelasannya sangat bagus👍

pmwicaksana
Автор

It would be interesting to see how this could be adapted to work with the new Arduino UNO R4. It is not based on an ATmega chip therefore the registers are different and from what I could see also the execution of instruction is rather different. I tried to apply the same concept presented in this video using the General Purpose Timers but I could not obtain a working example. Maybe you are able to do it having more experience in the field.

thomasdalberto
Автор

yes yes, I made a crude arduino clock a few months ago, you idea will help improve it. it won't be Casio accuracy but its good enough.

nsfeliz
Автор

Thanks! Always knew digitalWrite takes a while, but never cared enough to measure how long precisely :)

UltimateRobotics
Автор

Mind blowing video. May god keep you happy and healthy.

I_am_Ashok
Автор

Man, I was looking for these type of videos where they explain the register level timing codes and their functions. Great video!

noon
Автор

Super cool and interesting, the part with the delaymicroseconds not being accurate actually surprised me.

And thanks a lot for the reference on the port manipulation video :)

talofer
Автор

I think if we are going to use thath is better use justo c to program

diegoflores
Автор

So if we did 1000/microseconds we will get the frequency of the loop in Hz or as known as pulling rate? I am little bit confused about (sampling rate, frequency rate and pulling rate)? Also can this code be used with STM32? Thanks

sarwarn
Автор

How u make this website to run code make video on it

nishantkurade
Автор

Subscribed. Awsome content it makes my code more optimized.

jenalynreyes
Автор

what if you use port manipulation, create an array like const byte leds [6] = { B00010000, B... the 1s is the address in the port D you want to turn on.
so you call just the memory to PORTD = leds[number];

ricardogava
Автор

This is what I've been looking for. Experimenting with branchless Arduino programming and measuring its speed improvement has been problematic.

I would be interested in your take on branchless Arduino programming.

chucklearnslithics
Автор

Fantastic video. This video, this video and the optimize video are really explained very well and I learned a good bit in them. Hopefully we see more of these in the future!
Btw WOKWI is a godsend and I am grateful everyday for it.

ajciccone
Автор

I do not understand half of the thing mentions here because I am a true n00b. But I learn that speaking near to machine language is the best. If we can code with pure 1 and 0 then we would achieve the fastest and most accurate timing.

realchristopher