Table of Contents
Arduino Variables
In programming, we name the values that are stored for further processing with a single word called a variable. The value of a variable can change continuously (during runtime). The variable name should be chosen in such a way that it is easily readable and understandable within the program. In addition, a variable also has a data type that defines the value range.
The Variable must be declared before being used. So the data type, the variable name and the value of the variable are set. If no value is specified, the variable value is set to 0.
datatype Variable_Name = Value;
Example:
int motorSpeed = 255;
Comprehensible variable names such as motorSpeed or temperatureSensor improve the readability of the program.
Arduino Data types
A data type describes the value range and the possible operations of a variable. Since the data types of the variables require different amounts of memory, you should be aware of exactly which possible values the defined variable can have when creating the program: Is it only a digital state with the values yes and no or can the variable hold an entire text? You should select the variable data type accordingly. The most important data types and their value ranges for creating Arduino codes are listed below:
Data type | Range | Memory size | Explanation | Example |
boolean | true/false | 1 byte | Value for Yes/No or On/Off, | boolean machineStatus = true; |
int | -32,768 to 32,767 | 2 bytes | Integer values for values without decimal places | int potiValue = 1023; |
unsigned int | 0 to 4,294,967,295 | 2 bytes | Integer for positive values without decimal places | int tempValue = -5; |
byte | 0 to 255 | 1 byte | Low values (and correspondingly small memory requirements) | byte brightnessLED = 126; |
word | Unsigned 16-bit or 32-bit | 16-bit on Atmega boards. 32-bit on Due and Zero | Be aware of processor used | word value = 1500; |
long | -2,147,483,648 to 2,147,483,647 | 4 bytes | Integer numbers with big values | long offset = -15362; |
unsigned long | 0 to 4,294,967,295 | 4 bytes | Unsigned Integer numbers with big values | unsigned long secondsPassed = 45379; |
float | -3.4028235E+38 to 3.4028235E+38 | 4 bytes | Values with decimal places | float voltageValue = 3.3; |
double | -3.4028235E+38 to 3.4028235E+38 | 4 bytes | -3.4028235E+38 to 3.4028235E+38 | double distanceGPS= 722.54; |
char | -128 to 127 | 1 byte | Value for storing one character | char charR = ‘R’; |
unsigned char | 0 to 255 | 1 byte | Usually used with numbers having range 0 to 255 | unsigned char charNumber = 255; |
string | depends on definition | depends on need | Strings like texts or several characters | string brand[ ] = “ROBOSANS”; |
array | depends to definition | Depends on array size | store values in variables with table form like e.g. lists of ports or configuration values | int portsPWM[ ] = {3,5,6,9,10,11}; |
In the following section we will summerize the datatypes in short.
What are the data types used in Arduino ?
The the data types used in Arduino are:
boolean : 8 bit, logical true/false
int : 16 bit, -32,768 to 32,767
unsigned int : 16 bit, 0 to 4,294,967,295
byte : 8 bit, 0 to 255
word : 16 bit, 0 to 65,535
long : 32 bit, -2,147,483,648 to 2,147,483,647
unsigned long : 32 bit, 0-4,294,967,295
float : 32 bit, -3.4028235E38 to 3.4028235E38
char : 8 bit, 128 to 127
unsigned char : 8 bit, 0 to 255
Arduino sizeof
The Arduino sizeof function returns the number of bytes in a variable, or bytes an array occupies. Its syntax is:
sizeof(variable);
It returns the size of the variable/ array in number of bytes. The following sketch defines variables in various types and print the sizeof the variable on the serial monitor.
boolean boolVal;
int intVal;
unsigned int uintVal;
byte byteVal;
word wordVal;
long longVal;
unsigned long ulongVal;
float floatVal;
char charVal;
unsigned char ucharVal;
void setup() {
Serial.begin(9600);
Serial.print("Size of boolean in bytes = ");
Serial.println(sizeof(boolVal));
Serial.print("Size of int in bytes = ");
Serial.println(sizeof(intVal));
Serial.print("Size of unsigned int in bytes = ");
Serial.println(sizeof(uintVal));
Serial.print("Size of byte in bytes = ");
Serial.println(sizeof(byteVal));
Serial.print("Size of word in bytes = ");
Serial.println(sizeof(wordVal));
Serial.print("Size of long in bytes = ");
Serial.println(sizeof(longVal));
Serial.print("Size of unsigned long in bytes = ");
Serial.println(sizeof(ulongVal));
Serial.print("Size of float in bytes = ");
Serial.println(sizeof(floatVal));
Serial.print("Size of char in bytes = ");
Serial.println(sizeof(charVal));
Serial.print("Size of char in bytes = ");
Serial.println(sizeof(ucharVal));
}
void loop() {
}
The resulting sizeof variables in byte on the serial monitor is:

Arduino sizeof array
To get the size of array, use the function sizeof with the array name inside as follows
int myArray[] = {11, 12, 13, 14, 15, 16, 17, 18};
int arraySize = sizeof(myArray);
Arduino length of array
To get the length of an array, divide the number of bytes present in the given array with the number of bytes in the array data type as follows
int myArray[] = {11, 12, 13, 14, 15, 16, 17, 18};
int arrayLength = sizeof(myArray) / sizeof(int);
Do you want to learn more? you can read these articles down below.
16×2 LCD Interfacing With Arduino
DHT11 Humidity And Temperature Sensor
Interfacing 7 Segment Display with Arduino
Sources: