One importatn thing to know are the variable data types existing on Arduino, and that is why I’ll try to talk about Variables Types in Arduino.

For starters, here you have a table of the different types, their sizes (something very important to consider), and links to each one in the official documentation. Then I will proceed to make a description of each type so that we can understand them better.

TypeSizeRange
boolean1 byte0 a 1
char1 byte-128 a 127
unsigned char1 byte0 a 255
int2 bytes-32.768 a 32.767
unsigned int2 bytes0 a 65535
word2 bytes0 a 65535
long4 bytes-2.147.483.648 a 2.147483.647
unsigned long4 bytes0 a 4.294.967.295
float4 bytes3,4028235E-38 a 3,4028235E+38
double4 bytes3,4028235E-38 a 3,4028235E+38
string1 byte per letter + 1 control byteN/A
String()??N/A
arraySum of array elements sizes +1 control byteN/A
enumN/AN/A
structN/AN/A
pointerN/AN/A
voidN/AN/A

In the table you can see that I’ve put string twice, but don’t worry, it’s not a mistake. This is because in the official Arduino documentation they call string an array of chars, and that is why the size of a string is the same as that of the array in the table.
In addition to the so-called string (array of chars), there is a type of String variable, which is different from the one already mentioned above. Its use is discouraged because it consumes more memory, but we can find cases in which it is impossible to avoid it. Among the differences that exist, the array is of fixed size in its declaration and this cannot vary, while the string does allow it. We also have some specific functions for the String type that would be impossible for us to use in an array.

Numerical

Variables of the numerical type can only contain numbers as their name indicates, and within the numerical type you can find two subtypes, which are the integers (bool, int, char …) and decimals (float and double). Another thing to know is that all the variables indicated in the upper part of the table are numerical variables, and except for String (), array, enum, struct, pointer and void, all can be treated as such.

Boolean

The boolean variable is a variable that will allow us to store zero or one in it, so it will only be used for use in states such as that of a led (on or off). Given its size it could be replaced by a variable of type byte or char without problems, which would give us more flexibility.

Also, as I explain in my post Arduino: Save space in SRAM, replacing this type with a byte type together with the use of bits, will allow us to have up to 8 booleans in the same byte.

Char and unsigned char

Both types have the same number of numbers, except that char also includes a negative range. If we want to store a number between 0 and 255, we can use the unsigned type for it, while if the number contains negative digits and goes between -128 and 127, then our best option will be unsigned char.
These types are also used to store characters of the extended ASCII table, but char only allows us to store letters of the first 128 characters of the table, while if we use a string (array of chars), it will allow us to store any character of the table.

Esta imagen muestra una tabla de caracteres ASCII extendidos.
Tabla de caracteres ASCII extendidos
void setup() {
  Serial.begin(9600);
}

void loop() {
  char test_array[] = "K";
  char test_char = 75;
  Serial.print(F("Array: "));
  Serial.println(test_array);
  Serial.print(F("Char: "));
  Serial.println(test_char);
  delay(1000);
}

In the example you can see how both give the same result, but nevertheless the array allows us to group several characters to form words and even phrases.

Int, unsigned int and word

These types of variables also enter the group of numerals, and therefore serve to store numbers. Unlike char, this does not allow you to store letters, but only numbers. These types of variables allow you to store a range of numbers between -32,768 and 32,767 for the variable of type int, and between 0 and 65535 for the variable of type unsigned int, which is equivalent to a variable of the type word.

Long and unsigned long

These types of variables will be those of greater rank that we will find within the different types of variables, and therefore the ones that occupy the most. Its use is recommended only in the case that it is necessary to store a very large number, between a range of -2,147,483,648 and 2,147483,647 for signed long, and 0 to 4,294,967,295 for unsigned long.

Float and double

With this type of variables we enter the last of the numerical type and those that will allow us to use decimal numbers. Unlike in C ++, both variables are the same (in C ++ double it uses double the memory and is also larger), and its range is from 3.4028235E-38 to 3.4028235E + 38.

String

This type of variable could be said to really not exist, or at least not in this context, since it is basically an array of type char. To declare it in our program we will never use string, but instead we will use a definition char and some keys indicating that it is an array.

char string[] = "Hola Mundo";

Since it is an array of the char type, its memory consumption will be exactly the same, which is 1 control byte + 1 byte for each character.

String()

Unlike the one indicated above, this is not an array of characters, but rather a real String. Since this type of variable is mutable, its memory consumption is much higher. For example, a basic sketch with a variable containing a long phrase consumes 1,438 bytes using char, while the same phrase in a variable of type String starts to consume 2,690 bytes. That is why its use is discouraged unless it is explicitly necessary, and in the event that it will not vary, the use of the F function is recommended, as indicated in my post Arduino: Save space in SRAM.

Array

Arrays are groups of variables of other types, which allows grouping several variables under the same identifier, as long as they are of the same type. This can be used to generate, for example, an array of chars with a long phrase instead of single characters, or group related integers together. The size of these arrays is equal to the sum of the size of all the variables they contain, plus a control byte.

To access a variable within an array, we will have to do it through its position in that array (index), and taking zero as the first position. With this in mind, if we want to access the third entry of an array, we will have to use the number 2.

char array_chars[] = "Hola"; // (4 * 1) + 1 = 5 bytes
int array_ints[] = {12, 25, 46, 91}; // (4 * 2) + 1 = 9 bytes
float array_floats[5] = {12, 25, 46, 91}; // (4 * 4) + 1 = 17 bytes
Serial.prinln(array_ints[1]); // Enviará por el puerto serial 25

Void is a type of variable of null type, and is generally used for functions that do not return any value.
On the other hand we have the variables of type Enum, struct and pointer which go beyond the intention of this article and therefore I will not explain them at the moment.

Greetings and I hope it has served you.

Daniel Carrasco

DevOps with several years of experience, and cloud architect with experience in Google Cloud Platform and Amazon Web Services. In his spare time experimenting with Arduino and electronics.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.