Timing Arduino code

This is a quick and very helpful recipe. There are several time-sensitive applications on the Arduino, and sometimes we need to find the speed at which the Arduino executes various commands. Here we have a simple implementation that will tell us how much time it takes to set a digital pin at HIGH and LOW 10,000 times.

Getting ready

For this recipe, we require an Arduino board connected to a computer via USB.

How to do it…

We just need to write the following code:

// Variable to hold the passed time
unsigned long time = 0;
int pin = 3; // Declare a pin

void setup(){ 
  Serial.begin(115200); // High speed Serial 
  pinMode(pin, OUTPUT); 
}

void loop(){
  // Get current time
  time = micros();
  
  // Code to be tested for execution time
 for ...

Get Arduino Development Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.