Source: https://roboticsbackend.com/arduino-variable-types-complete-guide/#char
What are the different Arduino variable types?
Whether you are a complete Arduino beginner or you already know how to program, this guide will help you discover and all the most useful Arduino variable types.
First of all, Arduino is a subset of C/C++, with additional functionalities related to the hardware features of the board. So, you might expect to have similar data types. This is true for some types, but with Arduino you also get modified and new exclusive types.
Let’s discover all the Arduino variable types you will use, with the limits and particularities for each of them.
Table of Contents
Arduino Variable Types – Round Numbers
byte
The byte number is the smallest Arduino data type you can use for round numbers when programming with Arduino. A byte contains 8 bits.
A bit is simply a binary piece of information: 0 or 1.
You are learning how to use Arduino to build your own projects?
Check out Arduino For Beginners and learn step by step.
So, a byte will contain 8 binary values. For example, the number 45 written in 8-bit will look like this: 00101101. This is how the byte will be stored in the Arduino. In your program you can choose to either use the binary representation of the number, or the decimal representation (hexadecimal also works).
And you can print a number with the Serial.println() function, using different arguments for different representations of the number.
Here the result in the Serial Monitor will be:
The min value for a byte is 0, and the max value is 255 (byte is an unsigned data type, more on that later in this tutorial).
As you can see that’s quite short, so pay attention when you use this data type. Only use it to store very small numbers.
And what happens if you try to store a number lower than 0, or bigger than 255? Well the program will still work, but your variable will overflow. Overflow means that once the value reaches 256, it will go back to 0.
So, if you try to assign 260 to a byte:
You will get:
260 has become 4, because 256 goes back to 0, then 257 becomes 1, etc… and 260 becomes 4.
So, again, pay attention when using small data types such as byte in your Arduino programs: if you try to use a too big number, the variable will overflow and its value won’t be correct. Your program will still compile, but when running you’ll get all kinds of application errors.
Int
Int, or integer, is one of the most common variable types you will use and encounter.
An int is a round number which can be positive or negative.
On Arduino boards such as Uno, Nano, and Mega, an int stores 2 bytes of information. So, for example, 9999 will be represented by 00100111 00001111. Although you don’t need to know the binary representation, you can just work with decimal numbers.
You can use int everywhere, to store any information represented by a round number. For example: a counter, temperature, number of steps for a stepper motor, angle for a servomotor, etc.
The min value for a 2-bytes int is -32 768 and the max value is +32 767. As for bytes, it can overflow.
For example, if you try to assign 32 768 (which is just above the max limit), the value you will read inside the variable will be -32 768.
And if you try to assign -32 769, you will get +32 767.
Thus, pay attention not to use too big numbers with int. On boards such as Arduino Due and Zero, integers store 4 bytes, so the value range is much higher: -2,147,483,648 to 2,147,483,647.
But on classic Arduino boards (Uno, Nano, Mega, etc.), if you want to use bigger integer numbers you’ll have to use long instead of int.
long
A long is exactly the same as an int, but using 4 bytes. The minimum value becomes -2,147,483,648 and the max value 2,147,483,647. With this you don’t need to worry too much about overflowing.
Usually, you’ll use long when you know (or suppose) the size of an int won’t be enough.
Arduino Variable Types – unsigned
For the standard round number variables, you can add “unsigned” before the data type to modify it a little bit.
If you add “unsigned”, the variable will contain only positive numbers, starting from 0.
This is what we had for byte, which is already an unsigned data type (in fact, similar to unsigned char, which you’ll see later).
unsigned int
To create an unsigned int:
An unsigned int will start at 0 and have a max value of 65 535 (4,294,967,295 in 4 bytes board such as Due/Zero). As you don’t have negative numbers anymore, all those numbers are added to the max positive value you can use.
The concept of overflow here is the same. If you try to use 65 536 you’ll go back to 0.
So, unsigned int can be used if you’re sure that you’ll only store positive numbers (it will enforce it) for the variable. Also, the max limit increases, so for example if you have to use a variable that goes from 0 until 50 000 for reading a sensor, this will be a good option.
unsigned long
To create an unsigned long:
An unsigned long will start at 0 and have a max value of 4,294,967,295, which is a very big number.
Again, the use you’ll make of long is pretty similar to int data type, just for larger numbers.
On Arduino, when you try to get the time with millis or micros, you will get a result in unsigned long.
Arduino Variable Types – bool/boolean
The bool/boolean is a particular Arduino data type which only contains a binary information: 1 or 0 (true or false).
You will use booleans to test conditions where the answer is a simple yes/no.
In Arduino you can use the standard C++ bool type, or the boolean type which is identical. However, the Arduino documentation suggests that you only use bool.
Arduino Variable Types – Float Numbers
For now we’ve only seen how to store and use round numbers with Arduino. But what if you want to store a number such as 3.14 ?
float
A float variable can store a negative or positive float number. As for every data type it has a minimum and a maximum: 3.4028235E+38 to -3.4028235E+38. This is much bigger than long, even though a float is also stored on 4 bytes. The reason is simply because both data types are stored in a different manner.
To create a float:
So, the resolution of a float is greater than round numbers, which make them convenient for some computations or to read a continuous value from a sensor.
Note however that the precision for float is not 100%, contrary to round number data types.
For example, if you try to assign 3.00 inside a float number, the real value might be something like 3.0000001.
double
In boards such as Uno, Mega and Nano, double and float are identical.
On Due for example, the double is stored on 8 bytes instead of 4, which makes it different. You can store even bigger numbers.
One thing to know about float numbers (float or double data type): the Arduino micro-controller will take much more time to process a computation with float numbers than with round numbers. As the computation power is very limited, try to use round numbers as much as you can, and float numbers only when you don’t have the choice.
Arduino Variable Types – Text data types
Great, now you have seen how to store booleans, round and float numbers. The last important category here is how to store text, which is often used when you want to share information with the user (text makes more sense to a human than a hexadecimal code) or to communicate in ASCII for example.
char
The char data type is quite particular because it’s mainly used to store letters, but in the end the real value is a number between -128 and +127 (stored in 1 byte). The corresponding values between letter and numbers are the ones you can find in the ASCII table.
For example, the letter ‘F’ uppercase has the value 70, and ‘f’ lowercase has the value 102.
You can make computations using char. If you add +1 to the letter ‘F’, the number will become 71, and the corresponding letter ‘G’ uppercase.
The char data type can be quite useful to send data over Serial/I2C/SPI between devices. It doesn’t take much space (1 byte which is the minimum), so it’s faster to transfer, and you can directly associate an action to a letter – ex: ‘m’ to move the motor, ‘s’ to stop, ‘a’ to accelerate, etc.
unsigned char
The unsigned char data type is in fact the exact same as the byte variable type. Both have a minimum value of 0 and a max of 255.
It is recommended that you use byte instead of unsigned char.
String
The String data type is specific to Arduino, you can’t find it in standard C/C++. Also, note the uppercase “S”. The variable type is String, not string.
You will use this type to store text. You can perform operations such as concatenation directly with the “+” operator.
String is a class, it’s not a primitive data type. To create a string, you’d have to use an array of chars. With the String class, you get to simplify your life and you also have a bunch of extra functionalities.
Array of Variables
And of course, each of the Arduino data types you’ve seen here can be stored inside an array.
Make sure to only use one single data type for all elements of an array.
Conclusion – Arduino Data Types
In this tutorial you have discovered all the standard Arduino data types you’ll most frequently use in your programs.
Don’t hesitate to come back to this guide whenever you’re not sure about which variable type you should use, or what are their limitations.
A few general pieces of advice to finish:
- As you saw, you could get erratic results if some of your variables overflow. So make sure to always now what is the range and the min/max value a variable can store.
- Also, the more complex the data type, the more computation time it will take. And the Arduino is much less powerful than a “standard” computer, so you’ll have to take that into account. If you see that your program is too slow, then you might want to measure how long a certain action takes, and try to make it faster by changing the data types for some variables and rearranging/optimizing the operations.
- Finally, if you’re using round numbers and you know that you only expect positive numbers, use the unsigned version of the data type. Your program will be clearer and less prone to errors.
Now, don’t forget that premature optimization is the root of all evil. When you write your programs, use larger data type if any hesitation, and only optimize when you see that things are too slow.