Experimenting a lot with the Arduino platform reveals a lot of features and traps and we collected a few of them to make the development of Arduino applications easy.
Speed up I/O Access
On every AVR based Arduino board, the clock speed is 16MHz. Each instruction on the controller completes under 4 cycles, so theoretically, you can do 4 million instructions in one second. However, the I/O speed is much slower. That’s because of the digitalWrite and digitalRead functions. Each time you execute them, a lot of additional code executes also. The additional code is responsible for detecting & mapping the numerical pins to a output port. On every microcontroller, the I/O devices are mapped in groups of 8 pins to ports, which are special registers in the controller.

(SOURCE: VISUALHUNT)
So the question arises, how slow is it then? In some measurements, the maximal output speed of the following program is around 116-117 KHz:
void setup() { pinMode(13, OUTPUT); } void loop() { digitalWrite(13, LOW); digitalWrite(13, HIGH); }
We can improve our program by wrapping the loop code in an infinite loop. It will speed up the execution a litle bit, because the CPU has to do less function calls. The improved loop:
void setup() { pinMode(13, OUTPUT); } void loop() { while (1) { digitalWrite(13, LOW); digitalWrite(13, HIGH); //required if you do serial communication if (serialEventRun) serialEventRun(); } }
In some measurements, the output speed was around 126-127KHz, which is 8.6% faster, but we can improve it. To really top the output speed, we will have to use low level port access. To use low level poor access, we will have to read the details of the microcontroller used on the board, but it just takes away the beauty of the Arduino platform. There’s a library for that purpose, that speeds up I/O access.
It has some limitations. Mainly that it only supports the Uno, Nano, Leonardo, Mega, and Attiny45/85/44/84 models. The code for the library can be found here.
void setup() { pinMode(13, OUTPUT); } void loop() { while (1) { //WriteD13 writes Pin 13. D means digital. //to write to pin 8 use WriteD8 function. WriteD13(HIGH); WriteD13(LOW); //required if you do serial communication if (serialEventRun) serialEventRun(); } }
It provides inline functions for fast I/O access. The following example produces an output signal around 1Mhz, which is really nice. But, if you put the code in the earlier discussed infinite loop, it gets even faster. With it, you can produce an output signal around< 2,4MHz.
Get Available Free RAM

(SOURCE: VISUALHUNT)
The following function will return the currently free RAM memory in bytes.
unsigned int UnusedRAM() { unsigned int byteCounter = 0; byte *byteArray; while ( (byteArray = (byte*) malloc (byteCounter * sizeof(byte))) != NULL ) { byteCounter++; free(byteArray); } free(byteArray); return byteCounter; }
Inline Your Small Functions
Inline functions are a great way to speed up your program, especially, if you call them in the loop function. By default, the inline keyword only suggests the function for inlining. The compiler may decide that it’s not worth inlining in the optimization phase.

(SOURCE: VISUALHUNT)
The GCC manual says that an inline function is as fast as a Macro. As always, there’s a catch. If you make a function inline, it will make your program code size larger, if you call that function in several places.
To force a function inline, use the __inline keyword
__inline void SomeFunction() { Serial.println("I'm an inline function"); }
Hope these tips helped and will go a long way to help you build Arduino applications and let’s change the world! Stay tuned for more next week on #TuesdayTips