HOW TO OPTIMIZE ARDUINO CODE

In microcontroller systems where resources are constrained, developers invest a lot of time and resources to produce efficient code. The main objective is to improve the overall performance of the system. The article we present here explores various techniques to optimize our Arduino code. We categorize these techniques into two: general techniques and advanced techniques. We will give a general description of each method, and then we will take a look at some examples.

In the context of Arduino programming, code optimization is a method of improving the code to produce an efficient program. The idea is to improve certain aspects of the program, such as program size, memory consumption, execution time, throughput, and power consumption. Ideally, an optimized program is smaller in format, consumes less memory, has a lower execution time, and consumes less power. These are important design considerations that should be taken into account when writing code for the application in resource-constrained embedded systems. But the most crucial parameter of code optimization is that its output must be the same as the output of the non-optimized code.

CONSIDERATIONS IN OPTIMIZING CODE

There are a few factors, however, which developers should consider before optimizing their code. One of the biggest challenges is to know what and when to optimize. For example, if the problem statement is to develop an Arduino program for monitoring the level of water in a tank, optimization is unnecessary because you are not likely to exceed the Arduino’s limits in any way. Furthermore, the process of code optimization costs money and time. Therefore, you should optimize the code that really needs optimization.


Having said that, if other solutions such as using a bigger Arduino are not feasible, then you can consider code optimization as an option for your project. In this article, the author will focus on techniques for optimizing code on the popular Arduino Uno. The methods we are going to discuss here can also be applied to other Arduino boards.

WHY OPTIMIZING CODE IS IMPORTANT

We learned in the previous article that some data is created when executing an Arduino program. This data is related to function calls and interrupt routines. Problems may arise when the sketch or the generated data require more space than the designated size. When this happens, the Arduino program may fail in different ways. Hence, there is a need to optimize the code and avoid a situation as shown in the diagram (b) below.

As we have mentioned earlier in this article, code optimization produces faster code. This result is not only necessary for performing calculations but also for executing input-output operations. For instance, these operations may include communicating with external hardware devices or merely updating the status of the analog and digital pins. It is necessary for applications that demand tighter control or applications which require faster feedback loops, such as automatic control systems.

In short, these are the benefits of optimizing your Arduino code:

  1. Code runs faster
  2. Better code readability.
  3. Code maintenance
  4. Saves memory.

TECHNIQUES FOR CODE OPTIMIZATION

The diagram below shows the different techniques developers use to optimize their code on the Arduino. Some of the techniques are language-independent, and some are tailored to work only on the Arduino.

REMOVING UNNECESSARY CODE

Unnecessary code in this context refers to any unused functions, libraries, or variables that you may have included in your sketch. Even though they are not used in the sketch, they still occupy some space in memory. Therefore, it is necessary to remove such dead code in your sketch to free up some memory. It is simple to check for any unused variables in your sketch; here is an example.

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  int myPin = 10;
}

void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   
  delay(500);                      
  digitalWrite(LED_BUILTIN, LOW);    
  delay(500);                       
}

The unused integer variable (myPin), flags a warning in the message section of the Arduino IDE as shown below, and you can remove the unused variable.

USING SMALLER DATA TYPES

In the context of computer programming, a variable is a way of naming and storing a value in memory for later use. It consists of a type, a name and optionally, an initial value. Primarily, we need to understand the data types for representing variables in an Arduino sketch.

Data TypeSize (bits)Values
bool81 or 0
char8-128 to127
unsigned char, byte80 to 255
short, int16-327768 to 32767
unsigned int, word160 to 65535
long32-2147483648 to 2147483648
unsigned long320 to 4294967295
float, double321.175e-38 to 3.402e38

As we can see from the table above, different data types have different sizes. Therefore, one should always use a data type that has the smallest range to accommodate the data. It is also advantageous to have a rough idea of the scope of your data because using more extensive data types results in a larger sketch size.

USING FUNCTIONS INSTEAD OF REPEATING CODE

We can demonstrate this principle by using an example of an SOS Morse Code distress signal. Three dots represent S, and O is represented by three dashes. So, SOS in Morse Code will be 3 dots, 3 dashes, and 3 dots. In our implementation, each dot is 250 milliseconds long, and each dash is 1000 milliseconds long. This is a perfect example since the Morse Code contains code that repeats at regular intervals.

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

void loop()
{
  digitalWrite(LED_BUILTIN, HIGH);
  delay(250);
  digitalWrite(LED_BUILTIN, LOW);
  delay(250); 
  digitalWrite(LED_BUILTIN, HIGH);
  delay(250);
  digitalWrite(LED_BUILTIN, LOW);
  delay(250);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(250);
  digitalWrite(LED_BUILTIN, LOW);
  delay(250);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(250);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(250);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(250);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(250);
  digitalWrite(LED_BUILTIN, LOW);
  delay(250);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(250);
  digitalWrite(LED_BUILTIN, LOW);
  delay(250);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(250);
  digitalWrite(LED_BUILTIN, LOW);
  delay(250);
  delay(3000);
}

This can be re-written as:

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

void loop()
{
  dot(); dot(); dot();
  dash(); dash(); dash();
  dot(); dot(); dot();
  delay(3000);
}

void dot()
{
  digitalWrite(LED_BUILTIN, HIGH);
  delay(250);
  digitalWrite(LED_BUILTIN, LOW);
  delay(250);
}

void dash()
{
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(250);
}

Using functions for this problem results in about 200 bytes of memory savings. This is mainly because a single instance of the function is created in memory. And when the function is called again, the CPU loads the code again from its location in memory, without having to re-create the variables.

USING LOCAL VARIABLES INSTEAD OF GLOBAL VARIABLES

For us to understand the benefits of using local variables, we need to know how the Arduino treats the local and global variables. But firstly, a variable is called a Global Variable if it is declared before the setup() and loop() functions. While a Local Variable is a variable that we can maintain and call within a function.

A global variable can be accessed by any function within the sketch, whereas a local variable is only visible within that function only. The effects of these variables on the functionality of the program are beyond the scope of this article since we are only interested in the potential performance issues of global versus local variables.

HEAP VERSUS STACK

Global variables are allocated to the heap, while the local variables are allocated to the stack. The difference between these two locations is the speed of access, or generally, the number of machine instructions required to complete a task. Because of the different architectures of these two systems, the stack has higher access times compared to the heap. As a result, it is faster to access local variables than global variables.

Since global variables are declared before any function, this means that they are assigned only once. This value is kept for the entire duration of your program, whereas the local variables are allocated when your application executes a particular function. That value is then deallocated when the program completes executing the function. This means that the size of the global variables remain the same throughout your program, while the capacity of local variables varies.

To conclude, we can use local variables to optimize the Arduino code since the speed of access, and memory size directly affects the overall performance of the Arduino. In the future, we hope to investigate the effect of Dynamic Memory Allocation on the performance of the Arduino code.

F() STRINGS

Printing a lot of strings on the serial monitor or the LCD consumes a lot of RAM. To save the precious RAM, such strings can be saved on the Flash memory instead. To achieve this, the Arduino employs the F() macro. This simple, yet powerful solution forces the compiler to put the enclosed string in PROGMEM. Here is an example: Serial.print("Optimizing Code");

We can represent this code as: Serial.print(F("Optimizing Code"));

Including such a macro on these two words "Optimizing Code" , can save as much as 16 bytes. However, the F() macro only works on string literals.

MOVING CONSTANT DATA TO PROGMEM

In general, the Arduino stores variables in SRAM. AS we have noticed earlier, the size of these variables can change during program execution. To avoid running out of RAM, we need to control the data that goes into this memory block. To achieve this, we use the PROGMEM keyword to store the data in program memory instead of RAM. It is particularly useful for data that never changes, such as constants. The drawback, however, is that this is slower; but the bigger picture is that we save RAM. Here is an example of PROGMEM implementation.

const int16_t chars[] = {200, 101, 521, 24, 892, 3012, 100};

becomes: const int16_t chars[] PROGMEM = {200, 101, 521, 24, 892, 3012, 100};

To read the variable from program memory, we use the following code:

void ReadData() {
  unsigned int displayInt;
  
  for (byte k = 0; k < 7; k++) {
    displayChars = pgm_read_word_near(chars + k);
    Serial.println(displayChars);
  }
  Serial.println();
}

The for loop in the code above assumes that you know the size of the data in your variable. However, if this information is not available at hand, you can replace the for loop with the code below:

for (byte k = 0; k < (sizeof(chars) / sizeof(chars[0])); k++) {
    displayInt = pgm_read_word_near(chars + k);
    Serial.println(displayInt);
  }

And we get the following values:

USING RESERVE() FOR STRINGS

Another method to optimize the Arduino code is to manage the memory of strings that grow in size. To do so, we use the reserve() function to pre-allocate memory for such strings. If we do not control this, errors that relate to memory fragmentation may occur, resulting in reduced performance. Here is the code for implementing the reserve() function.

  String string;
  string.reserve(50);

