Skip to content

Arduino Datatypes and Variables

    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 typeRange    Memory sizeExplanation  Example
    booleantrue/false1 byteValue for Yes/No or On/Off,  boolean machineStatus = true;
    int-32,768 to 32,7672 bytes  Integer values for values without decimal places  int potiValue = 1023;
    unsigned int0 to 4,294,967,295    2 bytesInteger for positive values without decimal places  int tempValue = -5;
    byte0 to 2551 byteLow values (and correspondingly small memory requirements)    byte brightnessLED = 126;
    wordUnsigned 16-bit or 32-bit16-bit on Atmega boards.
    32-bit on Due and Zero
    Be aware of processor usedword value = 1500;
    long-2,147,483,648 to 2,147,483,6474 bytesInteger numbers with big values        long offset = -15362;
    unsigned long0 to 4,294,967,2954 bytesUnsigned Integer numbers with big values  unsigned long secondsPassed = 45379;
    float-3.4028235E+38 to 3.4028235E+384 bytesValues with decimal places  float voltageValue = 3.3;
    double-3.4028235E+38 to 3.4028235E+384 bytes-3.4028235E+38 to 3.4028235E+38double distanceGPS= 722.54;
    char-128 to 127  1 byteValue for storing one character  char charR = ‘R’;
    unsigned char0 to 2551 byteUsually used with numbers having range 0 to 255unsigned char charNumber = 255;
    stringdepends on definitiondepends on needStrings like texts or several charactersstring brand[ ] = “ROBOSANS”;
    arraydepends to definitionDepends on array sizestore values in variables with table form like e.g. lists of ports or configuration valuesint portsPWM[ ] = {3,5,6,9,10,11};
    Arduino Data types

    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 variables in byte
    Arduino sizeof variables in byte

    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:

    arduino.cc