To use this correctly, we declare a variable of type String, and then pass on the number of bytes in memory to store this string.

ADVANCED TECHNIQUES

DIRECT PORT MANIPULATION

We can program the Arduino using pure C since the software is based on the avr-gcc compiler, and the hardware is based on Atmel AVR microcontrollers. Each pin on the microcontroller consists of the following register bits: PINxnDDxn, and PORTxn. A detailed description of these register bits can be found from the Atmega datasheet.

Two main improvements can be brought by direct port manipulation of your Arduino. Firstly, its speed. Direct port control results in much faster I/O control, thereby saving a couple of microseconds. Secondly, direct port control reduces the amount of memory your sketch will use. For example, the two code snippets below implement a simple LED blink program. The first one uses Arduino digitalWrite() functions, while the second uses direct port control in pure C.

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

void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   
  delay(1000);                       
  digitalWrite(LED_BUILTIN, LOW);    
  delay(1000);                       
}
void setup() {
  DDRB |= (1<<PD5);
}

void loop(){
  PORTB |= (1<<PD5);
  _delay_ms(1000);
  PORTB &= !(1<<PD5);
  _delay_ms(1000);
}

The size of the second program is 488 bytes, compared with the digitalWrite() program, which takes about 924 bytes of memory. However, there is a lot of debate on whether direct port manipulation in pure C counts as a method of code optimization for the Arduino. The Arduino software was written to simplify the process of microcontroller programming. The libraries “hide” this low-level C from us. Therefore, reverting to pure C can be seen as negating the purpose of using Arduino. The author has only mentioned this technique to demonstrate the memory aspects of pure C compared to the Arduino style.

REMOVING THE BOOTLOADER

The Arduino prototyping platform simplifies the process of transferring your code to the microcontroller. It simplifies this process by using firmware, instead of an external programmer. This firmware is called the bootloader, and it takes about 2000 bytes of flash memory. When all the options have been exhausted, then you can consider bypassing the bootloader. You can either use the Arduino as ISP or use an external programmer to “burn” your code without using the bootloader. When the microcontroller on the Arduino is programmed this way, the new image will overwrite the bootloader, thereby leaving more space for your sketch.

Source

36 thoughts on “HOW TO OPTIMIZE ARDUINO CODE

türkçe izle

Reply

Excellent post! We will be linking to this great post on our website. Keep up the great writing. Glennis Justis Karina

December 10, 2020 at 12:14 am

download

Reply

But wanna input that you have a very nice site, I love the style it really stands out. Margaux Vergil Peatroy

January 13, 2021 at 3:56 am

izle

Reply

There is perceptibly a bundle to identify about this. I assume you made certain good points in features also. Dannie Clemmie Kenji

January 13, 2021 at 1:57 pm

hd film izle

Reply

I am wordless when I have to comment on your blog. Your writing and thought is beyond any word of praise. Cinderella Nickey Zoeller

January 14, 2021 at 1:28 am

full hd tek parça izle

Reply

Great article! We are linking to this great article on our site. Darcy Sloan Culley

January 16, 2021 at 11:40 pm

full hd izle

Reply

I really enjoy the post. Thanks Again. Really Cool. Alidia Filippo Huxley

January 19, 2021 at 8:44 am

free porn

Reply

Appreciation to my father who informed me about this web site, this website is truly awesome. Zelda Pavlov Jarad

January 21, 2021 at 4:34 pm

web-dl

Reply

I will immediately grab your rss feed as I can not in finding your e-mail subscription link or newsletter service. Dolores Ephrayim Rai

January 24, 2021 at 1:29 pm

porno

Reply

I just want you to know it has been fun watching and reading about this amazing adventure you have been on. Blessings to you both. Aleta Caesar Morrissey

January 25, 2021 at 1:30 pm

altyazili

Reply

Great delivery. Outstanding arguments. Keep up the amazing spirit. Brynn Royall Hughmanick

January 29, 2021 at 5:42 pm

netflix

Reply

Great blog here! Also your website loads up fast! What web host are you using? Can I get your affiliate link to your host? I wish my site loaded up as fast as yours lol Karylin Broddy Cony

January 30, 2021 at 5:24 am

720p

Reply

This information is invaluable. Where can I find out more? Malva Ambrosio Akel

January 30, 2021 at 7:59 pm

dublaj

Reply

Superb, what a blog it is! This web site provides valuable facts to us, keep it up. Justina Cary Phelgen

January 30, 2021 at 8:30 pm

bollywood

Reply

Hurrah! After all I got a weblog from where I know how to truly take valuable facts regarding my study and knowledge. Marlene Darby Benedikta

January 30, 2021 at 8:57 pm

netflix

Reply

I blog quite often and I genuinely appreciate your information. This great article has truly peaked my interest. I will book mark your blog and keep checking for new information about once a week. I subscribed to your RSS feed too. Tani Rik Yuria

January 31, 2021 at 7:13 am

dublaj

Reply

Thanks for the various tips contributed on this blog site. Candy Tiler Michey

January 31, 2021 at 8:18 am

dizi izle

Reply

Major thankies for the blog. Much thanks again. Want more. Wrennie Jeddy Healey

January 31, 2021 at 9:28 am

canli

Reply

I blog frequently and I seriously thank you for your content. Your article has truly peaked my interest. I will bookmark your site and keep checking for new information about once a week. I subscribed to your Feed too. Carlene Fredrick Archer

January 31, 2021 at 4:09 pm

altyazili

Reply

Awesome post. I am a regular visitor of your blog and appreciate you taking the time to maintain the excellent site. I will be a frequent visitor for a long time. Bren Fred Dylane

January 31, 2021 at 6:09 pm

movie download

Reply

This is full of tips that I will be using. Thank you for making them concise and putting them all in one place Berti Nevile Lowis

February 1, 2021 at 4:07 am

movie online

Reply

Very good post. I absolutely appreciate this site. Continue the good work! Sophronia Gregorius Tierza

February 1, 2021 at 5:59 am

filmkovasi

Reply

Magnificent beat ! I would like to apprentice while you amend your website, how could i subscribe for a blog web site? The account helped me a acceptable deal. I had been a little bit acquainted of this your broadcast provided bright clear concept Ebony Garold Allys

February 1, 2021 at 11:20 am

direk

Reply

Hi Dear, are you genuinely visiting this website regularly, if so then you will definitely obtain nice knowledge. Kit Rriocard Sekyere

February 1, 2021 at 1:19 pm

direk

Reply

Muchos Gracias for your blog. Really looking forward to read more. Want more. Antonetta Foss Shirleen

February 1, 2021 at 10:58 pm

movie download

Reply

I like the valuable information you provide in your articles. Brigit Robers Keeton

February 2, 2021 at 1:34 am

online

Reply

I have been examinating out a few of your articles and i can claim pretty clever stuff. I will surely bookmark your blog. Talya Granville Phillipe

February 2, 2021 at 2:57 am

free movies

Reply

Wow this us so pretty, I made a DREAMCATHER for my daughter before. I would love to do this too! Annadiane Swen Armillas

February 2, 2021 at 6:17 am

movies

Reply

I like the valuable information you provide in your articles. Trina Chauncey Paryavi

February 2, 2021 at 7:32 am

series online

Reply

Thank you for the good writeup. It in fact was a amusement account it. Look advanced to far added agreeable from you! However, how can we communicate? Tandie Aguste Hugh

February 2, 2021 at 10:22 am

altyazili

Reply

Very nice blog post. I certainly appreciate this website. Stick with it! Kaela Nickolai Quillan

February 3, 2021 at 12:14 am

episodes

Reply

Nice post. I learn something new and challenging on blogs I stumbleupon on a daily basis. It will always be interesting to read content from other authors and use a little something from their web sites. Barbee Omar Bertrand

February 3, 2021 at 7:57 am

bedava

Reply

Looking forward to reading more. Great blog article. Fantastic. Ilise Harland Audry

February 7, 2021 at 7:34 pm

bluray

Reply

Undeniably believe that which you said. Your favorite justification seemed to be on the internet the easiest thing to be aware of. I say to you, I definitely get annoyed while people consider worries that they just do not know about. You managed to hit the nail upon the top and defined out the whole thing without having side effect , people could take a signal. Will likely be back to get more. Thanks Pegeen Bone Marika

February 8, 2021 at 9:24 am

dizi

Reply

Wow, awesome blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your website is great, let alone the content! Karee Franklin Phina

February 8, 2021 at 1:16 pm

Elvia Piepenbrink

Reply

nice show

April 10, 2021 at 4:32 am

box packaging

Reply

Hello! This is my first visit to your blog! We are a group of
volunteers and starting a new project in a community in the same niche.
Your blog provided us valuable information to work
on. You have done a outstanding job!

April 30, 2021 at 6:01 pm

Leave A Comment

Your email address will not be published. Required fields are marked *